-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathproject_prototype.py
134 lines (107 loc) · 5.35 KB
/
project_prototype.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
129
130
131
132
133
134
""" This file contains a small demo for our project using
only the CNN architectures. """
import glob
import time
import logging
import cv2
import numpy as np
from silence_tensorflow import silence_tensorflow
silence_tensorflow()
import tensorflow as tf
from NN_Models import AdverbModel, CharacterCNN, ColorModel, CommandModel, NumberModel, PrepositionModel
from CNN_DataPreparation import ConcatenateDataSet
def getTrainedModel(modelPath, modelCategory):
print("Fetching model weights...")
modelDict = {'Adverb': AdverbModel.AdverbNet(),
'Alphabet': CharacterCNN.CharCNN(),
'Colors': ColorModel.ColorsNet(),
'Commands': CommandModel.CommandsNet(),
'Numbers': NumberModel.NumbersNet(),
'Prepositions': PrepositionModel.PrepositionsNet()}
model = modelDict[modelCategory]
model.Model.compile(optimizer="Adam", loss='categorical_crossentropy', metrics=['accuracy'])
model.Model = tf.keras.models.load_model(modelPath + '/')
print("{} Model is loaded...".format(modelCategory))
return model.Model
def predictOneVideo(classDict, videoPath, operationMode):
# Model Loading:
savedModelPath = './SavedModels/'
videoPath = videoPath.replace("\\", "/")
path = videoPath.split('/')
categoryCNN = path[-1].split('_')[0]
if categoryCNN == '':
return "Error! No videos were passed"
savedModelPath += categoryCNN
dictForClass = classDict[categoryCNN]
cnnModel = getTrainedModel(savedModelPath, categoryCNN)
# Video Concatenation Operations:
print("Starting video preparation operations...")
haarDetector = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')
videoFrames = ConcatenateDataSet.getVideoFrames(videoPath)[:30]
if operationMode != 'SERVER':
# Rotate Frames:
videoFrames = [cv2.rotate(x, cv2.ROTATE_90_COUNTERCLOCKWISE) for x in videoFrames]
videoFrames = [ConcatenateDataSet.extractLipsHaarCascade(haarDetector, x) for x in videoFrames]
concatenatedImage = ConcatenateDataSet.stackFramesToImage(videoFrames)
if concatenatedImage is not None:
# Image Preparation:
if len(concatenatedImage.shape) == 3 and concatenatedImage.shape[2] != 1:
concatenatedImage = cv2.cvtColor(concatenatedImage, cv2.COLOR_BGR2GRAY)
img = cv2.resize(concatenatedImage, (224, 224))
img = np.array(img, dtype=np.float32)
img = np.reshape(img, (-1, 224, 224, 1))
img = img / 255
# Model Prediction:
print("Starting Model Prediction...")
modelPrediction = cnnModel.predict_classes(img)
log.info(modelPrediction)
log.info(cnnModel.predict(img))
dictForClass = list(dictForClass.items())
prediction = [item for item in dictForClass if modelPrediction[0] in item][0][0]
else:
prediction = "Error! Video {} has less than 30 frames".format(path[-1])
return prediction
def createClassLabelsDict():
"""Function to create the labels class dictionary
based on data generated from previous runs which will remain static"""
D = {'Prepositions': {'at': 0, 'by': 1, 'in': 2, 'with': 3},
'Numbers': {'eight': 0, 'five': 1, 'four': 2, 'nine': 3, 'one': 4,
'seven': 5, 'six': 6, 'three': 7, 'two': 8},
'Commands': {'bin': 0, 'lay': 1, 'place': 2, 'set': 3},
'Colors': {'blue': 0, 'green': 1, 'red': 2, 'white': 3},
'Alphabet': {'a': 0, 'b': 1, 'c': 2, 'd': 3, 'e': 4, 'f': 5, 'g': 6, 'h': 7,
'i': 8, 'j': 9, 'k': 10, 'l': 11, 'm': 12, 'n': 13, 'o': 14,
'p': 15, 'q': 16, 'r': 17, 's': 18, 't': 19, 'u': 20, 'v': 21,
'w': 22, 'x': 23, 'y': 24, 'z': 25},
'Adverb': {'again': 0, 'now': 1, 'please': 2, 'soon': 3}}
return D
def prototypeProject(receivedFiles, operationMode):
AllClassLabels = createClassLabelsDict()
mylist = glob.glob(receivedFiles)
mylist.sort(key=lambda x: x.split('_')[-1])
resultString = []
for video in mylist:
resultString.append(predictOneVideo(AllClassLabels, video, operationMode))
resultString = " ".join(resultString)
return resultString
if __name__ == "__main__":
start_time = time.time()
ModeOfOperation = 'SERVER' # Operates normally with the server.
if ModeOfOperation == 'SERVER':
receivedFilesFromServer = 'C:/Users/Amr Khaled/Desktop/Projects/Lipify-server/uploads/*.mp4'
predictionFilePath = 'C:/Users/Amr Khaled/Desktop/Projects/Lipify-server/prediction.txt'
else:
receivedFilesFromServer = "Prototype-Test-Videos/*.mp4"
predictionFilePath = 'Prototype-Test-Videos/Predictions.txt'
logFilePath = predictionFilePath.split('/')[:-1]
logFilePath = "/".join(logFilePath)
logging.basicConfig(filename=logFilePath + '/current run.log', filemode='w', level=logging.INFO)
log = logging.getLogger("my-logger")
log.info("App Start")
log.info("Mode Of Operation: {}".format(ModeOfOperation))
# AllClassLabels = getAllClassLabels() # from classLabels import getAllClassLabels()
result = prototypeProject(receivedFilesFromServer, ModeOfOperation)
predictionFile = open(predictionFilePath, "w") # write mode
predictionFile.write(result)
predictionFile.close()
print("Run Time: {} Seconds".format(time.time() - start_time))