Skip to content

Commit 0810190

Browse files
authored
Add files via upload
1 parent 4584984 commit 0810190

File tree

4 files changed

+184
-0
lines changed

4 files changed

+184
-0
lines changed

1. utils/ext_frame.py

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import os
2+
import cv2
3+
4+
modes = ['train', 'validation']
5+
basic_path = '../../dataset/AIC21_Track3/'
6+
7+
for mode in modes:
8+
# Data Path
9+
data_path = basic_path + mode + '/'
10+
11+
# Extract frame
12+
scenes = os.listdir(data_path)
13+
for scene in scenes:
14+
cams = os.listdir(data_path + scene)
15+
for cam in cams:
16+
# Set paths
17+
cam_path = data_path + scene + '/' + cam + '/'
18+
video_path, frame_path = cam_path + 'vdo.avi', cam_path + 'frame/'
19+
20+
# Create frame directory
21+
if not os.path.exists(frame_path):
22+
os.mkdir(frame_path)
23+
24+
# Read video
25+
video = cv2.VideoCapture(video_path)
26+
27+
# Save each frame
28+
f_num = 1
29+
while 1:
30+
success, image = video.read()
31+
if not success:
32+
break
33+
cv2.imwrite(frame_path + '%s_f%04d.jpg' % (cam, f_num), image)
34+
f_num += 1
35+
36+
# Print current status
37+
print('%s_%s Finished' % (scene, cam))

1. utils/gen_te_det_info.py

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
import os
2+
import cv2
3+
import copy
4+
import pickle
5+
import numpy as np
6+
import img2gps as img2gps
7+
import pre_process as pre_process
8+
9+
# Set
10+
basic_path = '../../dataset/AIC21_Track3/test/S06/'
11+
12+
# Visualize and save
13+
det_info = {'S06': {}}
14+
cams = os.listdir(basic_path)
15+
for cam in cams:
16+
# Newly add
17+
if cam not in det_info.keys():
18+
det_info['S06'][cam] = {}
19+
20+
# Open detection file
21+
det_file_path = basic_path + '/%s/det/det_mask_rcnn.txt' % cam
22+
det_file = np.loadtxt(det_file_path, dtype=np.float32, delimiter=',').reshape(-1, 10)
23+
24+
# Read RoI
25+
roi = cv2.imread(basic_path + cam + '/roi.jpg').astype('float32')[:, :, 0] / 255
26+
img_h, img_w = roi.shape[0], roi.shape[1]
27+
28+
# Filter with objectiveness score and size
29+
det_file = det_file[det_file[:, 6] >= 0.2, :]
30+
det_file = det_file[det_file[:, 4] * det_file[:, 5] >= 660, :]
31+
32+
# For each frame
33+
for f_num in range(1, int(np.max(det_file[:, 0] + 1))):
34+
# Newly add
35+
if f_num not in det_info['S06'][cam].keys():
36+
det_info['S06'][cam][f_num] = []
37+
38+
# Select detection results
39+
det_results = copy.deepcopy(det_file[det_file[:, 0] == f_num, :])
40+
41+
# Pre-process with IoU
42+
det_results = det_results[:, 2:7]
43+
det_results = [det_result for det_result in det_results]
44+
det_results = pre_process.merge_boxes(det_results)
45+
46+
# Save each bbox
47+
for det_result in det_results:
48+
# Read result
49+
left, top, w, h, score = det_result
50+
51+
# Calculate gps
52+
gps = img2gps.to_gps_coord('test', 'S06', cam, [left, top, w, h])
53+
54+
# Calculate info
55+
x, y = left + w * 0.5, top + h * 0.8
56+
57+
# Save info
58+
save_info = ['S06', cam, f_num, 0, left, top, w, h, img_w, img_h, x, y, gps[0], gps[1], score]
59+
det_info['S06'][cam][f_num].append(save_info)
60+
61+
# print current status
62+
print('S06_%s Finished' % cam)
63+
64+
65+
# Save save_info
66+
with open('../outputs/1. det/mask_rcnn_0.2/det_small.pickle', 'wb') as f:
67+
pickle.dump(det_info, f, pickle.HIGHEST_PROTOCOL)

1. utils/img2gps.py

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import numpy as np
2+
from numpy.linalg import inv
3+
4+
5+
def to_gps_coord(mode, scene, cam, bbox):
6+
# Read calibration information
7+
cal_file_path = '../../dataset/AIC21_Track3/%s/%s/%s/calibration.txt' % (mode, scene, cam)
8+
cal_file = open(cal_file_path, 'r')
9+
cal_lines = cal_file.readlines()
10+
11+
# Create Homography matrix
12+
H = cal_lines[0].split(':')[-1]
13+
H = H.replace(';', ' ').split()
14+
H = [float(i) for i in H]
15+
H = np.reshape(H, (3, 3))
16+
H = inv(H)
17+
18+
# Transform
19+
left, top, w, h = bbox
20+
center_pt = np.asarray([bbox[0] + 0.5 * w, bbox[1] + 0.5 * h, 1])
21+
center_pt = np.reshape(center_pt, (3, 1))
22+
gps_pt = np.matmul(H, center_pt)
23+
gps_pt = gps_pt / gps_pt[-1]
24+
25+
# Close
26+
cal_file.close()
27+
28+
return gps_pt[:2].squeeze()

1. utils/pre_process.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import numpy as np
2+
3+
4+
def calc_iou(a, b):
5+
# Decode information
6+
a_left, a_top, a_w, a_h, _ = a
7+
b_left, b_top, b_w, b_h, _ = b
8+
9+
# Calculate iou
10+
left, top = max(a_left, b_left), max(a_top, b_top)
11+
right, bot = min(a_left + a_w, b_left + b_w), min(a_top + a_h, b_top + b_h)
12+
inter = max(0, right - left) * max(0, bot - top)
13+
14+
# Form
15+
iou = np.zeros((3,))
16+
iou[0] = inter / (a_w * a_h + b_w * b_h - inter)
17+
iou[1] = inter / (a_w * a_h)
18+
iou[2] = inter / (b_w * b_h)
19+
20+
return iou
21+
22+
23+
def check_iou(a_boxes, b_boxes):
24+
# Check iou between boxes 'a' and boxes 'b'
25+
iou = np.zeros((len(a_boxes), len(b_boxes), 3))
26+
for idx, a in enumerate(a_boxes):
27+
for jdx, b in enumerate(b_boxes):
28+
iou[idx, jdx, :] = calc_iou(a, b)
29+
return iou
30+
31+
32+
# Filter over detected boxes
33+
def merge_boxes(boxes):
34+
while True:
35+
# Check connectivity of boxes
36+
iou = check_iou(boxes, boxes)
37+
con = 0.5 <= iou[:, :, 0]
38+
con += 0.66 <= iou[:, :, 1]
39+
con += 0.66 <= iou[:, :, 2]
40+
con = 1 <= con
41+
42+
# End condition
43+
if np.sum(con) == len(boxes):
44+
break
45+
46+
# Pick idx to delete
47+
del_idx_can = np.where(np.sum(con, axis=0) >= 2)[0]
48+
scores = [boxes[d_idx_c][-1] for d_idx_c in del_idx_can]
49+
del_idx = del_idx_can[scores.index(min(scores))]
50+
boxes.pop(del_idx)
51+
52+
return boxes

0 commit comments

Comments
 (0)