ZJF-Thunder
commited on
Commit
•
4c710ed
1
Parent(s):
e26e560
添加文件
Browse files- .gitignore +3 -3
- demo/1.mp4 +3 -0
- demo/2.mp4 +3 -0
- demo/27.jpg +3 -0
- demo/3.mp4 +3 -0
- demo/30.jpg +3 -0
- demo/31.jpg +3 -0
- demo/4.mp4 +3 -0
- demo/40.jpg +3 -0
- demo/6.jpg +3 -0
- demo/MMDet_Tutorial.ipynb +0 -0
- demo/create_result_gif.py +162 -0
- demo/demo.jpg +3 -0
- demo/demo.mp4 +3 -0
- demo/demo_27_result.jpg +3 -0
- demo/demo_27_result2.jpg +3 -0
- demo/demo_31_result2.jpg +3 -0
- demo/demo_40_result2.jpg +3 -0
- demo/demo_6_result.jpg +3 -0
- demo/demo_6_result2.jpg +3 -0
- demo/demo_result.jpg +3 -0
- demo/image_demo.py +26 -0
- demo/inference_demo.ipynb +0 -0
- demo/test.png +3 -0
- demo/video_demo.py +60 -0
- demo/webcam_demo.py +46 -0
.gitignore
CHANGED
@@ -20,7 +20,7 @@ parts/
|
|
20 |
sdist/
|
21 |
var/
|
22 |
wheels/
|
23 |
-
|
24 |
.installed.cfg
|
25 |
*.egg
|
26 |
MANIFEST
|
@@ -113,9 +113,9 @@ venv.bak/
|
|
113 |
*.pkl
|
114 |
*.pkl.json
|
115 |
*.log.json
|
116 |
-
work_dirs/
|
117 |
|
118 |
# Pytorch
|
119 |
-
|
120 |
*.py~
|
121 |
*.sh~
|
|
|
20 |
sdist/
|
21 |
var/
|
22 |
wheels/
|
23 |
+
#*.egg-info/
|
24 |
.installed.cfg
|
25 |
*.egg
|
26 |
MANIFEST
|
|
|
113 |
*.pkl
|
114 |
*.pkl.json
|
115 |
*.log.json
|
116 |
+
#work_dirs/
|
117 |
|
118 |
# Pytorch
|
119 |
+
#*.pth
|
120 |
*.py~
|
121 |
*.sh~
|
demo/1.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:de01ef2f627f1d9cb07fd3a4706c6263ba5cc01c31780875994d5e08768aac4a
|
3 |
+
size 3414636
|
demo/2.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:de01ef2f627f1d9cb07fd3a4706c6263ba5cc01c31780875994d5e08768aac4a
|
3 |
+
size 3414636
|
demo/27.jpg
ADDED
Git LFS Details
|
demo/3.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:8c7b5c369303a095a28cc6c63fd7ea275135fd3636db8f30bae038548b74c000
|
3 |
+
size 2833382
|
demo/30.jpg
ADDED
Git LFS Details
|
demo/31.jpg
ADDED
Git LFS Details
|
demo/4.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:2c6349db8e2ca67d4f480f88024bb1caae7bb45f53468991f28a8d3e5ece4438
|
3 |
+
size 2510028
|
demo/40.jpg
ADDED
Git LFS Details
|
demo/6.jpg
ADDED
Git LFS Details
|
demo/MMDet_Tutorial.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
demo/create_result_gif.py
ADDED
@@ -0,0 +1,162 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
import os
|
3 |
+
import os.path as osp
|
4 |
+
|
5 |
+
import matplotlib.patches as mpatches
|
6 |
+
import matplotlib.pyplot as plt
|
7 |
+
import mmcv
|
8 |
+
import numpy as np
|
9 |
+
|
10 |
+
try:
|
11 |
+
import imageio
|
12 |
+
except ImportError:
|
13 |
+
imageio = None
|
14 |
+
|
15 |
+
|
16 |
+
def parse_args():
|
17 |
+
parser = argparse.ArgumentParser(description='Create GIF for demo')
|
18 |
+
parser.add_argument(
|
19 |
+
'image_dir',
|
20 |
+
help='directory where result '
|
21 |
+
'images save path generated by ‘analyze_results.py’')
|
22 |
+
parser.add_argument(
|
23 |
+
'--out',
|
24 |
+
type=str,
|
25 |
+
default='result.gif',
|
26 |
+
help='gif path where will be saved')
|
27 |
+
args = parser.parse_args()
|
28 |
+
return args
|
29 |
+
|
30 |
+
|
31 |
+
def _generate_batch_data(sampler, batch_size):
|
32 |
+
batch = []
|
33 |
+
for idx in sampler:
|
34 |
+
batch.append(idx)
|
35 |
+
if len(batch) == batch_size:
|
36 |
+
yield batch
|
37 |
+
batch = []
|
38 |
+
if len(batch) > 0:
|
39 |
+
yield batch
|
40 |
+
|
41 |
+
|
42 |
+
def create_gif(frames, gif_name, duration=2):
|
43 |
+
"""Create gif through imageio.
|
44 |
+
|
45 |
+
Args:
|
46 |
+
frames (list[ndarray]): Image frames
|
47 |
+
gif_name (str): Saved gif name
|
48 |
+
duration (int): Display interval (s),
|
49 |
+
Default: 2
|
50 |
+
"""
|
51 |
+
if imageio is None:
|
52 |
+
raise RuntimeError('imageio is not installed,'
|
53 |
+
'Please use “pip install imageio” to install')
|
54 |
+
imageio.mimsave(gif_name, frames, 'GIF', duration=duration)
|
55 |
+
|
56 |
+
|
57 |
+
def create_frame_by_matplotlib(image_dir,
|
58 |
+
nrows=1,
|
59 |
+
fig_size=(300, 300),
|
60 |
+
font_size=15):
|
61 |
+
"""Create gif frame image through matplotlib.
|
62 |
+
|
63 |
+
Args:
|
64 |
+
image_dir (str): Root directory of result images
|
65 |
+
nrows (int): Number of rows displayed, Default: 1
|
66 |
+
fig_size (tuple): Figure size of the pyplot figure.
|
67 |
+
Default: (300, 300)
|
68 |
+
font_size (int): Font size of texts. Default: 15
|
69 |
+
|
70 |
+
Returns:
|
71 |
+
list[ndarray]: image frames
|
72 |
+
"""
|
73 |
+
|
74 |
+
result_dir_names = os.listdir(image_dir)
|
75 |
+
assert len(result_dir_names) == 2
|
76 |
+
# Longer length has higher priority
|
77 |
+
result_dir_names.reverse()
|
78 |
+
|
79 |
+
images_list = []
|
80 |
+
for dir_names in result_dir_names:
|
81 |
+
images_list.append(mmcv.scandir(osp.join(image_dir, dir_names)))
|
82 |
+
|
83 |
+
frames = []
|
84 |
+
for paths in _generate_batch_data(zip(*images_list), nrows):
|
85 |
+
|
86 |
+
fig, axes = plt.subplots(nrows=nrows, ncols=2)
|
87 |
+
fig.suptitle('Good/bad case selected according '
|
88 |
+
'to the COCO mAP of the single image')
|
89 |
+
|
90 |
+
det_patch = mpatches.Patch(color='salmon', label='prediction')
|
91 |
+
gt_patch = mpatches.Patch(color='royalblue', label='ground truth')
|
92 |
+
# bbox_to_anchor may need to be finetuned
|
93 |
+
plt.legend(
|
94 |
+
handles=[det_patch, gt_patch],
|
95 |
+
bbox_to_anchor=(1, -0.18),
|
96 |
+
loc='lower right',
|
97 |
+
borderaxespad=0.)
|
98 |
+
|
99 |
+
if nrows == 1:
|
100 |
+
axes = [axes]
|
101 |
+
|
102 |
+
dpi = fig.get_dpi()
|
103 |
+
# set fig size and margin
|
104 |
+
fig.set_size_inches(
|
105 |
+
(fig_size[0] * 2 + fig_size[0] // 20) / dpi,
|
106 |
+
(fig_size[1] * nrows + fig_size[1] // 3) / dpi,
|
107 |
+
)
|
108 |
+
|
109 |
+
fig.tight_layout()
|
110 |
+
# set subplot margin
|
111 |
+
plt.subplots_adjust(
|
112 |
+
hspace=.05,
|
113 |
+
wspace=0.05,
|
114 |
+
left=0.02,
|
115 |
+
right=0.98,
|
116 |
+
bottom=0.02,
|
117 |
+
top=0.98)
|
118 |
+
|
119 |
+
for i, (path_tuple, ax_tuple) in enumerate(zip(paths, axes)):
|
120 |
+
image_path_left = osp.join(
|
121 |
+
osp.join(image_dir, result_dir_names[0], path_tuple[0]))
|
122 |
+
image_path_right = osp.join(
|
123 |
+
osp.join(image_dir, result_dir_names[1], path_tuple[1]))
|
124 |
+
image_left = mmcv.imread(image_path_left)
|
125 |
+
image_left = mmcv.rgb2bgr(image_left)
|
126 |
+
image_right = mmcv.imread(image_path_right)
|
127 |
+
image_right = mmcv.rgb2bgr(image_right)
|
128 |
+
|
129 |
+
if i == 0:
|
130 |
+
ax_tuple[0].set_title(
|
131 |
+
result_dir_names[0], fontdict={'size': font_size})
|
132 |
+
ax_tuple[1].set_title(
|
133 |
+
result_dir_names[1], fontdict={'size': font_size})
|
134 |
+
ax_tuple[0].imshow(
|
135 |
+
image_left, extent=(0, *fig_size, 0), interpolation='bilinear')
|
136 |
+
ax_tuple[0].axis('off')
|
137 |
+
ax_tuple[1].imshow(
|
138 |
+
image_right,
|
139 |
+
extent=(0, *fig_size, 0),
|
140 |
+
interpolation='bilinear')
|
141 |
+
ax_tuple[1].axis('off')
|
142 |
+
|
143 |
+
canvas = fig.canvas
|
144 |
+
s, (width, height) = canvas.print_to_buffer()
|
145 |
+
buffer = np.frombuffer(s, dtype='uint8')
|
146 |
+
img_rgba = buffer.reshape(height, width, 4)
|
147 |
+
rgb, alpha = np.split(img_rgba, [3], axis=2)
|
148 |
+
img = rgb.astype('uint8')
|
149 |
+
|
150 |
+
frames.append(img)
|
151 |
+
|
152 |
+
return frames
|
153 |
+
|
154 |
+
|
155 |
+
def main():
|
156 |
+
args = parse_args()
|
157 |
+
frames = create_frame_by_matplotlib(args.image_dir)
|
158 |
+
create_gif(frames, args.out)
|
159 |
+
|
160 |
+
|
161 |
+
if __name__ == '__main__':
|
162 |
+
main()
|
demo/demo.jpg
ADDED
Git LFS Details
|
demo/demo.mp4
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:510a80cf99607d625a267b0fb21ce405cedb3c9352e25bb42b9972490cf5a1c7
|
3 |
+
size 297688
|
demo/demo_27_result.jpg
ADDED
Git LFS Details
|
demo/demo_27_result2.jpg
ADDED
Git LFS Details
|
demo/demo_31_result2.jpg
ADDED
Git LFS Details
|
demo/demo_40_result2.jpg
ADDED
Git LFS Details
|
demo/demo_6_result.jpg
ADDED
Git LFS Details
|
demo/demo_6_result2.jpg
ADDED
Git LFS Details
|
demo/demo_result.jpg
ADDED
Git LFS Details
|
demo/image_demo.py
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from argparse import ArgumentParser
|
2 |
+
|
3 |
+
from mmdet.apis import inference_detector, init_detector, show_result_pyplot
|
4 |
+
|
5 |
+
|
6 |
+
def main():
|
7 |
+
parser = ArgumentParser()
|
8 |
+
parser.add_argument('img', help='Image file')
|
9 |
+
parser.add_argument('config', help='Config file')
|
10 |
+
parser.add_argument('checkpoint', help='Checkpoint file')
|
11 |
+
parser.add_argument(
|
12 |
+
'--device', default='cuda:0', help='Device used for inference')
|
13 |
+
parser.add_argument(
|
14 |
+
'--score-thr', type=float, default=0.3, help='bbox score threshold')
|
15 |
+
args = parser.parse_args()
|
16 |
+
|
17 |
+
# build the model from a config file and a checkpoint file
|
18 |
+
model = init_detector(args.config, args.checkpoint, device=args.device)
|
19 |
+
# test a single image
|
20 |
+
result = inference_detector(model, args.img)
|
21 |
+
# show the results
|
22 |
+
show_result_pyplot(model, args.img, result, score_thr=args.score_thr)
|
23 |
+
|
24 |
+
|
25 |
+
if __name__ == '__main__':
|
26 |
+
main()
|
demo/inference_demo.ipynb
ADDED
The diff for this file is too large to render.
See raw diff
|
|
demo/test.png
ADDED
Git LFS Details
|
demo/video_demo.py
ADDED
@@ -0,0 +1,60 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
|
3 |
+
import cv2
|
4 |
+
import mmcv
|
5 |
+
|
6 |
+
from mmdet.apis import inference_detector, init_detector
|
7 |
+
|
8 |
+
|
9 |
+
def parse_args():
|
10 |
+
parser = argparse.ArgumentParser(description='MMDetection video demo')
|
11 |
+
parser.add_argument('video', help='Video file')
|
12 |
+
parser.add_argument('config', help='Config file')
|
13 |
+
parser.add_argument('checkpoint', help='Checkpoint file')
|
14 |
+
parser.add_argument(
|
15 |
+
'--device', default='cuda:0', help='Device used for inference')
|
16 |
+
parser.add_argument(
|
17 |
+
'--score-thr', type=float, default=0.3, help='Bbox score threshold')
|
18 |
+
parser.add_argument('--out', type=str, help='Output video file')
|
19 |
+
parser.add_argument('--show', action='store_true', help='Show video')
|
20 |
+
parser.add_argument(
|
21 |
+
'--wait-time',
|
22 |
+
type=float,
|
23 |
+
default=1,
|
24 |
+
help='The interval of show (s), 0 is block')
|
25 |
+
args = parser.parse_args()
|
26 |
+
return args
|
27 |
+
|
28 |
+
|
29 |
+
def main():
|
30 |
+
args = parse_args()
|
31 |
+
assert args.out or args.show, \
|
32 |
+
('Please specify at least one operation (save/show the '
|
33 |
+
'video) with the argument "--out" or "--show"')
|
34 |
+
|
35 |
+
model = init_detector(args.config, args.checkpoint, device=args.device)
|
36 |
+
|
37 |
+
video_reader = mmcv.VideoReader(args.video)
|
38 |
+
video_writer = None
|
39 |
+
if args.out:
|
40 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
41 |
+
video_writer = cv2.VideoWriter(
|
42 |
+
args.out, fourcc, video_reader.fps,
|
43 |
+
(video_reader.width, video_reader.height))
|
44 |
+
|
45 |
+
for frame in mmcv.track_iter_progress(video_reader):
|
46 |
+
result = inference_detector(model, frame)
|
47 |
+
frame = model.show_result(frame, result, score_thr=args.score_thr)
|
48 |
+
if args.show:
|
49 |
+
cv2.namedWindow('video', 0)
|
50 |
+
mmcv.imshow(frame, 'video', args.wait_time)
|
51 |
+
if args.out:
|
52 |
+
video_writer.write(frame)
|
53 |
+
|
54 |
+
if video_writer:
|
55 |
+
video_writer.release()
|
56 |
+
cv2.destroyAllWindows()
|
57 |
+
|
58 |
+
|
59 |
+
if __name__ == '__main__':
|
60 |
+
main()
|
demo/webcam_demo.py
ADDED
@@ -0,0 +1,46 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import argparse
|
2 |
+
|
3 |
+
import cv2
|
4 |
+
import torch
|
5 |
+
|
6 |
+
from mmdet.apis import inference_detector, init_detector
|
7 |
+
|
8 |
+
|
9 |
+
def parse_args():
|
10 |
+
parser = argparse.ArgumentParser(description='MMDetection webcam demo')
|
11 |
+
parser.add_argument('config', help='test config file path')
|
12 |
+
parser.add_argument('checkpoint', help='checkpoint file')
|
13 |
+
parser.add_argument(
|
14 |
+
'--device', type=str, default='cuda:0', help='CPU/CUDA device option')
|
15 |
+
parser.add_argument(
|
16 |
+
'--camera-id', type=int, default=0, help='camera device id')
|
17 |
+
parser.add_argument(
|
18 |
+
'--score-thr', type=float, default=0.5, help='bbox score threshold')
|
19 |
+
args = parser.parse_args()
|
20 |
+
return args
|
21 |
+
|
22 |
+
|
23 |
+
def main():
|
24 |
+
args = parse_args()
|
25 |
+
|
26 |
+
device = torch.device(args.device)
|
27 |
+
|
28 |
+
model = init_detector(args.config, args.checkpoint, device=device)
|
29 |
+
|
30 |
+
camera = cv2.VideoCapture(args.camera_id)
|
31 |
+
|
32 |
+
print('Press "Esc", "q" or "Q" to exit.')
|
33 |
+
while True:
|
34 |
+
ret_val, img = camera.read()
|
35 |
+
result = inference_detector(model, img)
|
36 |
+
|
37 |
+
ch = cv2.waitKey(1)
|
38 |
+
if ch == 27 or ch == ord('q') or ch == ord('Q'):
|
39 |
+
break
|
40 |
+
|
41 |
+
model.show_result(
|
42 |
+
img, result, score_thr=args.score_thr, wait_time=1, show=True)
|
43 |
+
|
44 |
+
|
45 |
+
if __name__ == '__main__':
|
46 |
+
main()
|