-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgui.py
More file actions
59 lines (55 loc) · 2.6 KB
/
gui.py
File metadata and controls
59 lines (55 loc) · 2.6 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
from tensorflow.keras.models import load_model
import numpy as np
import cv2
import os
import gradio as gr
LABELS = os.listdir("../HandGesture/images")
model1 = load_model("./models/cnn_basic.keras")
model2 = load_model("./models/vgg16.hdf5")
model3 = load_model("./models/handgest_transfer4.hdf5")
model4 = load_model("./models/MobileNetV2.keras")
model5 = load_model("./models/CNN_with_dropout.keras")
def predict_image(image, select_model):
models = {
"CNN from scratch": model1,
"VGG16": model2,
"DenseNet201": model3,
"MobileNetV2": model4,
"CNN_with_dropout": model5
}
if select_model == "CNN from scratch" or select_model == "CNN_with_dropout":
images_label = {i: label for i, label in enumerate(LABELS)}
image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
image = cv2.resize(image, (195, 195))
image = image.astype(np.float32) / 255.
image = np.expand_dims(image, axis=0)
prediction = models[select_model].predict(image)
predicted_class = np.argmax(prediction, axis=1)
if np.max(prediction) > 0.8:
return images_label[predicted_class[0]]
else:
return "Not Related"
elif select_model == "VGG16" or select_model == "DenseNet201":
labels = ['scissor', 'thumbs', 'paper', 'rock', 'rock_on', 'fingers_crossed', 'call_me', 'up', 'okay', 'peace']
image = cv2.resize(image, (60, 60))
image = image.astype(np.float32) / 255.
image = np.expand_dims(image, axis=0)
prediction = models[select_model].predict(image)
predicted_class = np.argmax(prediction, axis=1)
if np.max(prediction) > 0.7:
return labels[predicted_class[0]]
else:
return "Not Related"
elif select_model == "MobileNetV2":
labels = ['peace', 'rock_on', 'fingers_crossed', 'up', 'thumbs', 'rock', 'scissor', 'paper', 'okay', 'call_me']
image = cv2.resize(image, (195, 195))
image = image.astype(np.float32) / 255.
image = np.expand_dims(image, axis=0)
prediction = models[select_model].predict(image)
predicted_class = np.argmax(prediction, axis=1)
if np.max(prediction) > 0.8:
return labels[predicted_class[0]]
else:
return "Not Related"
iface = gr.Interface(fn=predict_image, inputs=[gr.Image(), gr.Dropdown(choices=["CNN from scratch", "VGG16", "DenseNet201", "MobileNetV2", "CNN_with_dropout"], label="Select Model")], outputs="label", title="Hand Gesture Detection", example_labels=LABELS)
iface.launch()