-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.py
39 lines (28 loc) · 1.13 KB
/
webcam.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
# import the opencv library
import cv2
face_classifier = cv2.CascadeClassifier(
cv2.data.haarcascades + "haarcascade_frontalface_default.xml"
)
vader = cv2.imread('images/Darth-Vader-Mask.png', cv2.IMREAD_UNCHANGED)
# define a video capture object
video_capture = cv2.VideoCapture(0)
def detect_bounding_box(vid):
gray_image = cv2.cvtColor(vid, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray_image, 1.1, 5, minSize=(40, 40))
for (x, y, w, h) in faces:
cv2.rectangle(vid, (x, y), (x + w, y + h), (0, 255, 0), 4)
return faces
while video_capture.isOpened():
result, video_frame = video_capture.read() # read frames from the video
if result is False:
break # terminate the loop if the frame is not read successfully
faces = detect_bounding_box(
video_frame
) # apply the function we created to the video frame
cv2.imshow(
"My Face Detection Project", video_frame
) # display the processed frame in a window named "My Face Detection Project"
if cv2.waitKey(1) & 0xFF == ord("q"):
break
video_capture.release()
cv2.destroyAllWindows()