-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathface-detector.py
More file actions
30 lines (22 loc) · 837 Bytes
/
face-detector.py
File metadata and controls
30 lines (22 loc) · 837 Bytes
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
import cv2
#Some pre-trained data on face frontels from opencv(haar cascade algorithm)
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#choose an image to detect faces in
#img = cv2.imread('elon.png')
#to capture video from webcam
webcam = cv2.VideoCapture(0)
###read the current frame
while True:
successful_frame_read, frame = webcam.read()
#convert to grayscale
grayscaled_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
#detect faces
face_coordinates = trained_face_data.detectMultiScale(grayscaled_img)
#draw rectangle around the faces
for (x, y, w, h) in face_coordinates:
cv2.rectangle(frame,(x,y),(x+w, y+h),(0, 255, 0), 2)
cv2.imshow('User',frame)
key = cv2.waitKey(1)
#stop if Q key is pressed
if key == 81 or key == 113:
break