-
Notifications
You must be signed in to change notification settings - Fork 0
/
webcam.py
executable file
·82 lines (63 loc) · 2.03 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/env python
import cv2
from time import sleep
from skimage import transform
import numpy as np
cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)
video_capture = cv2.VideoCapture(0)
def normalize(img, size=200):
return transform.resize(img, [size, size], mode='constant')
def MSE(img, img2):
img2 = normalize(img2).reshape([-1, 1])
img = normalize(img).reshape([-1, 1])
return np.sum(np.power(img - img2, 2))
def read():
my_face = cv2.imread('face.png', cv2.IMREAD_GRAYSCALE)
while True:
# Capture frame-by-frame
ret, frame = video_capture.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Display frame
cv2.imshow('Video', frame)
# Detect face
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors=5,
minSize=(30, 30)
)
# Draw a rectangle around the faces
if len(faces):
(x, y, w, h) = faces[0]
face = frame[y:y+h,x:x+w]
face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
face2 = face.copy()
face = normalize(face)
error = 10 ** 10
if my_face is not None:
error = MSE(my_face, face)
color = (0, 0, 255)
if error < 1000:
color = (0, 255, 0)
cv2.rectangle(frame, (x, y), (x+w, y+h), color, 2)
cv2.imshow('Video', frame)
print(f'w: {w:<5}h: {h:<5}{error}')
# cv2.imshow('Face', face)
else:
cv2.destroyWindow('Face')
key = cv2.waitKey(1) & 0xFF
if key == ord('s'):
cv2.imwrite('face.png', face2)
return True
elif key == ord('q'):
return False
def main():
r = True
while r:
r = read()
# When everything is done, release the capture
video_capture.release()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()