forked from open-mmlab/mmdeploy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrotated_object_detection.py
50 lines (40 loc) · 1.6 KB
/
rotated_object_detection.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
# Copyright (c) OpenMMLab. All rights reserved.
import argparse
from math import cos, sin
import cv2
import numpy as np
from mmdeploy_runtime import RotatedDetector
def parse_args():
parser = argparse.ArgumentParser(
description='show how to use sdk python api')
parser.add_argument('device_name', help='name of device, cuda or cpu')
parser.add_argument(
'model_path', help='path of SDK model dumped by model converter')
parser.add_argument('image_path', help='path of an image')
args = parser.parse_args()
return args
def main():
args = parse_args()
img = cv2.imread(args.image_path)
detector = RotatedDetector(args.model_path, args.device_name, 0)
rbboxes, labels = detector(img)
indices = [i for i in range(len(rbboxes))]
for index, rbbox, label_id in zip(indices, rbboxes, labels):
[cx, cy, w, h, angle], score = rbbox[0:5], rbbox[-1]
if score < 0.1:
continue
[wx, wy, hx, hy] = \
0.5 * np.array([w, w, -h, h]) * \
np.array([cos(angle), sin(angle), sin(angle), cos(angle)])
points = np.array([[[int(cx - wx - hx),
int(cy - wy - hy)],
[int(cx + wx - hx),
int(cy + wy - hy)],
[int(cx + wx + hx),
int(cy + wy + hy)],
[int(cx - wx + hx),
int(cy - wy + hy)]]])
cv2.drawContours(img, points, -1, (0, 255, 0), 2)
cv2.imwrite('output_detection.png', img)
if __name__ == '__main__':
main()