immortal11 commited on
Commit
8beff47
·
verified ·
1 Parent(s): c7e4513

Create classifier.py

Browse files
Files changed (1) hide show
  1. classifier.py +88 -0
classifier.py ADDED
@@ -0,0 +1,88 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import cv2
2
+ import os
3
+ import numpy as np
4
+ from tensorflow.keras.optimizers import Adam
5
+ from tensorflow.keras.models import load_model
6
+ from tensorflow.keras.models import model_from_json
7
+ import time
8
+
9
+
10
+ class Classifier:
11
+ def __init__(self):
12
+ self.is_image = None
13
+ self.classifier = None
14
+ self.face_detector = None
15
+ self.labels = {0: "Angry", 1: "Disgusted", 2: "Fearful",
16
+ 3: "Happy", 4: "Neutral", 5: "Surprised", 6: "Sad"}
17
+ self.load_models()
18
+
19
+ def check_file_type(self, is_image, input_path):
20
+ self.is_image = is_image
21
+ self.input_path = input_path
22
+ self.output_path = "result/" + self.input_path.split('/')[-1]
23
+ os.makedirs("result", exist_ok=True)
24
+
25
+ def load_models(self):
26
+ # Load VGG
27
+ yaml_file = open('models/VGG19.yaml', 'r')
28
+ self.classifier = model_from_json(yaml_file.read())
29
+ self.classifier.load_weights("models/VGG19.h5")
30
+ yaml_file.close()
31
+ print("VGG19 has been loaded")
32
+
33
+ # Load Face Detector
34
+ self.face_detector = cv2.CascadeClassifier(
35
+ 'models/haarcascade_frontalface_default.xml')
36
+ cv2.ocl.setUseOpenCL(False)
37
+ print("Face Detector has been loaded")
38
+
39
+ def predict(self, frame):
40
+
41
+ faces = self.face_detector.detectMultiScale(
42
+ frame, scaleFactor=1.3, minNeighbors=5)
43
+
44
+ gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
45
+ for (x, y, w, h) in faces:
46
+ cv2.rectangle(frame, (x, y-50), (x+w, y+h+10), (255, 0, 0), 2)
47
+ roi_gray = gray[y:y + h, x:x + w]
48
+ roi_color = frame[y:y + h, x:x + w]
49
+ # Convert grayscale to RGB
50
+ roi_color_rgb = cv2.cvtColor(roi_color, cv2.COLOR_BGR2RGB)
51
+ cropped_img = np.expand_dims(np.expand_dims(
52
+ cv2.resize(roi_color_rgb, (48, 48)), 0), -1)
53
+ prediction = self.classifier.predict(cropped_img)
54
+ maxindex = int(np.argmax(prediction))
55
+ cv2.putText(frame, self.labels[maxindex], (x+20, y-60),
56
+ cv2.FONT_HERSHEY_SIMPLEX, 1.4, (0, 0, 255), 2, cv2.LINE_AA)
57
+
58
+ return frame
59
+
60
+ def run(self):
61
+ start_time = time.time()
62
+ if self.is_image:
63
+ image_arr = cv2.imread(self.input_path)
64
+ predicted_frame = self.predict(frame=image_arr)
65
+ cv2.imwrite(self.output_path, predicted_frame)
66
+ else:
67
+ cap = cv2.VideoCapture(self.input_path)
68
+
69
+ # Get video properties
70
+ frame_width = int(cap.get(3))
71
+ frame_height = int(cap.get(4))
72
+ fps = int(cap.get(5))
73
+
74
+ # Define the codec and create a VideoWriter object to save the output video as .MOV
75
+ fourcc = cv2.VideoWriter_fourcc('a', 'v', 'c', '1')
76
+ out = cv2.VideoWriter(self.output_path, fourcc, fps,
77
+ (frame_width, frame_height))
78
+
79
+ while True:
80
+ ret, frame = cap.read()
81
+ if not ret:
82
+ break
83
+
84
+ predicted_frame = self.predict(frame=frame)
85
+ out.write(predicted_frame)
86
+
87
+ total_time = str(round(time.time() - start_time, 3)) + "s"
88
+ return self.output_path, total_time