-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfindcat.cpp
More file actions
72 lines (62 loc) · 1.68 KB
/
Copy pathfindcat.cpp
File metadata and controls
72 lines (62 loc) · 1.68 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
/*
code largely gathered from:
https://docs.opencv.org/4.11.0/db/d28/tutorial_cascade_classifier.html
and the related github for the example:
https://github.com/opencv/opencv/tree/4.x/data/haarcascades
*/
#include "opencv2/objdetect.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include <iostream>
using namespace std;
using namespace cv;
void detectAndDisplay(Mat frame);
CascadeClassifier cat_cascade;
int main()
{
// Load the cascade
if(!cat_cascade.load("haarcascade_frontalcatface.xml"))
{
cout << "--(!)Error loading cat cascade\n";
return -1;
}
// Open camera 2
VideoCapture cap(2);
if(!cap.isOpened())
{
cout << "--(!)Error opening camera 2\n";
return -1;
}
Mat frame;
while(true)
{
cap >> frame;
if(frame.empty())
{
cout << "--(!)No frame captured from camera\n";
break;
}
// Apply the classifier to the frame
detectAndDisplay(frame);
// Exit on ESC key
if(waitKey(1) == 27)
break;
}
return 0;
}
void detectAndDisplay(Mat frame)
{
Mat frame_gray;
cvtColor(frame, frame_gray, COLOR_BGR2GRAY);
equalizeHist(frame_gray, frame_gray);
// Detect cats
std::vector<Rect> cats;
cat_cascade.detectMultiScale(frame_gray, cats);
for(size_t i = 0; i < cats.size(); i++)
{
Point center(cats[i].x + cats[i].width/2, cats[i].y + cats[i].height/2);
ellipse(frame, center, Size(cats[i].width/2, cats[i].height/2), 0, 0, 360, Scalar(255, 0, 255), 4);
}
// Show the output
imshow("Cat Detection", frame);
}