-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrun.py
More file actions
104 lines (86 loc) · 2.54 KB
/
Copy pathrun.py
File metadata and controls
104 lines (86 loc) · 2.54 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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
from collections import abc
import cv2
import numpy as np
from keras import models
from tensorflow.keras.preprocessing.image import img_to_array
# Load haraarcascade to make box around face
detector = cv2.CascadeClassifier('utils/haarcascade_frontalface_default.xml')
# Load the saved model
model = models.load_model('utils/model_weights.h5')
# Capture video using cv2
video = cv2.VideoCapture(0)
# Prevents openCL usage and unnecessary logging messages
cv2.ocl.setUseOpenCL(False)
# Dictionary which assigns each label an emotion (alphabetical order)
emotion_dict = {0: "Marah", 1: "Bahagia", 2: "Netral", 3: "Sedih"}
# Keep looping
while True:
# Retrive video frame to draw bounding box around face
retrive, frame = video.read()
if not retrive:
break
# Convert frame to BGR2GRAY
gray = cv2.cvtColor(
frame,
cv2.COLOR_BGR2GRAY
)
# Detect face
faces = detector.detectMultiScale(
gray,
scaleFactor=1.3,
minNeighbors=5
)
# Loop each attribute in faces
for (x, y, w, h) in faces:
# Draw rectangle for face detection overlay
cv2.rectangle(
frame,
(x, y - 50),
(x + w, y + h + 10),
(255, 0, 0),
2
)
# Conver color to gray
roi_gray = gray[
y:y + h,
x:x + w
]
# crop image(chage size) to 64x64
cropped_img = np.expand_dims(
np.expand_dims(
cv2.resize(
roi_gray,
(48, 48)
), -1
), 0
)
# Prediction
prediction = model.predict(cropped_img)
# Get index value(prediction array value from model_weight.h5)
maxindex = int(np.argmax(prediction)) # <- not used, bcos now we compare by akurasi yg paling tinggi, if we use this nnti bakal di bulaitn ke 0/1 so we can't used this.
print(maxindex)
# Add text to rectangle overlay
cv2.putText(
frame,
emotion_dict[maxindex],
(x+20, y-60),
cv2.FONT_HERSHEY_SIMPLEX, 1,
(255, 255, 255), 2,
cv2.LINE_AA
)
# Show image output
cv2.imshow(
'Video',
cv2.resize(
frame,
(1600, 960),
interpolation=cv2.INTER_CUBIC
)
)
# Make q hotkey for close camera
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Run video
video.release()
# Cloase all related window(video)
cv2.destroyAllWindows()