How to use the Object Detection Bounding Box in a decision condition to turn on the LED on Raspberry Pi using C++ OpenCV? -
I am using OpenCV 2.4.8 to create an application that will detect guns. The application will be posted on Raspberry P. An LED / buzzer is also connected to Raspberry P. If the gun is detected, then the LED / buzzer should be turned on according to certain conditions. I have attached the relevant code. The problem is how to tell the code whether the gun is detected or not? I mean, the traced gun has been shown by a bounding box. Now, how to use that bounding box in IF-condition? I have commented on the desired lines in the code, like what I really want to do, can someone guide me how to do it? Any help is highly appreciated! Thank you.
This is my code snippet:
for (;;) {cap.retrieve (frame); Caps & gt; & Gt; Frame; Std :: vector & lt; Rect & gt; Guns; Mat frame_grey; CvtColor (frame, frame_grey, COLOR_BGR2GRAY); Equalize (frame_grey, frame_grey); // Address Guns gun_cascade.detectMultiScale (frame_gray, guns, 1.1, 2, 0 | CASCADE_SCALE_IMAGE, size (30, 30)); // Set Interest Area cv :: Rect roi_b; CV :: rectangle roi_c; CV :: ReadBox; Size_ty IC = 0; // IC is an index of the current element, AC = 0; // The current element is the area of size_t ib = 0; //IiB's largest element is int ab = 0; // AB (ic = 0; ic & lt; guns.size (IC ++) // Re-enter all existing elements (detected guns) {roi_c.x = gun [IC] .x; Roi_c.y = gun [IC] .y; Roi_c.width = (gun [IC] .Wide); Roi_c.height = (gun [IC]. Highlight); AC = roi_c.width * roi_c.height; // current Get the area of the element (detection gun) roi_b.x = gun [ib] .x; Roi_b.y = gun [ib] .y; Roi_b.width = (gun [ib] wide); Roi_b.height = (Gun [ib]. Hi); ab = roi_b.width * roi_b.height; // Get the area of the largest element, initially Is similar to the "present" element if (AC> AB) {IB = IC; Roi_b.x = gun [ib] .x; Roi_b.y = gun [ib] .y; Roi_b.width = (gun [ Ib] wide; roi_b.height = (gun [ib] .hite);} point pt1 (gun [ic] .x, gun [ic].); // display guns on main window in display- cameras //www.bbox.com/cvRect (roi_b.x) from the live stream point pt2 (gun (ic) .x + gun [ic] .high), (guns [ii] .i + gun [ic] Roi_b.y, roi_b.width, roi_b.height); // int counter = 0; Rectangle (frame, pt1, pt2, scalar (0, 0, 255), 2, 8, 0); PutText (frame, "Gun Detected!", Pt1, FONT_HERSHEY_COMPLEX_SMALL, 0.8, cvScalar (0,250,250), 1, CV_AA); // if (gun is detected) // counter = counter + 1; / * If (counter> 5) {Turn on the LED of Raspberry P; Counter = 0; } * //} imshow for the end loop ("original", frame); If (wait (30)> ==) break; } // the end of the external for loop returns 0; } // main main
You have a very clear code that can be brought down For single-line statements.
ab = 0; (IC = 0; IC & lt; guns size (IC ++) {roi_c = guns [i]; if (roi_c.area ()> ab) {roi_b = roi_c; ab = roi_c.area (); } Rectangle (frame, ROIIC, scalar (0, 0, 255), 2, 8, 0);} If you want to check that at least If a gun is detected, then you can type: if (guns.size ()> gt0) {..} If you want to check whether the biggest gun with some threshold You can go with something like (roi_b.area () & gt; some_threshold) . Hope this answer.
Comments
Post a Comment