-
Notifications
You must be signed in to change notification settings - Fork 1
/
live.py
62 lines (47 loc) · 1.74 KB
/
live.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import argparse
import cv2 as cv
import matplotlib.pyplot as plt
import sys
from matplotlib.animation import FuncAnimation
from utils import (
get_faces_frame,
FaceParams,
option_parser,
)
# ----------------------------------------------------------------------
# Parse command line arguments
# ----------------------------------------------------------------------
parser = argparse.ArgumentParser(
prog='live',
parents=[option_parser],
description='Detect faces from camera.'
)
args = parser.parse_args()
face_params = FaceParams(
args.scaleFactor,
args.minNeighbors,
args.minSize)
# ----------------------------------------------------------------------
# Setup video
# ----------------------------------------------------------------------
camera = cv.VideoCapture(0) # Get the first camera
if not camera.isOpened():
print('Unable to load camera!')
sys.exit(1)
# ----------------------------------------------------------------------
# Setup plot
# ----------------------------------------------------------------------
plt.axis('off') # Turn off axis in plot window
# ----------------------------------------------------------------------
# Detect faces and show results
# See [update frame in matplotlib with live camera preview](https://stackoverflow.com/a/44604435)
# ----------------------------------------------------------------------
print("\nPlease close the window (Ctrl-w) to quit.")
im = plt.gca().imshow(get_faces_frame(camera, face_params=face_params))
video = FuncAnimation(
plt.gcf(),
lambda i: im.set_data(get_faces_frame(camera, face_params=face_params)), # Update plot window with new camera frame
interval=100)
plt.show()
# When everything is done, release the capture
camera.release()