forked from vincentteyssier/emotion-detection-raspberry
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathemotion_detection_v1.py
More file actions
84 lines (66 loc) · 2.56 KB
/
emotion_detection_v1.py
File metadata and controls
84 lines (66 loc) · 2.56 KB
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
from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2
import numpy as np
import tensorflow as tf
import sys
DEBUG = True
# instanciate the camera
camera = PiCamera()
camera.resolution = (1920, 1080)
camera.framerate = 30
rawCapture = PiRGBArray(camera, size=(1920, 1080))
# allow the camera to warmup
time.sleep(0.1)
# Loads label file, strips off carriage return
label_lines = [line.rstrip() for line
in tf.gfile.GFile("./retrained_data/retrained_labels.txt")]
# load our pretrained model
with tf.gfile.FastGFile("./retrained_data/retrained_graph.pb", 'rb') as f:
graph_def = tf.GraphDef()
graph_def.ParseFromString(f.read())
_ = tf.import_graph_def(graph_def, name='')
# We use the Haar Cascade classifier
faceDetect = cv2.CascadeClassifier('./retrained_data/haarcascade_frontalface_default.xml')
# start the tensorflow session and start streaming and image processing
sess = tf.Session()
softmax_tensor = sess.graph.get_tensor_by_name('final_result:0')
# capture frames from the camera
for frame in camera.capture_continuous(rawCapture, format="bgr", use_video_port=True):
# transform into a numpy array
image = frame.array
# show the frame
cv2.imshow("face", image)
if DEBUG:
print (image.shape)
# transform to Gray scale
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
if DEBUG:
print (gray.shape)
# detect faces in our gray picture
faces = faceDetect.detectMultiScale(gray,
scaleFactor=1.3,
minNeighbors=5
)
for (x,y,w,h) in faces:
#sampleNum = sampleNum+1
#cv2.imwrite("./temp_dataset/"+str(sampleNum)+".jpg", gray[y:y+h,x:x+w])
# feed the detected face (cropped image) to the tf graph
predictions = sess.run(softmax_tensor, {'DecodeJpeg:0': gray[y:y+h,x:x+w]})
prediction = predictions[0]
# Get the highest confidence category.
prediction = prediction.tolist()
max_value = max(prediction)
max_index = prediction.index(max_value)
predicted_label = label_lines[max_index]
print("%s (%.2f%%)" % (predicted_label, max_value * 100))
cv2.rectangle(image,(x,y),(x+w,y+h),(255,0,0),2)
cv2.waitKey(100)
key = cv2.waitKey(1) & 0xFF
# clear the stream in preparation for the next frame
rawCapture.truncate(0)
# if the `q` key was pressed, break from the loop
if key == ord("q"):
cv2.destroyAllWindows()
break