Skip to content

Commit 239097c

Browse files
committed
Face detection
1 parent 74c4755 commit 239097c

File tree

3 files changed

+38
-2
lines changed

3 files changed

+38
-2
lines changed

.gitattributes

Lines changed: 0 additions & 2 deletions
This file was deleted.
Binary file not shown.
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import cv2
2+
3+
cap = cv2.VideoCapture(0)
4+
5+
# haar trained model on frontal faces only
6+
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
7+
8+
9+
while(True):
10+
11+
ret, frame = cap.read() #read frame by frame
12+
#A frame is a array of 3 matrices where each matrix is for the respective color blue, green, red
13+
14+
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) #convert into gray
15+
16+
#gray is a single matrix now
17+
18+
#detectMultiScale function detect faces and return an array of position coordinates and sizes
19+
# 1.3 is scaled factor, if its high we may miss some pixels and thus faces
20+
# 5 is minNeighbors, defines how many neighbor rectangles should be identified to retain it, higher value = less false positives
21+
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
22+
23+
24+
25+
#define rectangle if faces detected, passed red in BGR format
26+
for (x,y,w,h) in faces:
27+
28+
cv2.rectangle(frame, (x, y), (x+w, y+h), (0 , 0, 255), 2)
29+
30+
cv2.imshow('frame', frame)
31+
32+
if cv2.waitKey(1) & 0xFF == ord('q'):#q terminates script
33+
34+
break
35+
36+
cap.release()
37+
38+
cv2.destroyAllWindows()

0 commit comments

Comments
 (0)