Skip to content

Commit 8efc50f

Browse files
Img_processing: Add optional travel status
1 parent 1abbbb7 commit 8efc50f

File tree

3 files changed

+71
-43
lines changed

3 files changed

+71
-43
lines changed

img_processing/data.json

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[
2+
{
3+
"path": "C:/Users/user1/Desktop/day/sun/sun_in_street.mov",
4+
"focal_length": 1500,
5+
"is_travel": true
6+
},
7+
{
8+
"path": "C:/Users/user1/Desktop/day/close_cars/road.mov",
9+
"focal_length": 2000,
10+
"is_travel": true
11+
}
12+
]

img_processing/include/manager.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
#include "log_manager.h"
1111
#include "velocity.h"
1212
#include "communication.h"
13+
1314
class Manager {
1415
public:
1516
static logger imgLogger;
@@ -45,7 +46,7 @@ class Manager {
4546
bool isResetTracker(bool isTravel);
4647
bool isTrack(bool isTravel);
4748
void sendAlerts(std::vector<std::vector<uint8_t>> &alerts);
48-
void runOnVideo(std::string videoPath);
49+
void runOnVideo(std::string videoPath, bool isTravel);
4950
int readIdFromJson(const char *target);
5051
};
5152
#endif //__MANAGER_H__

img_processing/src/manager.cpp

Lines changed: 57 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
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;
711
using namespace std;
812
using namespace cv;
913

@@ -44,40 +48,50 @@ void Manager::init()
4448

4549
void 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

179194
void 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

252267
void Manager::sendAlerts(vector<vector<uint8_t>> &alerts)

0 commit comments

Comments
 (0)