-
Notifications
You must be signed in to change notification settings - Fork 8
/
facevideo.py
128 lines (109 loc) · 4.67 KB
/
facevideo.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/usr/bin/env python3
# -*- coding = utf-8 -*-
import os
import sys
import glob
import json
import time
import argparse
import cv2
import mtcnn
from pprint import pprint
import numpy as np
import matplotlib.pyplot as plt
from util.baseimgops import resize, grayscale
from util.classifyimgops import apply_offsets
from util.constant import *
from tensorflow.keras.models import model_from_json
from tensorflow.keras.losses import categorical_crossentropy
from tensorflow.keras.optimizers import Adam
from data.load_data import get_ckplus_data
from util.info import load_info
X_train, X_validation, X_test, y_train, y_validation, y_test = get_ckplus_data()
# A program that detects faces from a continuous video feed.
# If running the program from the command line, you can choose the detector using the -m flag.
# Otherwise, if running in an IDE, set the below "runchoice" variable to whatever choice you want.
# Currently, the DNN is the best detector. It will always default to DNN and the -m argument, but it can be overridden.
# Make sure "runchoice" is "None" if you are not using it.
ap = argparse.ArgumentParser()
ap.add_argument("-m", "--model", default = "DNN",
help = "The type of model to be used. (MTCNN, DNN, or Cascade).")
ap.add_argument("-s", "--save", action = 'store_true',
help = "Save images from the video feed to a directory.")
args = vars(ap.parse_args())
runchoice = ""
detector = runchoice.lower() if runchoice.lower() in ["mtcnn", "dnn", "cascade", "fer"] else args["model"]
CENTER_X = 100
CENTER_Y = 80
CENTER_POS = (CENTER_X, CENTER_Y)
list = glob.glob('imageruntest/*')
for path in list:
try: os.remove(path)
except Exception as e:
print("Error while deleting: ", path)
raise e
vr = cv2.VideoCapture(0) # (VR -> Video Recognizer)
time.sleep(1) # Allow camera to initialize.
# Load classifiers and detectors.
cascade_face, net, cascade_eye = load_info(eyes = True)
det = mtcnn.MTCNN()
# Bring the video screen to the front (MacOS ONLY).
if sys.platform == "darwin": os.system("""osascript -e 'tell app "Finder" to set frontmost of process "Python" to be true'""")
def showPositionedWindow(window_name, img_name, coords):
cv2.namedWindow(window_name)
cv2.moveWindow(window_name, coords[0], coords[1])
cv2.imshow(window_name, img_name)
i = 0
while True:
_, frame = vr.read()
gray_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
if detector.lower() == "mtcnn": # MTCNN Detection
if i % 5 == 0:
faces = det.detect_faces(frame)
for face in faces:
print(face)
boundingbox = face['box']
cv2.rectangle(frame, (boundingbox[0], boundingbox[1]),
(boundingbox[0] + boundingbox[2], boundingbox[1] + boundingbox[3]),
(0, 255, 255), 2)
showPositionedWindow('frame', frame, CENTER_POS)
if detector.lower() == "dnn": # DNN Detection
(h, w) = frame.shape[:2]
blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)), 1.0, (300, 300), swapRB = False, crop = False)
net.setInput(blob)
faces = net.forward()
for k in range(0, faces.shape[2]):
c = faces[0, 0, k, 2]
if c < 0.5: continue
box = faces[0, 0, k, 3:7] * np.array([w, h, w, h])
(x, y, xe, ye) = box.astype("int")
# FOR TESTING
# face_coordinates = (x, y, xe, ye)
# x1, x2, y1, y2 = apply_offsets(face_coordinates, (20, 40))
cv2.rectangle(frame, (x, y), (xe, ye), (0, 255, 255), 2)
image = grayscale(resize(frame[x: xe, y: ye])) / 255
# img = np.expand_dims(img, axis = -1)
# pprint(ckplus_classes[np.argmax(model.predict(img))])
if detector.lower() == "cascade": # Cascade Detection
faces = cascade_face.detectMultiScale(gray_frame, scaleFactor = 1.2, minNeighbors = 5)
for (x, y, w, h) in faces:
if i % 5 == 0 and i != 0:
print(str(i) + ": " + str(faces))
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 3)
gray_reg = gray_frame[y:y+h, x:x+w]
color_reg = frame[y:y+h, x:x+w]
eyes = cascade_eye.detectMultiScale(gray_reg, scaleFactor = 1.2)
# Uncomment below if you want to try to detect eyes, but it doesn't work quite well.
for (x1, y1, w1, h1) in eyes:
# cv2.rectangle(color_reg, (x1, y1), (x1 + w1, y1 + h1), (0, 0, 255), 2)
pass
showPositionedWindow('frame', frame, CENTER_POS)
# To save images from the video feed.
if args['save']:
if i % 5 == 0 and i != 0:
cv2.imwrite('imageruntest/testimg' + str(i) + '.jpg', frame)
if cv2.waitKey(1) & 0xFF == ord('z'):
break
i += 1
vr.release()
cv2.destroyAllWindows()