Skip to content

Commit 984b30a

Browse files
authored
Create face_detect3.py
1 parent 149816c commit 984b30a

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

Face-Detection/v3/face_detect3.py

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import cv2
2+
3+
face_cascade = cv2.CascadeClassifier("haarcascade_frontalface.xml")
4+
5+
6+
def detect():
7+
cap = cv2.VideoCapture(0)
8+
9+
while True:
10+
_, img = cap.read()
11+
12+
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
13+
14+
face = face_cascade.detectMultiScale(gray, 1.1, 4)
15+
a=str(len(face))
16+
font = cv2.FONT_HERSHEY_SIMPLEX
17+
if int(a)==1:
18+
cv2.putText(img,a+"face detected",(15,350), font, 2,(255,0,0),2,cv2.LINE_AA)
19+
else:
20+
cv2.putText(img,a+"faces detected",(15,350), font, 2,(255,0,0),2,cv2.LINE_AA)
21+
22+
for (x, y, w, h) in face:
23+
cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
24+
25+
cv2.imshow("Face Detect", img)
26+
27+
if cv2.waitKey(1) & 0xFF == ord('q'):# Press q to quit
28+
break
29+
30+
cap.release()
31+
cv2.destroyAllWindows()
32+
33+
34+
detect()

0 commit comments

Comments
 (0)