CAVAPA-GUI  30.5.2014
 All Classes Namespaces Functions Variables Typedefs Enumerations Pages
analysiscontroller.h
1 /****************************************************************************
2  * Copyright (c) 2014, Joel Kivelä, Erkki Koskenkorva, Oskari Leppäaho,
3  * Mika Lehtinen and Petri Partanen.
4  * All rights reserved.
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  * * Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the distribution.
14  * * Neither the name of the copyright holders nor the names of its
15  * contributors may be used to endorse or promote products derived from
16  * this software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29 ****************************************************************************/
30 #ifndef ANALYSISCONTROLLER_H
31 #define ANALYSISCONTROLLER_H
32 
33 #include <map>
34 #include <string>
35 #include <vector>
36 
37 #include <QObject>
38 #include <QTimer>
39 
40 #include "common.h"
41 #include "results.h"
42 #include "source/camera.h"
43 
44 namespace cavapa_gui
45 {
70 class AnalysisController : public QObject
71 {
72  Q_OBJECT
73 public:
78 
79  ~AnalysisController() { stop(); }
80 
85 
103  SourceID addSource(const std::string& path, SourceID id = UNDEFINED_SOURCE,
104  FrameTime start_point = 0);
105 
123  bool addSource(const std::vector<std::string>& paths,
124  SourceID id = UNDEFINED_SOURCE, FrameTime start_point = 0);
125 
133  bool canChangeSourceResolution(SourceID id) const;
134 
140  bool canPause() const;
141 
151  int countAvailableCameras() const;
152 
158  bool exportResults(const ExportOptions& options) const
159  { return results.exportToCSV(options); }
160 
169  double getCalculationFPS() const
170  { return running ? Camera::getDefaultFPS() : 0.0; }
171 
180  std::string getLastError() const { return last_error; }
181 
196  std::vector<FrameStats> getResults(FrameTime start, FrameTime stop,
197  int points) const
198  { return results.getStatistics(start, stop, points); }
199 
208  int getSourceCount() const { return sources.size(); }
209 
218  std::string getSourceDescription(SourceID id) const;
219 
226  std::vector<cv::Rect> getSourceExcludes(SourceID id) const;
227 
235  std::vector<std::string> getSourceFilenames(SourceID id) const;
236 
241  std::vector<SourceID> getSourceListing() const;
242 
253 
260  cv::Size getSourceResolution(SourceID id) const;
261 
274 
281 
288 
298  int initCameras(int max_loops = 10);
299 
304  bool isPaused() const { return paused; }
305 
310  bool isRunning() const { return running; }
311 
323  bool load(const std::string& path = "results.cpa");
324 
331  bool pause();
332 
339  bool removeSource(SourceID id);
340 
349  bool reset();
350 
360  bool save(const std::string& path = "results.cpa");
361 
380  bool setCameraFPS(double fps);
381 
390  void setHighlight(bool highlight) { highlight_sightings = highlight; }
391 
399  void setHighlightColor(const cv::Scalar& color)
400  { highlight_color = color; }
401 
413  bool setPosition(FrameTime pos);
414 
428  bool setRecorderCodec(const std::string& codec,
429  const std::string& extension = "avi");
430 
435  void setRecordTimeLimit(unsigned int length) { max_record_length = length; }
436 
452  bool setSourceBarrelCorrection(SourceID id, double amount);
453 
464  void setSourceExcludes(SourceID id, const std::vector<cv::Rect>& rects);
465 
476  bool setSourceResolution(SourceID id, const cv::Size& new_size);
477 
488  bool setVideoSpeed(double ratio = 1.0);
489 
496  void setWorkDirectory(const std::string& path);
497 
503  bool sourceCanRecord(SourceID id) const;
504 
511  bool sourcePlay(SourceID id);
512 
521  bool sourceSeek(SourceID id, FrameTime position);
522 
531  bool sourceStepBackward(SourceID id) { return stepSource(id, -1); }
532 
541  bool sourceStepForward(SourceID id) { return stepSource(id, 1); }
542 
549  bool sourceStop(SourceID id);
550 
563  bool start(const std::vector<SourceID>& calc_sources,
564  const std::vector<cavapa::CameraSettings>& settings,
565  const std::vector<cavapa::CalibrationPoint>& points,
566  bool record = false,
567  int calc_time = 0);
568 
573  void stop();
574 
575 private:
586  bool checkDiskSpace();
587 
596  void checkLag(FrameTime time_passed);
597 
602  void checkRecordLimits();
603 
612  inline std::string createRecordName(SourceID id) const;
613 
620  cv::Mat drawExcludes(SourceID id, const cv::Mat& image);
621 
627  static unsigned int getFreeDiskSpace(const std::string& path);
628 
636  inline std::shared_ptr<Source> getSource(SourceID id) const;
637 
644  SourceID getSourceDevice(int device) const;
645 
653  std::vector<FrameCapture> highlight(
654  const std::vector<FrameCapture>& captures,
655  const std::vector<cavapa::Sightings>& sightings) const;
656 
663  inline cv::Mat highlightFrame(const cv::Mat& image,
664  const std::vector<cavapa::Sighting>& sightings) const;
665 
671  static inline bool isVideoFile(const std::shared_ptr<Source>& src);
672 
677  inline void playSelectedSources();
678 
685  static cv::Mat printText(const cv::Mat& image, const std::string& text);
686 
691  inline bool selectedAreVideos() const;
692 
697  inline bool selectedSourcesOpen() const;
698 
709  std::vector<cavapa::CameraSettings> setCalculationSettings(
710  const std::vector<SourceID>& calc_sources,
711  const std::vector<cavapa::CameraSettings>& settings);
712 
719  void setLastError(const std::string& desc) { last_error = desc; }
720 
726  inline bool sourceExists(SourceID id) const;
727 
733  inline bool sourceInSelection(SourceID id) const;
734 
742  bool stepSource(SourceID id, int amount);
743 
747  inline void stopSelectedSources();
748 
753  static inline FrameTime timeNow();
754 
755  // How many concurrent lag misses we need to trigger a warning.
756  static const unsigned int LAG_WARNING_CONCURRENT = 5;
757 
758  // Interval for lag warnings in seconds.
759  static const unsigned int LAG_WARNING_INTERVAL = 5;
760 
761  // Maximum possible fps that can be set.
762  static const unsigned int MAXIMUM_FPS = 50.0;
763 
764  FrameTime calculation_time; // Calculation time
765  FrameTime calculation_stop; // Holds automatic stop time
766  std::unique_ptr<cavapa::CavapaDetector> detector = nullptr; // Detector
767  std::map<SourceID, std::vector<cv::Rect>> exclude_rectangles;
768  std::string folder = ""; // Work folder
769  cv::Scalar highlight_color = cv::Scalar(0, 0, 255); // Coloring
770  bool highlight_sightings = false; // Sighting highlighting
771  FrameTime lag_warning_last = 0; // Holds last lag time warning
772  unsigned int lag_warning_concurrent = 0;// Concurrent lag warnings
773  std::string last_error = ""; // Last occurred error
774  FrameTime last_space_check = 0; // Last disk space checktime
775  FrameTime last_update_time = 0; // Holds last source retrieve time
776  unsigned int max_record_length = 0; // Maximum seconds per video file
777  bool paused = true; // Calculation pause status.
778  Results results; // Calculation results
779  bool recording = false; // If calculation is recording
780  bool running = false; // If calculation is in progress
781  std::vector<SourceID> selected_sources; // Sources in calculation
782  std::vector<std::shared_ptr<Source>> sources; // All video sources
783  double speed_ratio = 1.0; // Source framerate ratio
784  QTimer timer; // Source frame update timer
785  std::string video_extension = "avi"; // Default extension for video files
786 
787 #ifdef _DEBUG_TIMERS
788  int processed_frames = 0;
789  std::uint64_t total_process_time = 0;
790 #endif // _DEBUG_TIMERS
791 
792 private slots:
798  void updateSources();
799 
800 signals:
810  void calculationComplete();
811 
822  void error(const std::string& message, ErrorLevel level);
823 
835  void updated(const std::vector<FrameCapture>& frames,
836  const FrameStats& statistics);
837 };
838 } // namespace
839 
840 #endif // ANALYSISCONTROLLER_H
bool canPause() const
Indicates if the running calculation can be paused.
Definition: analysiscontroller.cpp:201
bool isPaused() const
Retrieves the current pause status.
Definition: analysiscontroller.h:304
SourceType getSourceType(SourceID id) const
Returns the type of the selected source.
Definition: analysiscontroller.cpp:435
bool removeSource(SourceID id)
Removes the source.
Definition: analysiscontroller.cpp:604
std::vector< FrameStats > getStatistics(FrameTime start, FrameTime stop, int points) const
Returns the calculation statistics from the given period.
Definition: results.cpp:334
bool sourceStepForward(SourceID id)
Steps the source one frame forward.
Definition: analysiscontroller.h:541
FrameTime getVideoLength(SourceID id) const
Retrieves the length of the video source.
Definition: analysiscontroller.cpp:441
bool exportToCSV(const ExportOptions &options) const
Exports the results to the CSV file.
Definition: results.cpp:124
unsigned int SourceID
Used to indicate unique source ID-numbers.
Definition: common.h:229
const SourceID UNDEFINED_SOURCE
Used to indicate unknown sources.
Definition: common.h:234
bool isRunning() const
Retrieves the current status of the calculation.
Definition: analysiscontroller.h:310
void setWorkDirectory(const std::string &path)
Sets the path for the calculation directory.
Definition: analysiscontroller.cpp:857
void setRecordTimeLimit(unsigned int length)
Sets the maximum length of one recording.
Definition: analysiscontroller.h:435
void setHighlightColor(const cv::Scalar &color)
Sets the sighting's highlighting color.
Definition: analysiscontroller.h:399
bool sourceStepBackward(SourceID id)
Steps the source one frame back.
Definition: analysiscontroller.h:531
int countAvailableCameras() const
Counts the available connected hardware cameras.
Definition: analysiscontroller.cpp:280
bool canChangeSourceResolution(SourceID id) const
Checks if the source resolution can be changed.
Definition: analysiscontroller.cpp:194
void setSourceExcludes(SourceID id, const std::vector< cv::Rect > &rects)
Sets the excluded rectangles for a source.
bool setCameraFPS(double fps)
Sets the default FPS for camera sources.
Definition: analysiscontroller.cpp:760
bool save(const std::string &path="results.cpa")
Saves the calculation results and sightings.
Definition: analysiscontroller.cpp:659
bool sourceStop(SourceID id)
Stops playing the video source.
Definition: analysiscontroller.cpp:950
The structure to hold statistics about the source performance.
Definition: common.h:327
void error(const std::string &message, ErrorLevel level)
Signals that something went wrong during the calculation.
bool setVideoSpeed(double ratio=1.0)
Sets the video source speed.
Definition: analysiscontroller.cpp:846
bool load(const std::string &path="results.cpa")
Loads the calculation results and recorded file information.
Definition: analysiscontroller.cpp:549
std::vector< FrameStats > getResults(FrameTime start, FrameTime stop, int points) const
Returns calculation results from a given period.
Definition: analysiscontroller.h:196
std::vector< std::string > getSourceFilenames(SourceID id) const
Returns the list of the files associated with the source.
Definition: analysiscontroller.cpp:388
bool sourcePlay(SourceID id)
Starts playing a video source.
Definition: analysiscontroller.cpp:902
cv::Size getSourceResolution(SourceID id) const
Retrieves the source's frame resolution.
Definition: analysiscontroller.cpp:423
bool setSourceBarrelCorrection(SourceID id, double amount)
Sets the barrel correction on a source.
Definition: analysiscontroller.cpp:813
Base class of the CAVAPA calculations.
Definition: analysiscontroller.h:70
bool pause()
Pauses or unpauses the calculation.
Definition: analysiscontroller.cpp:559
SourceID addSource(const std::string &path, SourceID id=UNDEFINED_SOURCE, FrameTime start_point=0)
Adds a custom video source.
Definition: analysiscontroller.cpp:98
int getSourceCount() const
Retrieves the total count of sources.
Definition: analysiscontroller.h:208
int initCameras(int max_loops=10)
Initializes the hardware cameras.
Definition: analysiscontroller.cpp:509
SourceType
Available source types are the following ones: CAMERA = hardware or network camera, NOTHING = not a working source, STREAM = network stream, VIDEO = video file and VIDEOSET = set of multiple files.
Definition: common.h:68
DISALLOW_COPY_AND_ASSIGN(AnalysisController)
Copy and assign of the class is not allowed.
bool setSourceResolution(SourceID id, const cv::Size &new_size)
Sets the source resolution.
Definition: analysiscontroller.cpp:829
SourceStats getSourceStats(SourceID id) const
Retrieves the source's current statistics.
Definition: analysiscontroller.cpp:429
void calculationComplete()
Signals that calculation has ended.
bool reset()
Resets the calculation results.
Definition: analysiscontroller.cpp:628
void updated(const std::vector< FrameCapture > &frames, const FrameStats &statistics)
It signals frame updates or calculation updates.
std::uint64_t FrameTime
Used to store milliseconds interval in frame times.
Definition: common.h:138
void setHighlight(bool highlight)
Sets the individual sighting rectangle highlights.
Definition: analysiscontroller.h:390
std::vector< cv::Rect > getSourceExcludes(SourceID id) const
Returns the currently excluded rectangles from the source.
The structure holds CSV export options.
Definition: common.h:183
std::vector< SourceID > getSourceListing() const
Returns a list of all sources in the class.
Definition: analysiscontroller.cpp:405
bool exportResults(const ExportOptions &options) const
Exports the current Results to the CSV File.
Definition: analysiscontroller.h:158
FrameTime getSourcePosition(SourceID id) const
Retrieves the current time position of a source.
Definition: analysiscontroller.cpp:417
bool start(const std::vector< SourceID > &calc_sources, const std::vector< cavapa::CameraSettings > &settings, const std::vector< cavapa::CalibrationPoint > &points, bool record=false, int calc_time=0)
Starts the CAVAPA calculation.
Definition: analysiscontroller.cpp:971
double getCalculationFPS() const
Retrieves the calculation framerate.
Definition: analysiscontroller.h:169
AnalysisController()
Creates the CAVAPA object.
Definition: analysiscontroller.cpp:87
void stop()
Stops the current CAVAPA calculation.
Definition: analysiscontroller.cpp:1101
bool setPosition(FrameTime pos)
Sets the current time position of all the sources.
Definition: analysiscontroller.cpp:781
std::string getLastError() const
Retrieves the last error that occured.
Definition: analysiscontroller.h:180
ErrorLevel
Error level indicator.
Definition: common.h:77
std::string getSourceDescription(SourceID id) const
Returns the description of the source.
Definition: analysiscontroller.cpp:366
bool sourceSeek(SourceID id, FrameTime position)
Seeks the video source to a specific time.
Definition: analysiscontroller.cpp:924
static double getDefaultFPS()
Retrieves the default framerate that has been set.
Definition: camera.h:114
bool setRecorderCodec(const std::string &codec, const std::string &extension="avi")
Sets the default camera recorder codec.
Definition: analysiscontroller.cpp:806
bool sourceCanRecord(SourceID id) const
Checks if the source is capable of recording.
Definition: analysiscontroller.cpp:875