Commit cff1fb9fe7d5711c8e4f922b410e88459529d01b

Authored by Josh Klontz
2 parents b2a21413 3e8e9bbc

Merge pull request #307 from biometrics/stopwatch_refactor

rewrote StopWatchTransform
Showing 1 changed file with 52 additions and 30 deletions
openbr/plugins/time.cpp
@@ -7,56 +7,78 @@ namespace br @@ -7,56 +7,78 @@ namespace br
7 * \ingroup transforms 7 * \ingroup transforms
8 * \brief Gives time elapsed over a specified transform as a function of both images (or frames) and pixels. 8 * \brief Gives time elapsed over a specified transform as a function of both images (or frames) and pixels.
9 * \author Jordan Cheney \cite JordanCheney 9 * \author Jordan Cheney \cite JordanCheney
  10 + * \author Josh Klontz \cite jklontz
10 */ 11 */
11 -class StopWatchTransform : public TimeVaryingTransform 12 +class StopWatchTransform : public MetaTransform
12 { 13 {
13 Q_OBJECT 14 Q_OBJECT
  15 + Q_PROPERTY(QString description READ get_description WRITE set_description RESET reset_description)
  16 + BR_PROPERTY(QString, description, "Identity")
14 17
15 - Q_PROPERTY(br::Transform* child READ get_child WRITE set_child RESET reset_child)  
16 - BR_PROPERTY(br::Transform*, child, NULL)  
17 -  
18 - long timeElapsed;  
19 - long numImgs;  
20 - long numPixels; 18 + br::Transform *transform;
  19 + mutable QMutex mutex;
  20 + mutable long miliseconds;
  21 + mutable long images;
  22 + mutable long pixels;
21 23
22 public: 24 public:
23 - StopWatchTransform() : TimeVaryingTransform(false, false) 25 + StopWatchTransform()
24 { 26 {
25 - timeElapsed = 0;  
26 - numImgs = 0;  
27 - numPixels = 0; 27 + reset();
28 } 28 }
29 29
30 private: 30 private:
31 - void projectUpdate(const Template &src, Template &dst) 31 + void reset()
32 { 32 {
33 - QTime watch;  
34 -  
35 - if (child == NULL)  
36 - qFatal("Can't find child transform! Command line syntax is StopWatch(name of transform to profile)"); 33 + miliseconds = 0;
  34 + images = 0;
  35 + pixels = 0;
  36 + }
37 37
38 - watch.start(); 38 + void init()
  39 + {
  40 + transform = Transform::make(description);
  41 + }
39 42
40 - child->project(src, dst); 43 + void train(const QList<TemplateList> &data)
  44 + {
  45 + transform->train(data);
  46 + }
41 47
42 - int time = watch.elapsed(); 48 + void project(const Template &src, Template &dst) const
  49 + {
  50 + QTime watch;
  51 + watch.start();
  52 + transform->project(src, dst);
43 53
44 - timeElapsed += time;  
45 - numImgs++;  
46 - numPixels += (src.m().rows * src.m().cols); 54 + QMutexLocker locker(&mutex);
  55 + miliseconds += watch.elapsed();
  56 + images++;
  57 + foreach (const cv::Mat &m, src)
  58 + pixels += (m.rows * m.cols);
47 } 59 }
48 60
49 - void finalize(TemplateList &output) 61 + void finalize(TemplateList &)
50 { 62 {
51 - (void)output; 63 + qDebug("\nProfile for \"%s\"\n"
  64 + "\tSeconds: %g\n"
  65 + "\tTemplates/s: %g\n"
  66 + "\tPixels/s: %g\n",
  67 + qPrintable(description),
  68 + miliseconds / 1000.0,
  69 + images * 1000.0 / miliseconds,
  70 + pixels * 1000.0 / miliseconds);
  71 + reset();
  72 + }
52 73
53 - qDebug() << "\n\nProfiled " << numImgs << " images:\n" <<  
54 - "\tavg time per image: " << (double)timeElapsed / numImgs << " ms\n" <<  
55 - "\tavg time per pixel: " << (double)timeElapsed / numPixels << " ms\n"; 74 + void store(QDataStream &stream) const
  75 + {
  76 + transform->store(stream);
  77 + }
56 78
57 - timeElapsed = 0;  
58 - numImgs = 0;  
59 - numPixels = 0; 79 + void load(QDataStream &stream)
  80 + {
  81 + transform->load(stream);
60 } 82 }
61 }; 83 };
62 84