Commit 571e977f4bdfbad916f5cb3eee30bdd0b4378729

Authored by Josh Klontz
2 parents 063496f6 29a644f2

Merge pull request #190 from biometrics/large_galleries

Better support for dealing with large galleries
openbr/core/bee.cpp
... ... @@ -164,23 +164,30 @@ Mat BEE::readMat(const br::File &matrix, QString *targetSigset, QString *querySi
164 164 int typeSize = isMask ? sizeof(BEE::Mask_t) : sizeof(BEE::Simmat_t);
165 165  
166 166 // Get matrix data
167   - qint64 bytesExpected = (qint64)rows*(qint64)cols*(qint64)typeSize;
168 167 Mat m;
169 168 if (isMask)
170 169 m.create(rows, cols, OpenCVType<BEE::Mask_t,1>::make());
171 170 else
172 171 m.create(rows, cols, OpenCVType<BEE::Simmat_t,1>::make());
173 172  
174   - qint64 read = file.read((char*)m.data, bytesExpected);
175   - if (read != bytesExpected)
176   - qFatal("Invalid matrix size.");
  173 + qint64 bytesPerRow = m.cols * typeSize;
  174 +
  175 + for (int i=0; i < m.rows;i++)
  176 + {
  177 + cv::Mat aRow = m.row(i);
  178 + qint64 bytesRead = file.read((char *)aRow.data, bytesPerRow);
  179 + if (bytesRead != bytesPerRow)
  180 + {
  181 + qFatal("Didn't read complete row!");
  182 + }
  183 + }
177 184 if (!file.atEnd())
178 185 qFatal("Expected matrix end of file.");
179 186 file.close();
180 187  
181   - Mat result;
  188 + Mat result = m;
182 189 if (isDistance ^ matrix.get<bool>("negate", false)) m.convertTo(result, -1, -1);
183   - else result = m.clone();
  190 +
184 191 return result;
185 192 }
186 193  
... ...
openbr/core/qtutils.cpp
... ... @@ -99,7 +99,16 @@ void readFile(const QString &amp;file, QByteArray &amp;data, bool uncompress)
99 99  
100 100 void writeFile(const QString &file, const QStringList &lines)
101 101 {
102   - writeFile(file, lines.join("\n"));
  102 + QFile f(file);
  103 + touchDir(f);
  104 +
  105 + if (!f.open(QFile::WriteOnly))
  106 + qFatal("Failed to open %s for writing.", qPrintable(file));
  107 +
  108 + foreach(const QString & line, lines) {
  109 + f.write((line+"\n").toLocal8Bit() );
  110 + }
  111 + f.close();
103 112 }
104 113  
105 114 void writeFile(const QString &file, const QString &data)
... ...
openbr/plugins/format.cpp
... ... @@ -20,6 +20,9 @@
20 20 #ifndef BR_EMBEDDED
21 21 #include <QtXml>
22 22 #endif // BR_EMBEDDED
  23 +#include <QFile>
  24 +#include <QFileInfo>
  25 +
23 26 #include <opencv2/highgui/highgui.hpp>
24 27 #include <opencv2/highgui/highgui_c.h>
25 28 #include "openbr_internal.h"
... ... @@ -117,22 +120,30 @@ class binFormat : public Format
117 120  
118 121 void write(const Template &t) const
119 122 {
120   - QByteArray data;
121   - QDataStream stream(&data, QFile::WriteOnly);
122   - if (raw) {
123   - const Mat &m = t;
124   - stream.writeRawData((const char*)m.data, m.total()*m.elemSize());
125   - } else {
126   - Mat m;
127   - t.m().convertTo(m, CV_32F);
  123 + QFile f(file);
  124 + QtUtils::touchDir(f);
  125 + if (!f.open(QFile::WriteOnly))
  126 + qFatal("Failed to open %s for writing.", qPrintable(file));
  127 +
  128 + Mat m;
  129 + if (!raw) {
  130 + if (t.m().type() != CV_32FC1)
  131 + t.m().convertTo(m, CV_32F);
  132 + else m = t.m();
  133 +
128 134 if (m.channels() != 1) qFatal("Only supports single channel matrices.");
129 135  
130   - stream.writeRawData((const char*)&m.rows, 4);
131   - stream.writeRawData((const char*)&m.cols, 4);
132   - stream.writeRawData((const char*)m.data, 4*m.rows*m.cols);
  136 + f.write((const char *) &m.rows, 4);
  137 + f.write((const char *) &m.cols, 4);
133 138 }
  139 + else m = t.m();
134 140  
135   - QtUtils::writeFile(file, data);
  141 + qint64 rowSize = m.cols * sizeof(float);
  142 + for (int i=0; i < m.rows; i++)
  143 + {
  144 + f.write((const char *) m.row(i).data, rowSize);
  145 + }
  146 + f.close();
136 147 }
137 148 };
138 149  
... ...
openbr/plugins/gallery.cpp
... ... @@ -574,32 +574,55 @@ class txtGallery : public Gallery
574 574 Q_PROPERTY(QString label READ get_label WRITE set_label RESET reset_label STORED false)
575 575 BR_PROPERTY(QString, label, "")
576 576  
577   - QStringList lines;
578   -
  577 + QFile f;
579 578 ~txtGallery()
580 579 {
581   - if (!lines.isEmpty())
582   - QtUtils::writeFile(file.name, lines);
  580 + f.close();
583 581 }
584 582  
585 583 TemplateList readBlock(bool *done)
586 584 {
  585 + *done = false;
  586 + if (f.atEnd())
  587 + f.seek(0);
  588 +
587 589 TemplateList templates;
588   - foreach (const QString &line, QtUtils::readLines(file)) {
589   - int splitIndex = line.lastIndexOf(' ');
590   - if (splitIndex == -1) templates.append(File(line));
591   - else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1)));
  590 +
  591 + for (qint64 i = 0; i < readBlockSize; i++)
  592 + {
  593 + QByteArray lineBytes = f.readLine();
  594 + QString line(lineBytes);
  595 +
  596 + if (!line.isEmpty()){
  597 + int splitIndex = line.lastIndexOf(' ');
  598 + if (splitIndex == -1) templates.append(File(line));
  599 + else templates.append(File(line.mid(0, splitIndex), line.mid(splitIndex+1)));
  600 + }
  601 +
  602 + if (f.atEnd()) {
  603 + *done=true;
  604 + break;
  605 + }
592 606 }
593   - *done = true;
  607 +
594 608 return templates;
595 609 }
596 610  
  611 + void init()
  612 + {
  613 + f.setFileName(file);
  614 + QtUtils::touchDir(f);
  615 + if (!f.open(QFile::ReadWrite))
  616 + qFatal("Failed to open %s for read/write.", qPrintable(file));
  617 + }
  618 +
597 619 void write(const Template &t)
598 620 {
599 621 QString line = t.file.name;
600 622 if (!label.isEmpty())
601 623 line += " " + t.file.get<QString>(label);
602   - lines.append(line);
  624 +
  625 + f.write((line+"\n").toLocal8Bit() );
603 626 }
604 627 };
605 628  
... ... @@ -612,26 +635,49 @@ BR_REGISTER(Gallery, txtGallery)
612 635 class flatGallery : public Gallery
613 636 {
614 637 Q_OBJECT
615   - QStringList lines;
616 638  
  639 + QFile f;
617 640 ~flatGallery()
618 641 {
619   - if (!lines.isEmpty())
620   - QtUtils::writeFile(file.name, lines);
  642 + f.close();
621 643 }
622 644  
623 645 TemplateList readBlock(bool *done)
624 646 {
  647 + *done = false;
  648 + if (f.atEnd())
  649 + f.seek(0);
  650 +
625 651 TemplateList templates;
626   - foreach (const QString &line, QtUtils::readLines(file))
627   - templates.append(File(line));
628   - *done = true;
  652 +
  653 + for (qint64 i = 0; i < readBlockSize; i++)
  654 + {
  655 + QByteArray line = f.readLine();
  656 +
  657 + if (!line.isEmpty())
  658 + templates.append(File(QString(line)));
  659 +
  660 + if (f.atEnd()) {
  661 + *done=true;
  662 + break;
  663 + }
  664 + }
  665 +
629 666 return templates;
630 667 }
631 668  
  669 + void init()
  670 + {
  671 + f.setFileName(file);
  672 + QtUtils::touchDir(f);
  673 + if (!f.open(QFile::ReadWrite))
  674 + qFatal("Failed to open %s for read/write.", qPrintable(file));
  675 +
  676 + }
  677 +
632 678 void write(const Template &t)
633 679 {
634   - lines.append(t.file.flat());
  680 + f.write((t.file.flat()+"\n").toLocal8Bit() );
635 681 }
636 682 };
637 683  
... ...