-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFaceDetection.cpp
More file actions
95 lines (66 loc) · 2.09 KB
/
Copy pathFaceDetection.cpp
File metadata and controls
95 lines (66 loc) · 2.09 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
#include "opencv2/imgcodecs.hpp"
#include "opencv2/highgui.hpp"
#include "opencv2/imgproc.hpp"
#include "opencv2/objdetect.hpp"
#include <iostream>
using namespace std;
void findFaceFromPic(string path) {
cv::Mat photo = cv::imread(path);
cv::CascadeClassifier faceCascade;
faceCascade.load("haarcascade_frontalface_default.xml"); // Your cascade location
if (faceCascade.empty()) cout << "XML file is not loaded." << endl;
else {
vector<cv::Rect> faces;
faceCascade.detectMultiScale(photo, faces, 1.1, 10);
for (int i = 0; i < faces.size(); i++) {
cv::rectangle(photo, faces[i].tl(), faces[i].br(), cv::Scalar(255, 0, 255), 3);
}
}
cv::imshow("Müdür", photo);
cv::waitKey(0);
cv::destroyWindow("Müdür");
}
void findFaceFromCam(int ch) {
cv::VideoCapture cam;
cv::CascadeClassifier faceCascade;
faceCascade.load("haarcascade_frontalface_default.xml");
if (cam.open(ch)) {
cout << "Cam is open rn." << endl;
cv::Mat img;
if (faceCascade.empty()) cout << "XML file is not loaded." << endl;
else {
while (cam.grab()) {
vector<cv::Rect> faces;
cam.retrieve(img);
faceCascade.detectMultiScale(img, faces, 1.1, 10);
for (int i = 0; i < faces.size(); i++) {
cv::rectangle(img, faces[i].tl(), faces[i].br(), cv::Scalar(255, 0, 255), 3);
cv::putText(img, "Human", { faces[i].x, faces[i].y - 20 }, cv::FONT_HERSHEY_TRIPLEX, 1, cv::Scalar(240, 214, 45), 2);
}
cv::imshow("Webcam Rty", img);
if (cv::waitKey(5) == 27) {//when "esc" is pressed, video ends
break;
cv::destroyAllWindows();
}
}
}
}
else {
cout << "Cam is not open might be wrong channel." << endl;
}
}
int main() {
cout << "Press 1 for finding pictures from a picture, 2 for webcam." << endl;
int sel; cin >> sel;
if (sel == 1) {
cout << "Type the path: " << endl;
string path; cin >> path;
findFaceFromPic(path);
}
else if (sel == 2) {
cout << "Type the channel (mostly 0 or 1): " << endl;
int path; cin >> path;
findFaceFromCam(path);
}
return 0;
}