-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathobject_detection_yolo.cpp
More file actions
1362 lines (1035 loc) · 49.1 KB
/
object_detection_yolo.cpp
File metadata and controls
1362 lines (1035 loc) · 49.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// This code is written at BigVision LLC. It is based on the OpenCV project. It is subject to the license terms in the LICENSE file found in this distribution and at http://opencv.org/license.html
// Usage example: ./object_detection_yolo.out --video=run.mp4
// ./object_detection_yolo.out --image=bird.jpg
#include <fstream>
#include <sstream>
#include <iostream>
#include <opencv2/core/cuda.hpp>
#include <opencv2/cudawarping.hpp>
#include <opencv2/cudaarithm.hpp>
#include <opencv2/cudaimgproc.hpp>
// solvePnP is listed in this header file!
#include <opencv2/calib3d/calib3d.hpp>
#include <opencv2/dnn.hpp>
#include <opencv2/imgproc.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/xfeatures2d.hpp>
#include <opencv2/features2d.hpp>
#include <opencv2/xfeatures2d/nonfree.hpp>
#include <opencv2/core.hpp>
#include "ConeROI.hpp"
#include "ConeEst.hpp"
#include "dataStructures.h"
#include "matching2D.hpp"
// add custom keys if you want to use the parser to your liking
const char* keys =
"{help h usage ? | | Usage examples: \n\t\t./object_detection_yolo.out --image=dog.jpg \n\t\t./object_detection_yolo.out --video=run_sm.mp4}"
"{left_image il |<none>| input image }"
"{right_image ir |<none>| input image }"
"{left_vid v |<none>| input video }"
"{device d |<cpu>| input device }"
"{is_left l |<none>| input int }"
"{right_vid rv |<none>| input video }"
;
using namespace cv;
using namespace dnn;
using namespace std;
using namespace cv::xfeatures2d;
/**
* \brief Compute and draw the epipolar lines in two images
* associated to each other by a fundamental matrix
*
* \param title Title of the window to display
* \param F Fundamental matrix
* \param img1 First image
* \param img2 Second image
* \param points1 Set of points in the first image
* \param points2 Set of points in the second image matching to the first set
* \param inlierDistance Points with a high distance to the epipolar lines are
* not displayed. If it is negative, all points are displayed
**/
// Initialize the parameters
float confThreshold = 0.5; // Confidence threshold
float nmsThreshold = 0.4; // Non-maximum suppression threshold
int inpWidth = 416; // Width of network's input image
int inpHeight = 416; // Height of network's input image
vector<string> classes;
vector<string> classes_right;
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& outs, Mat& frame_right,
const vector<Mat>& outs_right, int is_left, Mat cameraMatrix, Mat distCoeffs);
void print_num_cones(vector<int> classIds, string image);
void postprocess_yolo_right_img(Mat& frame, const vector<Mat>& outs, Mat& frame_right,
const vector<Mat>& outs_right, int is_left, Mat cameraMatrix, Mat distCoeffs);
void draw_bounding_boxes(vector<int> indices, vector<Rect> boxes, vector<int> classIds, vector<float> confidences, Mat frame);
void compute_bounding_box(const vector<Mat>& outs, Mat& frame, vector<int>& classIds, vector<float>& confidences, vector<Rect>& boxes, vector<int>& centerX, vector<int>& centerY);
void keypoint_detection(vector<int>& indices, vector<Rect>& boxes, Mat& frame, vector<cv::KeyPoint>& final_keypoints, vector<int>& centerX, vector<int>& centerY);
// void average_keypoint(vector<KeyPoint>& input, vector <Keypoint>& output);
// template <typename T1, typename T2>
// static void drawEpipolarLines(const std::string& title, const cv::Matx<T1,3,3> F,
// const cv::Mat& img1, const cv::Mat& img2,
// const std::vector<cv::Point_<T2>> points1,
// const std::vector<cv::Point_<T2>> points2,
// const float inlierDistance = -1);
// template <typename T>
// static float distancePointLine(const cv::Point_<T> point, const cv::Vec<T,3>& line);
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame, float zEst, int show_coordinates); // float xEst, float yEst);
void print_inference_time(Net net, Mat& frame, string outputFile);
void show_frame(Mat& frame, string frame_name);
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net);
int main(int argc, char** argv)
{
const std::string& calibrationFile = "left_calibration.xml";
cv::FileStorage fs;
cv::Size calibSize;
cv::Mat cameraMatrix;
cv::Mat distCoeffs;
fs.open(calibrationFile, cv::FileStorage::READ);
fs["cameraMatrix"] >> cameraMatrix;
fs["distCoeffs"] >> distCoeffs;
fs["calibImageSize"] >> calibSize;
float imgCenter_x = cameraMatrix.at<double>(0, 2);
float imgCenter_y = cameraMatrix.at<double>(1, 2);
float focal_px_x = cameraMatrix.at<double>(0, 0);
float focal_px_y = cameraMatrix.at<double>(1, 1);
fs.release();
CommandLineParser parser(argc, argv, keys);
parser.about("Use this script to run object detection using YOLO3 in OpenCV.");
if (parser.has("help"))
{
parser.printMessage();
return 0;
}
string classesFile = "cones.names"; // use the list of class names for our cones i.e., blue, orange, and yellow
ifstream ifs(classesFile.c_str());
string line;
while (getline(ifs, line)) classes.push_back(line);
string device = "cpu";
device = parser.get<String>("device");
String modelConfiguration = "yolov4-tiny.cfg";
String modelWeights = "yolov4-tiny-best.weights";
int left_cam_only = 0;
// Load the network
Net net = readNetFromDarknet(modelConfiguration, modelWeights);
Net net_right = readNetFromDarknet(modelConfiguration, modelWeights);
if (device == "cpu")
{
cout << "Using CPU device" << endl;
net.setPreferableBackend(DNN_TARGET_CPU);
}
// Open a video file or an image file or a camera stream.
string str, outputFile;
VideoCapture cap;
VideoWriter video;
Mat frame, blob;
int FPS = 10;
string str_right, outputFile_right;
VideoCapture cap_right;
VideoWriter video_right;
Mat frame_right, blob_right;
int is_left = 0;
try {
outputFile = "yolo_out_cpp.avi";
if (parser.has("left_image"))
{
// Open the image file
str = parser.get<String>("left_image");
ifstream ifile(str);
if (!ifile) throw("error");
// cap.open(str);
frame = imread(str);
str.replace(str.end()-4, str.end(), "_yolo_out_cpp.jpg");
outputFile = str;
}
if (parser.has("right_image"))
{
// Open the image file
str_right = parser.get<String>("right_image");
ifstream ifile(str_right);
if (!ifile) throw("error");
// cap_right.open(str_right);
frame_right = imread(str_right);
str_right.replace(str_right.end()-4, str_right.end(), "_yolo_out_cpp.jpg");
outputFile_right = str_right;
}
if (parser.has("left_vid"))
{
// Open the video file (left camera)
str = parser.get<String>("left_vid");
ifstream ifile(str);
if (!ifile) throw("error");
cap.open(str);
str.replace(str.end()-4, str.end(), "_yolo_out_cpp.avi");
outputFile = str;
}
if (parser.has("right_vid")) {
// right camera
if (left_cam_only == 0) {
str_right = parser.get<String>("right_vid");
ifstream ifile_right(str_right);
if (!ifile_right) throw("error");
cap_right.open(str_right);
str_right.replace(str_right.end()-4, str_right.end(), "_yolo_out_cpp.avi");
outputFile_right = str_right;
}
}
// Open the webcam
else cap.open(parser.get<int>("device"));
}
catch(...) {
cout << "Could not open the input image/video stream" << endl;
return 0;
}
// Get the video writer initialized to save the output video
if (parser.has("left_vid")) {
video.open(outputFile, VideoWriter::fourcc('M','J','P','G'), FPS, Size(cap.get(CAP_PROP_FRAME_WIDTH), cap.get(CAP_PROP_FRAME_HEIGHT)));
if (left_cam_only == 0) {
video_right.open(outputFile_right, VideoWriter::fourcc('M','J','P','G'), FPS, Size(cap_right.get(CAP_PROP_FRAME_WIDTH), cap_right.get(CAP_PROP_FRAME_HEIGHT)));
}
}
// process the images and videos
if (parser.has("left_vid")) {
// Create a window
static const string kWinName = "Deep learning object detection in OpenCV";
namedWindow(kWinName, WINDOW_NORMAL);
// Process frames.
while (waitKey(1) < 0)
{
// get frame from the video
cap >> frame;
cap_right >> frame_right;
// Stop the program if reached end of video
if (frame.empty()) {
cout << "Done processing !!!" << endl;
cout << "Output file is stored as " << outputFile << endl;
waitKey(3000);
break;
}
// Runs the forward pass to get output of the output layers
vector<Mat> outs;
vector<Mat> outs_right;
// Create a 4D blob from a frame.
blobFromImage(frame, blob, 1/255.0, cv::Size(inpWidth, inpHeight), Scalar(0,0,0), true, false);
net.setInput(blob); // anything to do with "net" means it's dealing with darknet
net.forward(outs, getOutputsNames(net));
blobFromImage(frame_right, blob_right, 1/255.0, cv::Size(inpWidth, inpHeight), Scalar(0,0,0), true, false);
net_right.setInput(blob_right);
net_right.forward(outs_right, getOutputsNames(net_right));
// postprocess(frame, outs, frame_right, outs_right, is_left, cameraMatrix, distCoeffs);
postprocess_yolo_right_img(frame, outs, frame_right, outs_right, is_left, cameraMatrix, distCoeffs);
print_inference_time(net, frame, outputFile);
print_inference_time(net_right, frame_right, outputFile_right);
// Write the frame with the detection boxes
Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U);
if (parser.has("left_image")) imwrite(outputFile, detectedFrame);
else video.write(detectedFrame);
Mat detectedFrame_right;
frame_right.convertTo(detectedFrame_right, CV_8U);
// imshow("right image", detectedFrame_right);
// waitKey(0);
video_right.write(detectedFrame_right);
imshow(kWinName, detectedFrame);
}
}
// supplied images!
else {
cv::String left_path("../../../../FPS_10_stereo_left/*.png");
cv::String right_path("../../../../FPS_10_stereo_right/*.png");
vector<cv::String> fn;
vector<cv::String> fn_right;
// vector<cv::Mat> data;
cv::glob(left_path,fn,true); // recurse
cv::glob(right_path, fn_right, true);
for (size_t k=0; k<fn.size(); ++k)
{
cv::Mat im = cv::imread(fn[k]);
cv::Mat im_right = cv::imread(fn_right[k]);
if (im.empty()) continue; //only proceed if sucsessful
// you probably want to do some preprocessing
frame = im;
outputFile = fn[k];
frame_right = im_right;
outputFile_right = fn_right[k];
// Runs the forward pass to get output of the output layers
vector<Mat> outs;
vector<Mat> outs_right;
// Create a 4D blob from a frame.
blobFromImage(frame, blob, 1/255.0, cv::Size(inpWidth, inpHeight), Scalar(0,0,0), true, false);
net.setInput(blob);
net.forward(outs, getOutputsNames(net));
blobFromImage(frame_right, blob_right, 1/255.0, cv::Size(inpWidth, inpHeight), Scalar(0,0,0), true, false);
net_right.setInput(blob_right);
net_right.forward(outs_right, getOutputsNames(net_right));
// postprocess(frame, outs, frame_right, outs_right, is_left, cameraMatrix, distCoeffs);
postprocess_yolo_right_img(frame, outs, frame_right, outs_right, is_left, cameraMatrix, distCoeffs);
print_inference_time(net, frame, outputFile);
print_inference_time(net_right, frame_right, outputFile_right);
// show_frame(frame, "left_image");
// show_frame(frame_right, "right_image");
}
}
cap.release();
cap_right.release();
if (parser.has("left_vid")) {
video.release();
video_right.release();
}
return 0;
}
void show_frame(Mat& frame, string frame_name) {
// Write the frame with the detection boxes
Mat detectedFrame;
frame.convertTo(detectedFrame, CV_8U);
imshow(frame_name, detectedFrame);
waitKey(0);
}
void print_inference_time(Net net, Mat& frame, string outputFile) {
// Put efficiency information. The function getPerfProfile returns the overall time for inference(t) and the timings for each of the layers(in layersTimes)
vector<double> layersTimes;
double freq = getTickFrequency() / 1000;
double t = net.getPerfProfile(layersTimes) / freq;
string label = format("Inference time for a frame : %.2f ms", t);
putText(frame, label, Point(0, 15), FONT_HERSHEY_SIMPLEX, 0.5, Scalar(0, 0, 255));
cout << "Frame: " << outputFile << ", Inference Time: " << t << endl;
}
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess(Mat& frame, const vector<Mat>& outs, Mat& frame_right,
const vector<Mat>& outs_right, int is_left, Mat cameraMatrix, Mat distCoeffs)
{
vector<int> classIds;
vector<float> confidences;
vector<Rect> boxes;
int centerX;
int centerY;
int width;
int height;
// cout << outs.size() << endl;
for (size_t i = 0; i < outs.size(); ++i)
{
// Scan through all the bounding boxes output from the network and keep only the
// ones with high confidence scores. Assign the box's class label as the class
// with the highest score for the box.
float* data = (float*)outs[i].data;
for (int j = 0; j < outs[i].rows; ++j, data += outs[i].cols)
{
Mat scores = outs[i].row(j).colRange(5, outs[i].cols);
Point classIdPoint;
double confidence;
// Get the value and location of the maximum score
minMaxLoc(scores, 0, &confidence, 0, &classIdPoint);
if (confidence > confThreshold)
{
// manually resized the bounding box to encapsulate the entire cone
// tried training the object detection algorithm but didn't see an improvement in performance.
centerX = (int)(data[0] * frame.cols);
centerY = (int)(data[1] * frame.rows);
width = (int)(data[2] * frame.cols)/2 - 20;
height = (int)(data[3] * frame.rows) + 95;
int left = centerX - width / 2 - 10;
int top = centerY - height / 2 - 15;
// cout << "width is: " << width << endl;
// cout << "height is: " << height << endl;
// cout << "centerX is: " << centerX << endl;
// cout << "centerY is: " << centerY << endl;
classIds.push_back(classIdPoint.x);
confidences.push_back((float)confidence);
boxes.push_back(Rect(left, top, width, height));
}
}
}
// Perform non maximum suppression to eliminate redundant overlapping boxes with
// lower confidences
vector<int> indices;
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
// added by Kelvin
double baseline = 550; // 550mm
double f2 = 1499.093; // focal length of right camera = 5mm (in the x axis)
double f1 = 2724.847; // focal length of left camera in the x axis
double lImgCenter_x = 548.607;
double lImgCenter_y = 845.6168;
double rImgCenter_x = 921.35846;
double rImgCenter_y = 616.6407;
// cout << "after rImgCenter" << endl;
std::vector<std::vector<cv::Point3f>> conePointsVec;
// define 7 points that shape a cone
std::vector<cv::Point3f> conePoints;
conePoints.push_back(cv::Point3f(0, 0, 0));
for (int i = 1; i <= 3; i++) {
float x = -77.5/3.0f * i;
float y = 300.0f/3.0f * i;
conePoints.push_back(cv::Point3f( x, y, 0));
conePoints.push_back(cv::Point3f(-x, y, 0));
}
std::vector<cv::Point3f> conePointsBig;
conePointsBig.push_back(cv::Point3f(0, 0, 0));
for (int i = 1; i <= 3; i++) {
float x = -77.5/3.0f * i;
float y = 505.0f/3.0f * i;
conePointsBig.push_back(cv::Point3f( x, y, 0));
conePointsBig.push_back(cv::Point3f(-x, y, 0));
}
conePointsVec.push_back(conePoints);
conePointsVec.push_back(conePointsBig);
cv::Mat imgGray;
cv::Mat roi_left_out;
// crop the left frame with the bounding box of a cone from the left frame
for (size_t i = 0; i < indices.size(); ++i)
{
int idx = indices[i];
Rect box = boxes[idx];
// cout << "idx is: " << idx << endl;
// try {
// cv::Mat roi(frame, box);
// if (roi.empty()) {
// continue;
// }
// cv::cvtColor(roi, imgGray, cv::COLOR_BGR2GRAY);
// roi_left_out = roi;
// } catch (const std::exception& e) { // reference to the base of a polymorphic object
// std::cout << "roi not possible" << endl; // information from length_error printed
// continue;
// }
// // perform SIFT on it to find the keypoints
// string detectorType = "SIFT";
// bool visDetector = false; // visualise the results
// vector<cv::KeyPoint> keypoints;
// DetectorTypeIndex detectorTypeIndex = getDetectorTypeIndex(detectorType);
// detKeypointsModern(keypoints, imgGray, detectorTypeIndex, visDetector);
// vector<cv::Point2f> keypoints_new;
// if (keypoints.size() == 0) {
// continue;
// }
// // manually moves the keypoints to the cone
// for (size_t i=0; i < keypoints.size(); i++) {
// cv::Point2f keypoint = keypoints[i].pt;
// keypoint.x = keypoint.x + centerX - 40;
// keypoint.y = keypoint.y + centerY - 40;
// // if (keypoint.x > centerX + width/3 || keypoint.x < centerX - width/3) {
// // continue;
// // }
// // if (keypoint.y > centerY + height/3 || keypoint.y < centerY - height/3) {
// // continue;
// // }
// keypoints_new.push_back(keypoint);
// }
// // // cout << "size of keypoints new is: " << keypoints.size() << endl;
// vector<cv::KeyPoint> keypoints_temp;
// cv::KeyPoint::convert(keypoints_new, keypoints_temp);
// // // cout << keypoints_new << endl;
// // ****************************************************************************************************************
// // draw the keypoints on the entire frame
// // drawKeypoints(frame, keypoints_temp, frame, Scalar(255, 255, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
// Mat tvec;
// Mat rvec;
// conePoints = conePointsVec[0];
// const std::vector<cv::Point3f> &conePts = conePoints;
// std::vector<cv::Point2f> keypoints_points;
// // cv::KeyPoint::convert(keypoints_temp, keypoints_points);
// cv::KeyPoint::convert(keypoints, keypoints_points);
// // cout << keypoints_points << endl;
// if (keypoints_points.size() < 7) {
// cout << "less than 7" << endl;
// continue;
// }
// std::vector<cv::Point2f> keypoints_points_tmp;
// // cout << keypoints_points << endl;
// keypoints_points_tmp.push_back(keypoints_points[0]);
// keypoints_points_tmp.push_back(keypoints_points[1]);
// keypoints_points_tmp.push_back(keypoints_points[2]);
// keypoints_points_tmp.push_back(keypoints_points[3]);
// keypoints_points_tmp.push_back(keypoints_points[4]);
// keypoints_points_tmp.push_back(keypoints_points[5]);
// keypoints_points_tmp.push_back(keypoints_points[6]);
// // cout << conePts << endl;
// // this won't work unless conePts and keypoints_points_tmp both have 7 elements each
// bool ret = cv::solvePnP(conePts, keypoints_points_tmp, cameraMatrix, distCoeffs, rvec, tvec, false, SolvePnPMethod::SOLVEPNP_IPPE);
// // estimate depth
// double est_depth = 0;
// // this line won't work till the solvePnP line works. sigh.
// if (tvec.size().empty()) {
// cout << "tvec size is [0x0]" << endl;
// continue;
// }
// // cout << tvec.size() << endl;
// est_depth = tvec.at<double>(2, 0);
// if (est_depth < 0) {
// est_depth = abs(est_depth);
// }
// cout << "est_depth is: " << est_depth << endl;
// cv::Mat imgGray_right;
// cv::Mat roi_right_out;
// cv::Rect projRect_out;
// //******************************************************************************************
// // bounding box for right image
// try {
// cv::Rect projRect(box);
// int x_p = box.x - lImgCenter_x;
// int x_pp = (f2/est_depth) * (est_depth/f1 * x_p - baseline);
// projRect.x = x_pp + rImgCenter_x - 200;
// // cout << "after projRect.x" << endl;
// cv::Mat roi_right(frame_right, projRect);
// if (roi_right.empty()) {
// continue;
// }
// cv::cvtColor(roi_right, imgGray_right, cv::COLOR_BGR2GRAY);
// roi_right_out = roi_right;
// projRect_out = projRect;
// } catch (const std::exception& e) { // reference to the base of a polymorphic object
// std::cout << "roi not possible" << endl; // information from length_error printed
// continue;
// }
// vector<cv::KeyPoint> keypoints_right;
// detKeypointsModern(keypoints_right, imgGray_right, detectorTypeIndex, visDetector);
// // cout << "after keypoint_right" << endl;
// vector<cv::Point2f> keypoints_new_right;
// if (keypoints_right.size() < 7) {
// cout << "less than 7" << endl;
// continue;
// }
// // cout << "size of keypoints: " << keypoints.size() << endl;
// // manually moves the keypoints to the cone
// for (size_t i=0; i < keypoints_right.size(); i++) {
// cv::Point2f keypoint = keypoints_right[i].pt;
// keypoint.x = keypoint.x + centerX - 40;
// keypoint.y = keypoint.y + centerY - 40;
// // if (keypoint.x > centerX + width/3 || keypoint.x < centerX - width/3) {
// // continue;
// // }
// // if (keypoint.y > centerY + height/3 || keypoint.y < centerY - height/3) {
// // continue;
// // }
// keypoints_new_right.push_back(keypoint);
// }
// // cout << "after resizing keypoint" << endl;
// // cout << "size of keypoints new is: " << keypoints.size() << endl;
// vector<cv::KeyPoint> keypoints_temp_right;
// cv::KeyPoint::convert(keypoints_new_right, keypoints_temp_right);
// // draw the keypoints on the entire frame
// // drawKeypoints(frame_right, keypoints_temp_right, frame_right, Scalar(255, 255, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
// std::vector<cv::KeyPoint> featureKeypoints1;
// std::vector<cv::KeyPoint> featureKeypoints2;
// cv::Mat descriptors1;
// cv::Mat descriptors2;
// std::vector<cv::DMatch> matches;
// std::vector<cv::DMatch> matchesFilt;
// // cv::Ptr<cv::Feature2D> featureDetector;
// cv::Ptr<cv::DescriptorMatcher> descriptorMatcher;
// cv::Ptr<cv::FastFeatureDetector> FeatureDetector = cv::FastFeatureDetector::create(40);
// FeatureDetector->detect(roi_left_out, featureKeypoints1);
// FeatureDetector->detect(roi_right_out, featureKeypoints2);
// cv::Ptr<cv::xfeatures2d::BriefDescriptorExtractor> brief = cv::xfeatures2d::BriefDescriptorExtractor::create(32);
// brief->compute(roi_left_out, featureKeypoints1, descriptors1);
// brief->compute(roi_right_out, featureKeypoints2, descriptors2);
// drawKeypoints(frame, featureKeypoints1, frame, Scalar(255, 255, 255), DrawMatchesFlags::DRAW_OVER_OUTIMG);
// // imshow("Image", frame);
// // waitKey(1000);
// // ********************************************************************************************************************
// // /*
// // No descriptor in left or right frame, either due to insufficient light.
// // Or plain ground textures, expecially in synthetic data
// // */
// if (descriptors1.empty() || descriptors2.empty()) {
// cout << "descriptors empty" << endl;
// continue;
// }
// cout << "before match ##################################################" << endl;
// // cout << descriptors1 << endl;
// // cout << descriptors2 << endl;
// if (descriptors2.size() != descriptors1.size()) {
// cout << "description matrix size doesn't match" << endl;
// continue;
// }
// descriptorMatcher->match(descriptors1, descriptors2, matches);
// cout << "after match ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
// // // this code is causing seg faults atm...
// // // Filters for horizontal-ish matches only
// uint32_t yDelta = projRect_out.height * 0.1;
// for (const cv::DMatch &match : matches) {
// if (abs(featureKeypoints1[match.queryIdx].pt.y - featureKeypoints2[match.trainIdx].pt.y) < yDelta) {
// matchesFilt.push_back(match);
// }
// }
// // // Check if no valid matches
// // if (matchesFilt.size() == 0) {
// // cout << "matchesFilt size 0 " << endl;
// // continue;
// // }
// // cout << "matches" << endl;
// // std::vector<float> disparity;
// // for (const cv::DMatch &match : matchesFilt) {
// // float x1 = featureKeypoints1[match.queryIdx].pt.x;
// // x1 += box.x;
// // x1 -= lImgCenter_x;
// // float x2 = featureKeypoints2[match.trainIdx].pt.x;
// // x2 += projRect.x;
// // x2 -= rImgCenter_x;
// // disparity.push_back(x1*f2/f1 - x2);
// // }
// // // // Performance loss from not using a sorted heap should be negligable
// // std::sort(disparity.begin(), disparity.end());
// // float medDisp = disparity[(int) disparity.size()/2];
// // float zEst = baseline*f2/medDisp;
// // cout << "zEst is: " << zEst << endl;
// // added by Kelvin
// // disparity = (left_box.x - lImgCenter_x)*f2/f1 - (right_box.x - rImgCenter_x);
// // float zEst = _baseline*f2/disparity;
// float zEst = 4400; // 4400mm
// float xEst = 0;
// float yEst = 0;
// // if (is_left == 1) {
// // left image
// xEst = zEst*(box.x + box.width/2 - lImgCenter_x)/f1; // Andrew's code has a minus sign in front of zEst
// yEst = zEst*(box.y + box.height - lImgCenter_y)/f1; // Andrew's code has a minus sign in front of zEst
// // }
// // else {
// // right image
// xEst = zEst*(box.x + box.width/2 - rImgCenter_x)/f2; // Andrew's code has a minus sign in front of zEst
// yEst = zEst*(box.y + box.height - rImgCenter_y)/f2; // Andrew's code has a minus sign in front of zEst
// // }
// // convert to m
// zEst = zEst/1000;
// xEst = xEst/1000;
// yEst = yEst/1000;
float zEst = 0;
int show_coordinates = 0;
drawPred(classIds[idx], confidences[idx], box.x, box.y,
box.x + box.width, box.y + box.height, frame, zEst, show_coordinates); //, xEst, yEst);
}
}
// Draw the predicted bounding box
void drawPred(int classId, float conf, int left, int top, int right, int bottom, Mat& frame, float zEst, int show_coordinates) // , float xEst, float yEst)
{
//Draw a rectangle displaying the bounding box
rectangle(frame, Point(left, top), Point(right, bottom), Scalar(255, 178, 50), 3);
//Get the label for the class name and its confidence
string label = format("%.2f", conf);
if (!classes.empty())
{
CV_Assert(classId < (int)classes.size());
label = classes[classId] + ":" + label;
}
// string conePoseEst = format("(%.2f, %.2f, %.2f)m", xEst, yEst, zEst);
string conePoseEst = format("(%.2f)m", zEst);
//Display the label at the top of the bounding box
int baseLine;
Size labelSize = getTextSize(label, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
Size conePoseEstSize = getTextSize(conePoseEst, FONT_HERSHEY_SIMPLEX, 0.5, 1, &baseLine);
top = max(top, labelSize.height);
rectangle(frame, Point(left, top - round(1.5*labelSize.height)), Point(left + round(1.5*labelSize.width), top + baseLine), Scalar(255, 255, 255), FILLED);
putText(frame, label, Point(left, top), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,0),1);
// print cone pose estimation
// rectangle(frame, Point(left, bottom + round(2*conePoseEstSize.height)), Point(left + round(1.5*conePoseEstSize.width), bottom + baseLine), Scalar(255, 255, 255), FILLED);
rectangle(frame, Point(left, bottom), Point(left, bottom + baseLine), Scalar(255, 255, 255), FILLED);
if (show_coordinates == 1) {
putText(frame, conePoseEst, Point(left, bottom + round(2*conePoseEstSize.height)), FONT_HERSHEY_SIMPLEX, 0.75, Scalar(0,0,0),1);
}
}
// Get the names of the output layers
vector<String> getOutputsNames(const Net& net)
{
static vector<String> names;
if (names.empty())
{
//Get the indices of the output layers, i.e. the layers with unconnected outputs
vector<int> outLayers = net.getUnconnectedOutLayers();
//get the names of all the layers in the network
vector<String> layersNames = net.getLayerNames();
// Get the names of the output layers in names
names.resize(outLayers.size());
for (size_t i = 0; i < outLayers.size(); ++i)
names[i] = layersNames[outLayers[i] - 1];
}
// for (int i = 0; names.size(); i++) {
// cout << names[i] << endl;
// }
return names;
}
// Remove the bounding boxes with low confidence using non-maxima suppression
void postprocess_yolo_right_img(Mat& frame, const vector<Mat>& outs, Mat& frame_right,
const vector<Mat>& outs_right, int is_left, Mat cameraMatrix, Mat distCoeffs) {
// added by Kelvin
double baseline = 550; // 550mm
double f2 = 1499.093; // focal length of right camera = 5mm (in the x axis)
double f1 = 2724.847; // focal length of left camera in the x axis
double lImgCenter_x = 548.607;
double lImgCenter_y = 845.6168;
double rImgCenter_x = 921.35846;
double rImgCenter_y = 616.6407;
// ****************************************** LEFT IMAGE *****************************************************
vector<int> classIds;
vector<float> confidences;
vector<Rect> boxes;
vector<int> centerX;
vector<int> centerY;
compute_bounding_box(outs, frame, classIds, confidences, boxes, centerX, centerY);
print_num_cones(classIds, "Left Image");
// Perform non maximum suppression to eliminate redundant overlapping boxes with lower confidences
vector<int> indices;
NMSBoxes(boxes, confidences, confThreshold, nmsThreshold, indices);
vector<cv::KeyPoint> final_keypoints;
keypoint_detection(indices, boxes, frame, final_keypoints, centerX, centerY);
//******************************************* RIGHT IMAGE *****************************************************
cv::Mat imgGray_right;
cv::Mat roi_right_out;
cv::Rect projRect_out;
vector<int> centerX_right;
vector<int> centerY_right;
vector<int> classIds_right;
vector<float> confidences_right;
vector<Rect> boxes_right;
compute_bounding_box(outs_right, frame_right, classIds_right, confidences_right, boxes_right, centerX_right, centerY_right);
print_num_cones(classIds_right, "Right Image");
// Perform non maximum suppression to eliminate redundant overlapping boxes with lower confidences
vector<int> indices_right;
NMSBoxes(boxes_right, confidences_right, confThreshold, nmsThreshold, indices_right);
vector<cv::KeyPoint> final_keypoints_right;
keypoint_detection(indices_right, boxes_right, frame_right, final_keypoints_right, centerX_right, centerY_right);
// cv::KeyPoint final_left_keypoint;
// cv::KeyPoint final_right_keypoint;
// average_keypoint(final_keypoints, )
// std::vector<float> disparity;
// float temp;
// float temp_min = 1000000;
// for (size_t i=0; i < keypoints_left.size(); i++) {
// cv::Point2f keypoint_left = keypoints_left[i];
// int idx = indices[i];
// Rect box = boxes[idx];
// for (size_t j=0; j < keypoints_right.size(); j++) {
// cv::Point2f keypoint_right = keypoints_right[j];
// int idx_right = indices_right[j];
// Rect box_right = boxes_right[idx_right];
// temp = (box.x + keypoint_left.x - lImgCenter_x)*f2/f1 - (box_right.x + keypoint_right.x - rImgCenter_x);
// if (temp < temp_min) {
// temp_min = temp;
// }
// }
// disparity.push_back(temp_min);
// temp_min = 100000000;
// }
// float disparity_avg = (abs(disparity[0]) + abs(disparity[1]))/2;
draw_bounding_boxes(indices, boxes, classIds, confidences, frame);
draw_bounding_boxes(indices_right, boxes_right, classIds_right, confidences_right, frame_right);
}
void print_num_cones(vector<int> classIds, string image) {
cout << image << endl;
int num_blue = 0;
int num_yellow = 0;
for (int i = 0; i < classIds.size(); i++) {
// blue cone
if (classIds[i] == 0) {
num_blue = num_blue + 1;
}
else if (classIds[i] == 2) {
num_yellow = num_yellow + 1;
}
}
cout << "Total Blue Cones: " << num_blue << endl;
cout << "Total Yellow Cones: " << num_yellow << endl;
}
// void average_keypoint(vector<KeyPoint>& input, vector <KeyPoint>& output) {
// cv::Point2f avg_keypoint;
// cv::KeyPoint avg_keypoint_KeyPoint;
// avg_keypoint.x = 0;
// avg_keypoint.y = 0;
// for (size_t i=0; i < input.size(); i++) {
// cv::Point2f keypoint = input[i].pt;
// avg_keypoint.x = avg_keypoint.x + keypoint.x;
// avg_keypoint.y = avg_keypoint.y + keypoint.y;
// }
// avg_keypoint.x = avg_keypoint.x/input.size();
// avg_keypoint.y = avg_keypoint.y/input.size();
// cv::KeyPoint::convert(avg_keypoint, avg_keypoint_KeyPoint);
// output.push_back(avg_keypoint_KeyPoint;