-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathk-fold_change_person.py
More file actions
179 lines (137 loc) · 4.74 KB
/
Copy pathk-fold_change_person.py
File metadata and controls
179 lines (137 loc) · 4.74 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
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
import os
import pandas as pd
import json
import shutil
import cv2
class body_part:
def __init__(self, x, y, v):
self.x = x
self.y = y
self.v = v
def df_process(part):
if "NAN" in part:
return 0, 0, 0
else:
part = part.replace("'","")
part = part.replace("(","")
part = part.replace(")","")
part_x, part_y = part.split(',')
return int(float(part_x)), int(float(part_y)), 2
who = ""
"""
test_fold = os.listdir("data/test/images")
for test_img in test_fold:
shutil.move(f"data/test/images/{test_img}", "data/train/images/{test_img}")
"""
df = pd.read_csv("label/label.csv")
image_train = []
annotation_train=[]
image_test = []
annotation_test = []
count = 0
img_fold = os.listdir("data/train/images")
for img in img_fold:
n_jpg = img.split('.')[0]
mode = "train"
"""
if who in img_fold:
shutil.move(f"data/train/images/{img}", "data/test/images/{img}")
mode = "test"
"""
target = df[df['file_name']== n_jpg]
img2 = cv2.imread(f"data/{mode}/images/{img}")
h, w, _ = img2.shape
image_temp = {
'file_name': f'{img}',
'height': h,
'width': w,
'id': count
}
if mode == "train":
image_train.append(image_temp)
else:
image_test.append(image_temp)
if "front" in img:
front = True
else:
front = False
with open(f'label/predictions/{n_jpg}.json', 'r') as file:
json_data = json.load(file)
j_data = json_data[0]
keypoint_temp = []
pose = img.split('_')[1]
for i in range(17):
for j in range(3):
if j != 2:
keypoint_temp.append(j_data["keypoints"][i][j])
else:
if (int(j_data["keypoints"][i][0])== 0) and (int(j_data["keypoints"][i][1])== 0):
keypoint_temp.append(0)
elif not front:
if i in [2, 4, 6, 12]:
keypoint_temp.append(1)
else:
if (pose in ["1", "2", "3", "8"]) and (i in [14, 16]):
keypoint_temp.append(1)
elif (pose in ["5", "6", "9"] and i == 16):
keypoint_temp.append(1)
else:
keypoint_temp.append(2)
else:
keypoint_temp.append(2)
x, y, v = df_process(target['l_finger'].iloc[0])
left_finger = body_part(x, y, v)
x, y, v = df_process(target['r_finger'].iloc[0])
right_finger = body_part(x, y, v)
x, y, v = df_process(target['l_toe'].iloc[0])
left_toe = body_part(x, y, v)
x, y, v = df_process(target['r_toe'].iloc[0])
right_toe = body_part(x, y, v)
x, y, v = df_process(target['l_rib'].iloc[0])
left_rib = body_part(x, y, v)
x, y, v = df_process(target['r_rib'].iloc[0])
right_rib = body_part(x, y, v)
keypoint_temp.append(left_finger.x)
keypoint_temp.append(left_finger.y)
keypoint_temp.append(left_finger.v)
keypoint_temp.append(right_finger.x)
keypoint_temp.append(right_finger.y)
keypoint_temp.append(right_finger.v)
keypoint_temp.append(left_rib.x)
keypoint_temp.append(left_rib.y)
keypoint_temp.append(left_rib.v)
keypoint_temp.append(right_rib.x)
keypoint_temp.append(right_rib.y)
keypoint_temp.append(right_rib.v)
keypoint_temp.append(left_toe.x)
keypoint_temp.append(left_toe.y)
keypoint_temp.append(left_toe.v)
keypoint_temp.append(right_toe.x)
keypoint_temp.append(right_toe.y)
keypoint_temp.append(right_toe.v)
annotation_temp ={
'keypoints': keypoint_temp,
'num_keypoints': 23,
'area': (j_data["bbox"][0][2]-j_data["bbox"][0][0])*(j_data["bbox"][0][3]*j_data["bbox"][0][1]),
'iscrowed': 0,
'image_id': count,
'bbox': j_data["bbox"][0],
'category_id': 0,
'id': count
}
if mode == "train":
annotation_train.append(annotation_temp)
else:
annotation_test.append(annotation_temp)
count = count + 1
print(f"{' ' * 30}\r{count}/{len(img_fold)}", end='\r', flush=True)
data_train = {'images': image_train,
'annotations': annotation_train,
'categories': [{'id': 1, 'name': 'person'}]}
data_test = {'images': image_test,
'annotations': annotation_test,
'categories': [{'id': 1, 'name': 'person'}]}
with open ("data/annotations/annotation_train.json", 'w') as json_file:
json.dump(data_train, json_file)
with open ("data/annotations/annotation_test.json", 'w') as json_file:
json.dump(data_test, json_file)