-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.py
59 lines (46 loc) · 1.72 KB
/
test.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
import os
import numpy as np
import cv2
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
from tensorflow import keras
from keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras import layers
from tensorflow.keras import Model
from keras.optimizers import Adam
from keras.models import load_model
model = load_model('model.h5')
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
base = 'images'
train_set = os.path.join(base,'train')
train_generator = ImageDataGenerator(rescale = 1./255)
train = train_generator.flow_from_directory(train_set,
target_size = (96,96),
color_mode = 'grayscale',
batch_size = 64,
class_mode = 'categorical')
video = cv2.VideoCapture(0)
while True:
ret, frame = video.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
faces = faceCascade.detectMultiScale(
gray,
scaleFactor=1.1,
minNeighbors= 5,
minSize=(30, 30)
)
for (x, y, w, h) in faces:
cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 255), 2)
resized = cv2.resize(gray, (96,96))
resized = np.reshape(resized/255,(1,96,96,1))
output = model.predict(resized)
output = np.argmax(output.flatten())
labels = (train.class_indices)
labels = dict((v,k) for k,v in labels.items())
maximum = labels[output]
cv2.putText(frame, maximum, (x,y-20), cv2.FONT_HERSHEY_SIMPLEX,1, (0,255,255),1 )
cv2.imshow('Video', frame)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
video.release()
cv2.destroyAllWindows()