-
Notifications
You must be signed in to change notification settings - Fork 1
/
camerafeed-v1.py
68 lines (54 loc) · 1.97 KB
/
camerafeed-v1.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
import cv2
import numpy as np
from model import Model
# Instantiate Model
model = Model()
def output_prediction(image):
mod = model.create_model()
image = model.process(image)
return model.predictor(mod, image)
# Load Classifiers for Facial Detection
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# Video Capture
cap = cv2.VideoCapture(0)
while cap.isOpened():
ret, frame = cap.read()
frame = cv2.flip(frame, 1)
# Convert Frame to GRAYSCALE
frame_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
# Returns face in [x, y, w, h] relative to top left corner
face = face_cascade.detectMultiScale(frame_gray,
scaleFactor=1.3,
minNeighbors=5,
minSize=(60, 60),
flags=cv2.CASCADE_SCALE_IMAGE)
# Define gender
gender = 'female'
for fx, fy, fw, fh in face:
fy -= 40
fh += 50
face_frame = frame[fy:fy+fh, fx:fx+fw]
crop_img = cv2.resize(face_frame, (128, 128))
cv2.imshow('crop', crop_img)
img = cv2.rectangle(frame, (fx, fy),
(fx + fw, fy + fh),
(255, 0, 0), 1)
pred = output_prediction(crop_img)
print(pred)
if pred == 1:
cv2.putText(frame,
'Male', (fx + 10, fy - 10),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 0), 1) # Face Text
else:
cv2.putText(frame,
'Female', (fx + 10, fy - 10),
cv2.FONT_HERSHEY_COMPLEX, 0.5, (255, 255, 0), 1) # Face Text
if ret:
cv2.imshow('Video Capture', frame)
k = cv2.waitKey(1) & 0xFF
if k == ord('q'):
break
else:
break
cap.release()
cv2.destroyAllWindows()