-
Notifications
You must be signed in to change notification settings - Fork 0
/
plot_keypoints.py
170 lines (145 loc) · 5.66 KB
/
plot_keypoints.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
"""
Process CMU Hand dataset to get cropped hand datasets.
"""
import os
import numpy as np
import json
from PIL import Image
from tqdm import tqdm
import glob
import cv2
from matplotlib import pyplot as plt
size_x = 256
size_y = 256
train_labels=[]
for label_dir in tqdm(sorted(glob.glob("hand_labels/train/label/*.json")),total=1912):
# label_dir = 'hand_labels/test/label/' + img[:-4] + '.json'
dat = json.load(open(label_dir))
pts = np.array(dat['hand_pts'])
lbl = pts[:, :2]
train_labels.append(lbl)
#lbl = lbl.tolist()
train_labels = np.array(train_labels)#float64 dont change
train_labels = train_labels.reshape(1912,-1)
train_images = []
for img_path in tqdm(sorted(glob.glob("hand_labels/train/data/*.jpg")), total=1912):
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
#img = cv2.resize(img, (size_x, size_y))
train_images.append(img)
"convert list to np array for ml processing"
train_images = np.array(train_images) # dtype:uint8
train_labels_crop=[]
for label_dir in tqdm(sorted(glob.glob("hand_labels/train/label/*.json")),total=1912):
# label_dir = 'hand_labels/test/label/' + img[:-4] + '.json'
dat = json.load(open(label_dir))
pts = np.array(dat['hand_pts'])
xmin = min(pts[:, 0])
xmax = max(pts[:, 0])
ymin = min(pts[:, 1])
ymax = max(pts[:, 1])
B = max(xmax - xmin, ymax - ymin)
# B is the maximum dimension of the tightest bounding box
width = 2.2 * B # This is based on the paper
# the center of hand box can be
center = dat["hand_box_center"]
hand_box = [[center[0] - width / 2., center[1] - width / 2.],
[center[0] + width / 2., center[1] + width / 2.]]
hand_box = np.array(hand_box)
lbl = pts[:, :2] - hand_box[0, :]
lbl = lbl * 256 / width
train_labels_crop.append(lbl)
#lbl = lbl.tolist()
train_labels_crop = np.array(train_labels_crop)#float64 dont change
train_labels_crop = train_labels_crop.reshape(1912,-1)
train_images_crop = []
for img_path in tqdm(sorted(glob.glob("hand_labels/train/crop/*.jpg")), total=1912):
img = cv2.imread(img_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = cv2.resize(img, (size_x, size_y))
train_images_crop.append(img)
"convert list to np array for ml processing"
train_images_crop = np.array(train_images_crop) # dtype:uint8
# plots keypoints on face image
def plot_keypoints(img, points):
# display image
#plt.imshow(img, cmap='gray')
#plt.imshow(img)
#plt.imshow(np.float32(img), cmap='gray')
# plot the keypoints
for i in range(0, 42, 2):
#plt.scatter((points[i] + 0.5)*256, (points[i+1]+0.5)*256, color='red')
#plt.scatter(points[i], points[i + 1], color='red', s=1)
cv2.circle(img, (int(points[i]), int(points[i + 1])), 3, (0, 255, 0), thickness=-1) # , lineType=-1)#, shift=0)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#cv2.imshow("img",img)
cv2.imwrite("img.jpg", img)
#plt.show()
# plots keypoints on face image
def plot_keypoints2(img, points):
# display image
#plt.imshow(img, cmap='gray')
#plt.imshow(img)
#plt.imshow(np.float32(img), cmap='gray')
# plot the keypoints
for i in range(0, 42, 2):
#plt.scatter((points[i] + 0.5)*256, (points[i+1]+0.5)*256, color='red')
#plt.scatter(points[i], points[i + 1], color='red', s=1)
cv2.circle(img, (int(points[i]), int(points[i + 1])), 3, (0, 255, 0), thickness=-1) # , lineType=-1)#, shift=0)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#cv2.imshow("img",img)
cv2.imwrite("img_crop.jpg", img)
#plt.show()
# plots keypoints on face image
def plot_keypoints3(img, points):
# display image
#plt.imshow(img, cmap='gray')
#plt.imshow(img)
#plt.imshow(np.float32(img), cmap='gray')
# plot the keypoints
for i in range(0, 42, 2):
#plt.scatter((points[i] + 0.5)*256, (points[i+1]+0.5)*256, color='red')
#plt.scatter(points[i], points[i + 1], color='red', s=1)
cv2.circle(img, (int(points[i]), int(points[i + 1])), 3, (0, 255, 0), thickness=-1) # , lineType=-1)#, shift=0)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#cv2.imshow("img",img)
cv2.imwrite("img_flip.jpg", img)
#plt.show()
# plots keypoints on face image
def plot_keypoints4(img, points):
# display image
#plt.imshow(img, cmap='gray')
#plt.imshow(img)
#plt.imshow(np.float32(img), cmap='gray')
# plot the keypoints
for i in range(0, 42, 2):
#plt.scatter((points[i] + 0.5)*256, (points[i+1]+0.5)*256, color='red')
#plt.scatter(points[i], points[i + 1], color='red', s=1)
cv2.circle(img, (int(points[i]), int(points[i + 1])), 3, (0, 255, 0), thickness=-1) # , lineType=-1)#, shift=0)
img = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
#cv2.imshow("img",img)
cv2.imwrite("img_rot.jpg", img)
#plt.show()
# does data augmentation by flipping the image
def augment_data(img, points):
rows, cols, channel = img.shape
new_img = np.copy(img)
# flip the image
for i in range(256):
for j in range(128):
temp = img[i][j]
new_img[i][j] = img[i][cols - j - 1]
new_img[i][cols - j - 1] = temp
# flip the points
new_points = np.copy(points)
for i in range(0, 42, 2):
#new_points[i] = -points[i]
new_points[i] = 256-points[i]-1
#new_points[i] = 256 - points[i] # 1 pixel differnec
return new_img, new_points
id = 500
#train_images_flip, train_labels_flip = augment_data(train_images_crop[id], train_labels_crop[id])
#plot_keypoints(train_images_aug[id], train_labels_aug[id])
plot_keypoints(train_images[id], train_labels[id])
plot_keypoints2(train_images_crop[id], train_labels_crop[id])
a=1