-
Notifications
You must be signed in to change notification settings - Fork 0
/
inference.py
executable file
·210 lines (155 loc) · 5.59 KB
/
inference.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import numpy as np
import matplotlib.pyplot as plt
from PIL import Image
import torch.nn as nn
import torch
import torch.nn.functional as F
from torchvision import models, transforms as T
import cv2
import math
from PIL import ImageDraw
from models import KeyPointDetectionModel, HandSegmentationModel
IMG_SIZE = 256
def determine_max(rect):
x_min = math.inf
x_max = 0
y_min = math.inf
y_max = 0
for i in rect:
if i[0] < x_min:
x_min = i[0]
if i[0] > x_max:
x_max = i[0]
if i[1] < y_min:
y_min = i[1]
if i[1] > y_max:
y_max = i[1]
# return ((x_min,y_min),(x_max,y_max))
return ((x_min, y_min), (x_max, y_max))
def find_contour(contours):
max_index = None
max_area = None
index = 0
for cnt in contours:
coordinates = determine_max(cnt.reshape([-1, 2]))
area = (coordinates[0][0] - coordinates[0][1]) * (
coordinates[1][0] - coordinates[1][1]
)
if max_area is None:
max_area = area
max_index = index
elif area > max_area:
max_area = area
max_index = index
index += 1
return max_index
def detect_keypoints(predictions):
ige = Image.new("RGB", (200, 200), "black")
draw = ImageDraw.Draw(ige)
for i in range(0, 21):
print("\n")
print("\n")
contours1, hierarchy = cv2.findContours(
predictions[i], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
cnt1 = None
if len(contours1):
cnt_index = find_contour(contours1)
if cnt_index is not None:
cnt1 = contours1[cnt_index]
if cnt1 is not None:
coordinates = determine_max(cnt1.reshape([-1, 2]))
draw.ellipse(
(
coordinates[0][0],
coordinates[0][1],
coordinates[0][0] + 12,
coordinates[0][1] + 12,
),
fill=(255, 255, 255),
)
contours2, hierarchy = cv2.findContours(
predictions[(i + 1) % 21], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
cnt2 = None
if len(contours2):
cnt_index = find_contour(contours2)
if cnt_index is not None:
cnt2 = contours2[cnt_index]
if (cnt1 is not None) and (cnt2 is not None):
coordinates1 = determine_max(cnt1.reshape([-1, 2]))
coordinates2 = determine_max(cnt2.reshape([-1, 2]))
draw.line(
(
coordinates1[0][0],
coordinates1[0][1],
coordinates2[0][0] + 5,
coordinates2[0][1] + 5,
),
fill=(255, 255, 255),
)
plt.imshow(ige)
plt.show()
def perform_inference():
from torchvision import transforms
model = HandSegmentationModel().eval().float()
model.load_state_dict(torch.load("model_256_resnet.pth"))
key_model = KeyPointDetectionModel()
key_model.load_state_dict(torch.load("model_200_keypoint_30.pth"))
key_model.eval()
normalize = transforms.Normalize(
mean=(0.485, 0.456, 0.406), std=(0.229, 0.224, 0.225)
)
transform = T.Compose([T.ToTensor(), normalize])
cap = cv2.VideoCapture(0)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Our operations on the frame come here
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
frame = np.asarray(Image.fromarray(frame).resize((IMG_SIZE, IMG_SIZE)))
out = model(transform(frame).view([1, 3, IMG_SIZE, IMG_SIZE]).float())
print(torch.max(out))
out = (out.view([IMG_SIZE, IMG_SIZE]).detach().numpy() >= 0.5).astype(np.uint8)
plt.imshow(out)
plt.show()
contours, hierarchy = cv2.findContours(
out, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
for cnt in contours:
coordinates = determine_max(cnt.reshape([-1, 2]))
area = (coordinates[1][0] - coordinates[0][0]) * (
coordinates[1][1] - coordinates[0][1]
)
print(area)
offset = 30
if area >= 10:
new_frame = Image.fromarray(frame)
new_frame = new_frame.crop(
(
coordinates[0][0] - offset,
coordinates[0][1] - offset,
coordinates[1][0] + offset,
coordinates[1][1] + offset,
)
)
new_frame = new_frame.resize((200, 200))
preds = (
key_model(transform(new_frame).view([-1, 3, 200, 200]))
.detach()
.numpy()
.reshape((21, 200, 200))
)
preds = np.asarray(preds.reshape((21, 200, 200)) > 0.5).astype(np.uint8)
contours1, hierarchy = cv2.findContours(
preds[0], cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE
)
plt.imshow(new_frame)
plt.show()
detect_keypoints(preds)
if cv2.waitKey(1) & 0xFF == ord("q"):
break
# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
perform_inference()