-
Notifications
You must be signed in to change notification settings - Fork 2
/
webcam_detect.py
105 lines (78 loc) · 2.73 KB
/
webcam_detect.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
import cv2
import numpy as np
import tensorflow as tf
from PIL import Image
import os
TFLITE_PATH: str = os.path.join(os.path.dirname(os.path.realpath(__file__)), "models", "model_mobilenet_v2.tflite")
IMAGE_SIZE: tuple[int, int] = (160, 160)
CLASS_NAMES: list[str] = [
"A", "B", "C", "D", "E",
"F", "G", "H", "I", "J",
"K", "L", "M", "N", "O",
"P", "Q", "R", "S", "T",
"U", "V", "W", "X", "Y",
"Z", "del", "space",
]
TARGET_FRAME_COUNT: int = 3
TARGET_CONSECUTIVE_PREDICTIONS: int = 4
TARGET_PREDICTION_SCORE: float = 0.92
def load_model():
interpreter = tf.lite.Interpreter(model_path=TFLITE_PATH)
# print(interpreter.get_signature_list())
classify_lite = interpreter.get_signature_runner("serving_default")
return classify_lite
def get_image_array(image_data):
img_array = tf.keras.utils.img_to_array(image_data)
img_array = tf.expand_dims(img_array, 0) # Create a batch
return img_array
def predict(classify_lite, image_array):
score_lite = classify_lite(input_2=image_array)["outputs"]
predicted_char = CLASS_NAMES[np.argmax(score_lite)]
prediction_score = np.max(score_lite)
return predicted_char, prediction_score
def max_predicted(predictions: dict[str, int]) -> tuple[str, int]:
return max(predictions.items(), key=lambda k: k[1])
def sign_detection(img):
classify_lite = load_model()
x1, y1 = 100, 100
x2, y2 = (x1 + IMAGE_SIZE[0]), (y1 + IMAGE_SIZE[1])
previous_predictions: dict[str, int] = {letter: 0 for letter in CLASS_NAMES}
text: str = ""
img = cv2.flip(img, 1)
predicted_char: str = ""
prediction_score: float = 0.0
img_cropped = img[y1:y2, x1:x2]
image_data = Image.fromarray(img_cropped)
image_array = get_image_array(image_data)
predicted_char, prediction_score = predict(classify_lite, image_array)
if (prediction_score >= TARGET_PREDICTION_SCORE):
previous_predictions[predicted_char] += 1
letter, count = max_predicted(previous_predictions)
if (count >= TARGET_CONSECUTIVE_PREDICTIONS):
previous_predictions = {letter: 0 for letter in CLASS_NAMES}
if letter == "space":
text += " "
elif letter == "del":
text = text[:-1]
else:
text += letter
cv2.putText(
img,
predicted_char.upper(),
(5, 100),
cv2.FONT_HERSHEY_SIMPLEX,
4,
(255, 255, 255),
4,
)
cv2.putText(
img,
f"(score = {prediction_score:.2f})",
(50, 70),
cv2.FONT_HERSHEY_SIMPLEX,
1,
(255, 255, 255),
)
cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 0), 2)
cv2.imshow("Video Capture", img)
return img, predicted_char, str(prediction_score)