-
Notifications
You must be signed in to change notification settings - Fork 142
/
run_demo.py
executable file
·38 lines (32 loc) · 1.55 KB
/
run_demo.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
import os
from omdet.inference.det_engine import DetEngine
from omdet.utils.plots import Annotator
from PIL import Image
import numpy as np
if __name__ == "__main__":
engine = DetEngine(batch_size=1, device='cuda')
img_paths = ['./sample_data/000000574769.jpg'] # path of images
labels = ["person", "cat", "orange"] # labels to be predicted
prompt = 'Detect {}.'.format(','.join(labels)) # prompt of detection task, use "Detect {}." as default
res = engine.inf_predict('OmDet-Turbo_tiny_SWIN_T', # prefix name of the pretrained checkpoints
task=prompt,
data=img_paths,
labels=labels,
src_type='local', # type of the image_paths, "local"/"url"
conf_threshold=0.30,
nms_threshold=0.5
)
print(res)
out_folder = './outputs'
for idx, img_path in enumerate(img_paths):
im = Image.open(img_path)
a = Annotator(np.ascontiguousarray(im), font_size=12, line_width=1, pil=True, font='sample_data/simsun.ttc')
for R in res[idx]:
a.box_label([R['xmin'], R['ymin'], R['xmax'], R['ymax']],
label=f"{R['label']} {str(int(R['conf'] * 100))}%",
color='red')
if not os.path.exists(out_folder):
os.mkdir(out_folder)
image = a.result()
img = Image.fromarray(image)
img.save('outputs/'+img_path.split('/')[-1])