forked from hsSam/PracticalPythonAndOpenCV_CaseStudies
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcam.py
More file actions
44 lines (33 loc) · 1.12 KB
/
cam.py
File metadata and controls
44 lines (33 loc) · 1.12 KB
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
41
42
43
44
from utilities.facedetector import FaceDetector
from utilities import imutils
import cv2
# Define paths
video_path = 'video/adrian_face.mov'
cascade_path = 'cascades/haarcascade_frontalface_default.xml'
# Construct the face detector
detector = FaceDetector(cascade_path)
# Load the video
camera = cv2.VideoCapture(video_path)
while True:
# Grab the current frame
(ok, frame) = camera.read()
# If a frame does not exist, video is over
if not ok:
break
# Resize the frame and convert it to greyscale
frame = imutils.resize(frame, width=300)
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Detect faces in the image and clone the frame
face_boxes = detector.detect(gray, 1.1, 5)
clone = frame.copy()
# Draw the face bounding boxes
for (x, y, w, h) in face_boxes:
cv2.rectangle(clone, (x, y), (x + w, y + h), (0, 255, 0), 2)
# Show our detected faces
cv2.imshow("Face", clone)
# Ff the 'q' key is pressed, stop the loop
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# Cleanup the camera and close any open windows
camera.release()
cv2.destroyAllWindows()