Commit ccc5d38ced19cd4596a669607d8f544dd74a1f4b
1 parent
ab5ac7a4
Using .vec and .txt galleries for training
Showing
3 changed files
with
19 additions
and
89 deletions
openbr/plugins/gallery/vec.cpp
| ... | ... | @@ -3,6 +3,12 @@ |
| 3 | 3 | namespace br |
| 4 | 4 | { |
| 5 | 5 | |
| 6 | +/*! | |
| 7 | + * \ingroup galleries | |
| 8 | + * \brief Reads/writes OpenCV's .vec format. | |
| 9 | + * \author Scott Klum \cite sklum | |
| 10 | + */ | |
| 11 | + | |
| 6 | 12 | class vecGallery : public Gallery |
| 7 | 13 | { |
| 8 | 14 | Q_OBJECT |
| ... | ... | @@ -56,7 +62,9 @@ class vecGallery : public Gallery |
| 56 | 62 | for (int r = 0; r < height; r++) |
| 57 | 63 | for (int c = 0; c < width; c++) |
| 58 | 64 | m.ptr(r)[c] = (uchar)vec[r*width+c]; |
| 59 | - templates.append(Template(m)); | |
| 65 | + Template t(m); | |
| 66 | + t.file.set("Label",1); | |
| 67 | + templates.append(t); | |
| 60 | 68 | } |
| 61 | 69 | |
| 62 | 70 | return templates; | ... | ... |
openbr/plugins/imgproc/slidingwindow.cpp
| ... | ... | @@ -30,7 +30,6 @@ namespace br |
| 30 | 30 | /*! |
| 31 | 31 | * \ingroup transforms |
| 32 | 32 | * \brief Applies a classifier to a sliding window. |
| 33 | - * Discards negative detections. | |
| 34 | 33 | * \author Jordan Cheney \cite JordanCheney |
| 35 | 34 | */ |
| 36 | 35 | |
| ... | ... | @@ -40,93 +39,12 @@ class SlidingWindowTransform : public Transform |
| 40 | 39 | |
| 41 | 40 | Q_PROPERTY(br::Classifier *classifier READ get_classifier WRITE set_classifier RESET reset_classifier STORED false) |
| 42 | 41 | Q_PROPERTY(QString cascadeDir READ get_cascadeDir WRITE set_cascadeDir RESET reset_cascadeDir STORED false) |
| 43 | - Q_PROPERTY(QString vecFile READ get_vecFile WRITE set_vecFile RESET reset_vecFile STORED false) | |
| 44 | - Q_PROPERTY(QString negFile READ get_negFile WRITE set_negFile RESET reset_negFile STORED false) | |
| 45 | - | |
| 46 | 42 | BR_PROPERTY(br::Classifier *, classifier, NULL) |
| 47 | 43 | BR_PROPERTY(QString, cascadeDir, "") |
| 48 | - BR_PROPERTY(QString, vecFile, "vec.vec") | |
| 49 | - BR_PROPERTY(QString, negFile, "neg.txt") | |
| 50 | - | |
| 51 | - QList<Mat> getPos() | |
| 52 | - { | |
| 53 | - FILE *file = fopen(vecFile.toStdString().c_str(), "rb"); | |
| 54 | - if ( !file ) | |
| 55 | - qFatal("Couldn't open the file"); | |
| 56 | - | |
| 57 | - short* vec = 0; | |
| 58 | - int count, vecSize, last, base; | |
| 59 | - | |
| 60 | - short tmp = 0; | |
| 61 | - if( fread( &count, sizeof( count ), 1, file ) != 1 || | |
| 62 | - fread( &vecSize, sizeof( vecSize ), 1, file ) != 1 || | |
| 63 | - fread( &tmp, sizeof( tmp ), 1, file ) != 1 || | |
| 64 | - fread( &tmp, sizeof( tmp ), 1, file ) != 1 ) | |
| 65 | - CV_Error_( CV_StsParseError, ("wrong file format for %s\n", qPrintable(vecFile)) ); | |
| 66 | - base = sizeof( count ) + sizeof( vecSize ) + 2*sizeof( tmp ); | |
| 67 | - | |
| 68 | - last = 0; | |
| 69 | - vec = (short*) cvAlloc( sizeof( *vec ) * vecSize ); | |
| 70 | - CV_Assert( vec ); | |
| 71 | - | |
| 72 | - QList<Mat> posImages; | |
| 73 | - for (int i = 0; i < 35770; i++) { | |
| 74 | - Mat pos(24, 24, CV_8UC1); | |
| 75 | - | |
| 76 | - CV_Assert( pos.rows * pos.cols == vecSize ); | |
| 77 | - uchar tmp = 0; | |
| 78 | - size_t elements_read = fread( &tmp, sizeof( tmp ), 1, file ); | |
| 79 | - if( elements_read != 1 ) | |
| 80 | - CV_Error( CV_StsBadArg, "Can not get new positive sample. The most possible reason is " | |
| 81 | - "insufficient count of samples in given vec-file.\n"); | |
| 82 | - elements_read = fread( vec, sizeof( vec[0] ), vecSize, file ); | |
| 83 | - if( elements_read != (size_t)(vecSize) ) | |
| 84 | - CV_Error( CV_StsBadArg, "Can not get new positive sample. Seems that vec-file has incorrect structure.\n"); | |
| 85 | - | |
| 86 | - if( feof( file ) || last++ >= count ) | |
| 87 | - CV_Error( CV_StsBadArg, "Can not get new positive sample. vec-file is over.\n"); | |
| 88 | - | |
| 89 | - for( int r = 0; r < pos.rows; r++ ) | |
| 90 | - for( int c = 0; c < pos.cols; c++ ) | |
| 91 | - pos.ptr(r)[c] = (uchar)vec[r * pos.cols + c]; | |
| 92 | - posImages.append(pos); | |
| 93 | - } | |
| 94 | - return posImages; | |
| 95 | - } | |
| 96 | - | |
| 97 | - QList<Mat> getNeg() | |
| 98 | - { | |
| 99 | - QList<Mat> negs; | |
| 100 | - | |
| 101 | - QStringList lines; | |
| 102 | - QtUtils::readFile(negFile,lines); | |
| 103 | 44 | |
| 104 | - foreach(const QString &line, lines) { | |
| 105 | - if (line[0] == QChar('#')) continue; | |
| 106 | - else negs.append(imread(qPrintable(line), CV_LOAD_IMAGE_GRAYSCALE)); | |
| 107 | - } | |
| 108 | - return negs; | |
| 109 | - } | |
| 110 | - | |
| 111 | - void train(const TemplateList &_data) | |
| 45 | + void train(const TemplateList &data) | |
| 112 | 46 | { |
| 113 | - (void)_data; | |
| 114 | - | |
| 115 | - QList<Mat> posImages = getPos(); | |
| 116 | - QList<Mat> negImages = getNeg(); | |
| 117 | - | |
| 118 | - QList<Mat> images; QList<float> labels; | |
| 119 | - foreach (const Mat &pos, posImages) { | |
| 120 | - images.append(pos); | |
| 121 | - labels.append(1.); | |
| 122 | - } | |
| 123 | - | |
| 124 | - foreach (const Mat &neg, negImages) { | |
| 125 | - images.append(neg); | |
| 126 | - labels.append(0.); | |
| 127 | - } | |
| 128 | - | |
| 129 | - classifier->train(images, labels); | |
| 47 | + classifier->train(data.data(), File::get<float>(data, "Label", -1)); | |
| 130 | 48 | } |
| 131 | 49 | |
| 132 | 50 | void project(const Template &src, Template &dst) const | ... | ... |
openbr/plugins/io/read.cpp
| ... | ... | @@ -15,7 +15,7 @@ |
| 15 | 15 | * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */ |
| 16 | 16 | |
| 17 | 17 | #include <opencv2/highgui/highgui.hpp> |
| 18 | - | |
| 18 | +#include <openbr/core/opencvutils.h> | |
| 19 | 19 | #include <openbr/plugins/openbr_internal.h> |
| 20 | 20 | |
| 21 | 21 | using namespace cv; |
| ... | ... | @@ -59,9 +59,13 @@ private: |
| 59 | 59 | else dst.file.fte = true; |
| 60 | 60 | } else { |
| 61 | 61 | foreach (const Mat &m, src) { |
| 62 | - const Mat img = imdecode(m, mode); | |
| 63 | - if (img.data) dst.append(img); | |
| 64 | - else dst.file.fte = true; | |
| 62 | + if (((m.rows > 1) && (m.cols > 1)) || (m.type() != CV_8UC1)) | |
| 63 | + dst += m; | |
| 64 | + else { | |
| 65 | + const Mat img = imdecode(m, mode); | |
| 66 | + if (img.data) dst.append(img); | |
| 67 | + else dst.file.fte = true; | |
| 68 | + } | |
| 65 | 69 | } |
| 66 | 70 | } |
| 67 | 71 | if (dst.file.fte) | ... | ... |