-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathdemo.py
150 lines (121 loc) · 6.42 KB
/
demo.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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
import os
import mediapipe as mp
import cv2
import numpy as np
import tensorflow as tf
model = tf.keras.models.load_model("./VGG_Naruto_Model2")
LENIENCY = 100
NUM_FRAMES = 0
cap = cv2.VideoCapture(0)
mpHands = mp.solutions.hands
hands = mpHands.Hands()
mpDraw = mp.solutions.drawing_utils
# Reading in label images
label_images = {}
for img_name in os.listdir('images'):
label_images.update({img_name[:-4]: cv2.imread(f'images/{img_name}')})
while True:
success, image = cap.read()
image = cv2.flip(image,1)
try:
imageRGB = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
except:
continue
results = hands.process(imageRGB)
# checking whether a hand is detected
if results.multi_hand_landmarks:
xs = []
ys = []
for handLms in results.multi_hand_landmarks: # working with each hand
for id, lm in enumerate(handLms.landmark):
h, w, c = image.shape
cx, cy = int(lm.x * w), int(lm.y * h)
xs.append(cx)
ys.append(cy)
#mpDraw.draw_landmarks(image, handLms, mpHands.HAND_CONNECTIONS)
if len(xs) and len(ys):
ma_x = max([0, max(xs) + LENIENCY])
ma_y = max([0, max(ys) + LENIENCY])
mi_x = max([0, min(xs) - LENIENCY])
mi_y = max([0, min(ys) - LENIENCY])
image = cv2.rectangle(img=image, pt1=(mi_x, mi_y), pt2=(ma_x, ma_y), color=(0, 0, 255), thickness=2)
cropped = image[mi_y:ma_y, mi_x:ma_x]
cropped = cv2.cvtColor(cropped, cv2.COLOR_BGR2GRAY)
# thresholding
tf_img = tf.image.convert_image_dtype(cropped, tf.dtypes.uint8)
#gray = tf.squeeze(tf_img,2)
gray = tf_img
#thresholded = otsu_thresholding(gray)
cropped = cv2.cvtColor(gray.numpy().astype(np.uint8), cv2.COLOR_GRAY2BGR)
image[mi_y:ma_y, mi_x:ma_x] = cropped
# # # # # # # # # # # # # # #
# Predictions Here #
# # # # # # # # # # # # # # #
labels = [
'bird',
'boar',
'dog',
'dragon',
'hare',
'horse',
'monkey',
'ox',
'ram',
'rat',
'serpent',
'tiger'
]
x = cropped
x = cv2.resize(x, (224,224))
x = tf.image.convert_image_dtype(x, tf.dtypes.uint8)
x = tf.expand_dims(x, 0)
pred = model.predict(x)[0]
confidences = dict()
for k in label_images.keys():
confidences.update({k: 0})
for i, p in enumerate(pred):
confidences[labels[i]] = int(p*100)
# # # # # # # # # # # # # # #
# Ends Here #
# # # # # # # # # # # # # # #
#image = image[mi_y:ma_y, mi_x:ma_x]
else:
confidences = dict()
for k in label_images.keys():
confidences.update({k: 0})
NUM_FRAMES += 1
colors = dict()
for k in confidences.keys():
if confidences[k] > 50:
colors.update({k: (0, 0, 255)})
else:
colors.update({k: (0, 0, 0)})
h, w, c = image.shape
b_shift = int(h//6)
b_offset = 30
image[b_shift*0:b_shift*1, 0:b_shift*1] = cv2.resize(label_images['bird'], (b_shift, b_shift))
image[b_shift*1:b_shift*2, 0:b_shift*1] = cv2.resize(label_images['boar'], (b_shift, b_shift))
image[b_shift*2:b_shift*3, 0:b_shift*1] = cv2.resize(label_images['dog'], (b_shift, b_shift))
image[b_shift*3:b_shift*4, 0:b_shift*1] = cv2.resize(label_images['dragon'], (b_shift, b_shift))
image[b_shift*4:b_shift*5, 0:b_shift*1] = cv2.resize(label_images['hare'], (b_shift, b_shift))
image[b_shift*5:b_shift*6, 0:b_shift*1] = cv2.resize(label_images['horse'], (b_shift, b_shift))
cv2.putText(image, str(confidences['bird']), (b_shift, b_shift*1-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['bird'], thickness=3)
cv2.putText(image, str(confidences['boar']), (b_shift, b_shift*2-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['boar'], thickness=3)
cv2.putText(image, str(confidences['dog']), (b_shift, b_shift*3-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['dog'], thickness=3)
cv2.putText(image, str(confidences['dragon']), (b_shift, b_shift*4-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['dragon'], thickness=3)
cv2.putText(image, str(confidences['hare']), (b_shift, b_shift*5-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['hare'], thickness=3)
cv2.putText(image, str(confidences['horse']), (b_shift, b_shift*6-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['horse'], thickness=3)
image[b_shift*0:b_shift*1, w-b_shift:w] = cv2.resize(label_images['monkey'], (b_shift, b_shift))
image[b_shift*1:b_shift*2, w-b_shift:w] = cv2.resize(label_images['ox'], (b_shift, b_shift))
image[b_shift*2:b_shift*3, w-b_shift:w] = cv2.resize(label_images['ram'], (b_shift, b_shift))
image[b_shift*3:b_shift*4, w-b_shift:w] = cv2.resize(label_images['rat'], (b_shift, b_shift))
image[b_shift*4:b_shift*5, w-b_shift:w] = cv2.resize(label_images['serpent'], (b_shift, b_shift))
image[b_shift*5:b_shift*6, w-b_shift:w] = cv2.resize(label_images['tiger'], (b_shift, b_shift))
cv2.putText(image, str(confidences['monkey']), (w-(b_shift+b_offset*4), b_shift*1-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['monkey'], thickness=3)
cv2.putText(image, str(confidences['ox']), (w-(b_shift+b_offset*4), b_shift*2-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['ox'], thickness=3)
cv2.putText(image, str(confidences['ram']), (w-(b_shift+b_offset*4), b_shift*3-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['ram'], thickness=3)
cv2.putText(image, str(confidences['rat']), (w-(b_shift+b_offset*4), b_shift*4-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['rat'], thickness=3)
cv2.putText(image, str(confidences['serpent']), (w-(b_shift+b_offset*4), b_shift*5-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['serpent'], thickness=3)
cv2.putText(image, str(confidences['tiger']), (w-(b_shift+b_offset*4), b_shift*6-b_offset), cv2.FONT_HERSHEY_SIMPLEX, fontScale=2, color=colors['tiger'], thickness=3)
cv2.imshow("Output", image)
cv2.waitKey(1)