-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathface_detector.py
40 lines (28 loc) · 1.07 KB
/
face_detector.py
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
import cv2
from random import randrange
# import randrange
# Load some pre_trained data on face frontals from opencv (haar cascade algorithm)
trained_face_data = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Choose photo
# img = cv2.imread('face.jpg') # import from my folder
# webcam_img = cv2.imread('webcam.jpg') # import from my folder
# Realtime video
webcam = cv2.VideoCapture(0)
while True:
# Read the frame
succesful_frame_read, frame = webcam.read()
# Convert the grayscale
grayscale_img = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces
face_coordinates = trained_face_data.detectMultiScale(grayscale_img)
# Draw the rectangles around the faces
for (x,y,w,h) in face_coordinates:
cv2.rectangle(frame, (x,y),(x + w,y + h) ,(0,255,0),2) # (image,top left , bottom right,color(BGR()),thickness)
cv2.imshow('Face detector',frame)
key = cv2.waitKey(1) # 1 miliseconds
# Stop if Q pressed
if key == 81 or key == 113:
break
# Release webcam
webcam.release()
print("Code completed")