1+ #include < iostream>
2+ #include < fstream>
3+ #include " nlohmann/json.hpp"
14#include " manager.h"
25#include " alert.h"
36
47#define ESC 27
58#define NUM_OF_TRACKING 10
69
10+ using json=nlohmann::json;
711using namespace std ;
812using namespace cv ;
913
@@ -44,40 +48,50 @@ void Manager::init()
4448
4549void Manager::mainDemo ()
4650{
47- string filePath = " ../data.txt" ;
48- // open the file
49- ifstream file (filePath);
51+ string path;
52+ int focalLength;
53+ bool isTravel;
54+ // Open the JSON file
55+ std::ifstream file (" ../data.json" );
56+
5057 if (!file.is_open ()) {
51- LogManager::logErrorMessage (ErrorType::FILE_ERROR);
58+ LogManager::logErrorMessage (ErrorType::FILE_ERROR,
59+ " Failed to open the file" );
60+ throw runtime_error (" Failed to open the file" );
5261 return ;
5362 }
54- string line;
55- // run over the file and read the lines
56- while (getline (file, line)) {
57- // intialize the iteration cnt
58- iterationCnt = 1 ;
59- istringstream iss (line);
60- string videoPath;
61- double focalLength;
62- // read the parameters
63- if (getline (iss, videoPath, ' |' ) && iss >> focalLength) {
64- // Trim leading and trailing whitespaces from videoPath
65- videoPath.erase (0 , videoPath.find_first_not_of (" \t\n\r\f\v " ));
66- videoPath.erase (videoPath.find_last_not_of (" \t\n\r\f\v " ) + 1 );
67- }
68- else {
69- LogManager::logErrorMessage (ErrorType::VIDEO_ERROR);
70- return ;
63+
64+ // Read the content of the file into a JSON object
65+ json jsonData;
66+ file >> jsonData;
67+
68+ // Check if the JSON data is an array
69+ if (jsonData.is_array ()) {
70+ // Iterate over each object in the array
71+ for (const auto &obj : jsonData) {
72+ iterationCnt = 1 ;
73+ if (obj.find (" path" ) != obj.end () && obj[" path" ].is_string ()) {
74+ path = obj[" path" ];
75+ }
76+
77+ if (obj.find (" focal_length" ) != obj.end () &&
78+ obj[" focal_length" ].is_number_integer ()) {
79+ focalLength = obj[" focal_length" ];
80+ }
81+
82+ if (obj.find (" is_travel" ) != obj.end () &&
83+ obj[" is_travel" ].is_boolean ()) {
84+ isTravel = obj[" is_travel" ];
85+ }
86+ // Get the distance instance and set the focal length
87+ Distance &distance = Distance::getInstance ();
88+ distance.setFocalLength (focalLength);
89+ runOnVideo (path, isTravel);
7190 }
72- // intialize focal length
73- Distance &distance = Distance::getInstance ();
74- distance.setFocalLength (focalLength);
75- runOnVideo (videoPath);
7691 }
77- cout << " finish reading data" ;
7892}
7993
80- void Manager::runOnVideo (string videoPath)
94+ void Manager::runOnVideo (string videoPath, bool isTravel )
8195{
8296 // Convert Windows file path to WSL file path format
8397 if (videoPath.length () >= 3 && videoPath[1 ] == ' :' ) {
@@ -95,13 +109,14 @@ void Manager::runOnVideo(string videoPath)
95109 throw runtime_error (" video not found" );
96110 return ;
97111 }
112+ // run on video
98113 while (1 ) {
99114 capture >> frame;
100115 if (frame.empty ()) {
101116 LogManager::logInfoMessage (InfoType::MEDIA_FINISH);
102117 break ;
103118 }
104- int result = processing (frame, true );
119+ int result = processing (frame, isTravel );
105120 if (result == -1 )
106121 return ;
107122 }
@@ -161,19 +176,19 @@ int Manager::processing(const Mat &newFrame, bool isTravel)
161176 iterationCnt = iterationCnt % NUM_OF_TRACKING + 1 ;
162177 }
163178
164- #ifdef LANE_DETECT
179+ #ifdef LANE_DETECT
165180 laneDetector.manageLaneDetector (this ->currentFrame );
166- #endif
167- // visual
168- #ifdef SHOW_FRAMES
169- drawOutput ();
170- imshow (" currentFrame" , *currentFrame);
171- int key = cv::waitKey (1 );
172- if (key == ESC) {
173- return -1 ;
174- }
175- return 1 ;
176- #endif
181+ #endif
182+ // visual
183+ #ifdef SHOW_FRAMES
184+ drawOutput ();
185+ imshow (" currentFrame" , *currentFrame);
186+ int key = cv::waitKey (1 );
187+ if (key == ESC) {
188+ return -1 ;
189+ }
190+ return 1 ;
191+ #endif
177192}
178193
179194void Manager::drawOutput ()
@@ -244,9 +259,9 @@ void Manager::drawOutput()
244259 putText (*currentFrame, " velocity" , Point (legendX + 15 , legendY + 50 ),
245260 FONT_HERSHEY_SIMPLEX, 0.6 , Scalar (255 , 255 , 255 ), 1 );
246261
247- #ifdef LANE_DETECT
262+ #ifdef LANE_DETECT
248263 laneDetector.drawLanesOnImage (currentFrame);
249- #endif
264+ #endif
250265}
251266
252267void Manager::sendAlerts (vector<vector<uint8_t >> &alerts)
0 commit comments