forked from PaddlePaddle/PaddleVideo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
get_image_label.py
164 lines (158 loc) · 5.5 KB
/
get_image_label.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
import cv2
import os
import json
# please change it to your path
path = '/workspace/wangqingzhong/Anti_UAV'
annotation_path = 'annotations'
train_img_path = 'train_imgs'
val_img_path = 'val_imgs'
if not os.path.exists(annotation_path):
os.makedirs(annotation_path)
if not os.path.exists(train_img_path):
os.makedirs(train_img_path)
if not os.path.exists(val_img_path):
os.makedirs(val_img_path)
train_info = {
'images': [],
'type':
'instances',
'annotations': [],
'categories': [{
"supercategory": "none",
"id": 1,
"name": "drone"
}, {
"supercategory": "none",
"id": 2,
"name": "noise"
}]
}
val_info = {
'images': [],
'type':
'instances',
'annotations': [],
'categories': [{
"supercategory": "none",
"id": 1,
"name": "drone"
}, {
"supercategory": "none",
"id": 2,
"name": "noise"
}]
}
# you can change it
interval = 5
dirs = os.listdir(path)
train_img_id = 0
val_img_id = 0
for d in dirs:
if 'new' in d:
video_file = os.path.join(path, d, 'IR.mp4')
label_file = os.path.join(path, d, 'IR_label.json')
labels = json.load(open(label_file, 'r'))
exits = labels['exist']
gt_bbox = labels['gt_rect']
assert len(exits) == len(gt_bbox)
videocap = cv2.VideoCapture(video_file)
i = 0
while True:
success, frame = videocap.read()
if success:
if i % interval == 0:
img_name = d + '_' + str(i) + '.jpg'
cv2.imwrite(os.path.join(val_img_path, img_name), frame)
height, width, depth = frame.shape
x, y, w, h = gt_bbox[i]
isexist = exits[i]
if isexist:
category_id = 1
else:
category_id = 2
draw_frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
(0, 255, 0), 2)
img_name_draw = d + '_' + str(i) + 'draw.jpg'
cv2.imwrite(os.path.join(val_img_path, img_name_draw),
draw_frame)
img_info = {
'file_name': img_name,
'height': float(height),
'width': float(width),
'id': val_img_id
}
ann_info = {
'area': float(w) * float(h),
'iscrowd': 0,
'bbox': [float(x),
float(y),
float(w),
float(h)],
'category_id': category_id,
'ignore': 0,
'image_id': val_img_id,
'id': val_img_id + 1
}
val_info['images'].append(img_info)
val_info['annotations'].append(ann_info)
val_img_id += 1
i += 1
else:
print('finish {}'.format(d))
break
else:
video_file = os.path.join(path, d, 'IR.mp4')
label_file = os.path.join(path, d, 'IR_label.json')
labels = json.load(open(label_file, 'r'))
exits = labels['exist']
gt_bbox = labels['gt_rect']
assert len(exits) == len(gt_bbox)
videocap = cv2.VideoCapture(video_file)
i = 0
while True:
success, frame = videocap.read()
if success:
if i % interval == 0:
img_name = d + '_' + str(i) + '.jpg'
cv2.imwrite(os.path.join(train_img_path, img_name), frame)
height, width, depth = frame.shape
x, y, w, h = gt_bbox[i]
isexist = exits[i]
if isexist:
category_id = 1
else:
category_id = 2
draw_frame = cv2.rectangle(frame, (x, y), (x + w, y + h),
(0, 255, 0), 2)
img_name_draw = d + '_' + str(i) + 'draw.jpg'
cv2.imwrite(os.path.join(train_img_path, img_name_draw),
draw_frame)
img_info = {
'file_name': img_name,
'height': height,
'width': width,
'id': train_img_id
}
ann_info = {
'area': float(w) * float(h),
'iscrowd': 0,
'bbox': [float(x),
float(y),
float(w),
float(h)],
'category_id': category_id,
'ignore': 0,
'image_id': train_img_id,
'id': train_img_id + 1
}
train_info['images'].append(img_info)
train_info['annotations'].append(ann_info)
train_img_id += 1
i += 1
else:
print('finish {}'.format(d))
break
with open('annotations/train.json', 'w') as f:
json.dump(train_info, f)
with open('annotations/val.json', 'w') as f:
json.dump(val_info, f)