-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
51 lines (40 loc) · 1.77 KB
/
Copy pathutils.py
File metadata and controls
51 lines (40 loc) · 1.77 KB
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
import cv2
import numpy as np
MARGIN = 10 # pixels
ROW_SIZE = 30 # pixels
FONT_SIZE = 1
FONT_THICKNESS = 1
TEXT_COLOR = (255, 255, 0) # yellow
def visualize(
image,
detection_result
) -> np.ndarray:
"""Рисует ограничительные рамки на входном изображении и возвращает их.
Args:
image: Входное RGB-изображение.
detection_result: Список всех визуализируемых сущностей "Detection".
Returns:
Изображение с ограничивающими рамками.
"""
for detection in detection_result.detections:
# Draw bounding_box
bbox = detection.bounding_box
start_point = bbox.origin_x, bbox.origin_y
end_point = bbox.origin_x + bbox.width, bbox.origin_y + bbox.height
center = (bbox.origin_x + bbox.width // 2,
bbox.origin_y + bbox.height // 2)
cv2.rectangle(image, start_point, end_point, TEXT_COLOR, 3)
cv2.circle(image, center, 2, (0, 0, 255), -1)
image_center = (image.shape[1] // 2, image.shape[0] // 2)
# Draw line between image center and bbox center
cv2.line(image, image_center, center, (0, 0, 130), 1)
# Draw label and score
category = detection.categories[0]
category_name = category.category_name
probability = round(category.score, 2)
result_text = category_name + ' (' + str(probability) + ')'
text_location = (MARGIN + bbox.origin_x,
MARGIN + ROW_SIZE + bbox.origin_y)
cv2.putText(image, result_text, text_location, cv2.FONT_HERSHEY_DUPLEX,
FONT_SIZE, TEXT_COLOR, FONT_THICKNESS, cv2.LINE_AA)
return image