-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathannotation_generation.py
178 lines (149 loc) · 6.5 KB
/
annotation_generation.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
# from frame_process import FrameProcessor
from config import config as config
import os
import cv2
from src.human_detection import HumanDetector
from util.visualize import Visualizer
import json
image_ext = ["jpg", "jpeg", "webp", "bmp", "png", "JPG"]
video_ext = ["mp4", "mov", "avi", "mkv", "MP4"]
fourcc = cv2.VideoWriter_fourcc(*'XVID')
fps = 12
save_size = config.out_size
show_size = config.show_size
show = True
class AnnotationJsonGenerator:
def __init__(self, json_name):
if not json_name:
json_name = "result.json"
self.file = open(json_name, "w")
self.save_image = 0
self.image_count = 0
self.anno_count = 0
self.JsonData = {}
self.JsonData["info"] = {}
self.JsonData["licenses"] = []
self.images=[]
self.JsonData["images"] = self.images
self.JsonData["annotations"] = []
self.JsonData["categories"] = []
Category = {"supercategory": "person",
"id": 1,
"name": "person",
"keypoints": [
"nose","left_eye","right_eye","left_ear","right_ear",
"left_shoulder","right_shoulder","left_elbow","right_elbow",
"left_wrist","right_wrist","left_hip","right_hip",
"left_knee","right_knee","left_ankle","right_ankle"
],
"skeleton": [
[16,14],[14,12],[17,15],[15,13],[12,13],[6,12],[7,13],[6,7],
[6,8],[7,9],[8,10],[9,11],[2,3],[1,2],[1,3],[2,4],[3,5],[4,6],[5,7]
]
}
self.JsonData["categories"].append(Category)
self.base_kps = 17
def update(self, idx, boxes, kps, kps_scores, name, img_h, img_w):
image = {}
image["file_name"] = name
image["id"] = self.image_count
image["height"] = img_h
image["width"] = img_w
self.images.append(image)
# If no bounding boxes, do not save frame
# item = {}
if len(idx) == 0:
self.save_image = 0
# return
else:
self.save_image = 1
idx, boxes, kps, kps_scores = idx.tolist(), boxes.tolist(), kps.tolist(), kps_scores.tolist()
for i, box, kp, kp_score in zip(idx, boxes, kps, kps_scores):
item = {}
remaining_kps = self.base_kps - len(kp)
head_kp = kp[0]
for _ in range(remaining_kps):
kp.insert(1, head_kp)
bw, bh = box[2] - box[0], box[3] - box[1]
item["bbox"] = [box[0], box[1], bw, bh]
keypoints =[]
kp_num = 0
for Keypoint in kp:
Keypoint[0] = Keypoint[0]
Keypoint[1] = Keypoint[1]
keypoints.extend(Keypoint)
if 0 < kp_num < 5:
keypoints.extend([0])
else:
keypoints.extend([2])
kp_num += 1
item["keypoints"] = keypoints
item["id"] = self.anno_count
self.anno_count += 1
item["area"] = box[2] * box[3]
item["category_id"] = 1
item["iscrowd"] = 0
item["num_keypoints"] = 17
item["image_id"] = self.image_count
self.JsonData["annotations"].append(item)
self.image_count += 1
def release(self):
self.file.write(json.dumps(self.JsonData, indent=2))
class Demo:
def __init__(self, input_src, output_src):
self.FP = FrameProcessor()
self.input = input_src
self.output = output_src
self.show = show
self.save_size = save_size
self.show_size = show_size
if os.path.isdir(self.input):
self.demo_type = "image_folder"
self.input_imgs = [os.path.join(self.input, file_name) for file_name in os.listdir(self.input)]
if self.output:
assert os.path.isdir(self.output), "The output should be a folder when the input is a folder!"
os.makedirs(self.output, exist_ok=True)
self.output_imgs = [os.path.join(self.output, file_name) for file_name in os.listdir(self.input)]
else:
raise ValueError("Unrecognized src: {}".format(self.input))
def run(self):
if self.demo_type == "image_folder":
for idx, img_name in enumerate(self.input_imgs):
frame = cv2.imread(img_name)
frame = self.FP.process(frame, img_name)
if self.show:
cv2.imshow("result", cv2.resize(frame, self.show_size))
cv2.waitKey(1)
if self.output:
cv2.imwrite(self.output_imgs[idx], cv2.resize(frame, self.save_size))
self.FP.release()
else:
raise ValueError
detector_cfg = "/media/hkuit164/Backup/2324_data/checkpoint/yolo_rgb/yolov3-1cls.cfg"
detector_weight = "/media/hkuit164/Backup/2324_data/checkpoint/yolo_rgb/last.pt"
detector_label = ""
pose_weight = "/media/hkuit164/Backup/2324_data/checkpoint/pose_rgb/latest.pth"
pose_model_cfg = ""
pose_data_cfg = ""
deepsort_weight = ""
sort_type = "sort"
class FrameProcessor:
def __init__(self, json_path="/media/hkuit164/Backup/xjl/hh_video_data/cut_video_selected/cor_total_selected_pretrain.json"):
self.HP = HumanDetector(detector_cfg, detector_weight, pose_weight, pose_model_cfg,
pose_data_cfg, "sort", "", "", "", "", "", debug=False, device="cpu")
self.Json = AnnotationJsonGenerator(json_path)
self.visualizer = Visualizer(self.HP.estimator.kps, det_label=detector_label)
def process(self, frame, name):
ids, boxes, boxes_cls, kps, kps_scores = self.HP.process(frame, print_time=True)
frame = self.visualizer.visualize(frame, ids, boxes, boxes_cls, kps, kps_scores)
self.HP.init_trackers()
self.Json.update(ids, boxes, kps, kps_scores, name.split("/")[-1], frame.shape[0], frame.shape[1])
return frame
def release(self):
self.Json.release()
if __name__ == '__main__':
# import config as config
input_src = "/media/hkuit164/Backup/xjl/hh_video_data/cut_video_selected/cor_total_selected"
output_src = "/media/hkuit164/Backup/xjl/hh_video_data/cut_video_selected/cor_total_selected_pretrain_output"
demo = Demo(input_src, output_src)
demo.run()