Spaces:
Runtime error
Runtime error
Upload utils/plot.py
Browse files- utils/plot.py +90 -0
utils/plot.py
ADDED
@@ -0,0 +1,90 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
import webcolors
|
3 |
+
import os
|
4 |
+
import uuid
|
5 |
+
import numpy as np
|
6 |
+
|
7 |
+
STANDARD_COLORS = [
|
8 |
+
'LawnGreen', 'Chartreuse', 'Aqua', 'Beige', 'Azure', 'BlanchedAlmond', 'Bisque',
|
9 |
+
'Aquamarine', 'BlueViolet', 'BurlyWood', 'CadetBlue', 'AntiqueWhite',
|
10 |
+
'Chocolate', 'Coral', 'CornflowerBlue', 'Cornsilk', 'Crimson', 'Cyan',
|
11 |
+
'DarkCyan', 'DarkGoldenRod', 'DarkGrey', 'DarkKhaki', 'DarkOrange',
|
12 |
+
'DarkOrchid', 'DarkSalmon', 'DarkSeaGreen', 'DarkTurquoise', 'DarkViolet',
|
13 |
+
'DeepPink', 'DeepSkyBlue', 'DodgerBlue', 'FireBrick', 'FloralWhite',
|
14 |
+
'ForestGreen', 'Fuchsia', 'Gainsboro', 'GhostWhite', 'Gold', 'GoldenRod',
|
15 |
+
'Salmon', 'Tan', 'HoneyDew', 'HotPink', 'IndianRed', 'Ivory', 'Khaki',
|
16 |
+
'Lavender', 'LavenderBlush', 'AliceBlue', 'LemonChiffon', 'LightBlue',
|
17 |
+
'LightCoral', 'LightCyan', 'LightGoldenRodYellow', 'LightGray', 'LightGrey',
|
18 |
+
'LightGreen', 'LightPink', 'LightSalmon', 'LightSeaGreen', 'LightSkyBlue',
|
19 |
+
'LightSlateGray', 'LightSlateGrey', 'LightSteelBlue', 'LightYellow', 'Lime',
|
20 |
+
'LimeGreen', 'Linen', 'Magenta', 'MediumAquaMarine', 'MediumOrchid',
|
21 |
+
'MediumPurple', 'MediumSeaGreen', 'MediumSlateBlue', 'MediumSpringGreen',
|
22 |
+
'MediumTurquoise', 'MediumVioletRed', 'MintCream', 'MistyRose', 'Moccasin',
|
23 |
+
'NavajoWhite', 'OldLace', 'Olive', 'OliveDrab', 'Orange', 'OrangeRed',
|
24 |
+
'Orchid', 'PaleGoldenRod', 'PaleGreen', 'PaleTurquoise', 'PaleVioletRed',
|
25 |
+
'PapayaWhip', 'PeachPuff', 'Peru', 'Pink', 'Plum', 'PowderBlue', 'Purple',
|
26 |
+
'Red', 'RosyBrown', 'RoyalBlue', 'SaddleBrown', 'Green', 'SandyBrown',
|
27 |
+
'SeaGreen', 'SeaShell', 'Sienna', 'Silver', 'SkyBlue', 'SlateBlue',
|
28 |
+
'SlateGray', 'SlateGrey', 'Snow', 'SpringGreen', 'SteelBlue', 'GreenYellow',
|
29 |
+
'Teal', 'Thistle', 'Tomato', 'Turquoise', 'Violet', 'Wheat', 'White',
|
30 |
+
'WhiteSmoke', 'Yellow', 'YellowGreen'
|
31 |
+
]
|
32 |
+
|
33 |
+
|
34 |
+
def from_colorname_to_bgr(color):
|
35 |
+
rgb_color = webcolors.name_to_rgb(color)
|
36 |
+
result = (rgb_color.blue, rgb_color.green, rgb_color.red)
|
37 |
+
return result
|
38 |
+
|
39 |
+
|
40 |
+
def standard_to_bgr(list_color_name):
|
41 |
+
standard = []
|
42 |
+
for i in range(len(list_color_name) - 36): # -36 used to match the len(obj_list)
|
43 |
+
standard.append(from_colorname_to_bgr(list_color_name[i]))
|
44 |
+
return standard
|
45 |
+
|
46 |
+
|
47 |
+
def get_index_label(label, obj_list):
|
48 |
+
index = int(obj_list.index(label))
|
49 |
+
return index
|
50 |
+
|
51 |
+
|
52 |
+
def plot_one_box(img, coord, label=None, score=None, color=None, line_thickness=None):
|
53 |
+
tl = line_thickness or int(round(0.001 * max(img.shape[0:2]))) # line thickness
|
54 |
+
color = color
|
55 |
+
c1, c2 = (int(coord[0]), int(coord[1])), (int(coord[2]), int(coord[3]))
|
56 |
+
cv2.rectangle(img, c1, c2, color, thickness=tl)
|
57 |
+
if label:
|
58 |
+
tf = max(tl - 2, 1) # font thickness
|
59 |
+
s_size = cv2.getTextSize(str('{:.0%}'.format(score)), 0, fontScale=float(tl) / 3, thickness=tf)[0]
|
60 |
+
t_size = cv2.getTextSize(label, 0, fontScale=float(tl) / 3, thickness=tf)[0]
|
61 |
+
c2 = c1[0] + t_size[0] + s_size[0] + 15, c1[1] - t_size[1] - 3
|
62 |
+
cv2.rectangle(img, c1, c2, color, -1) # filled
|
63 |
+
cv2.putText(img, '{}: {:.0%}'.format(label, score), (c1[0], c1[1] - 2), 0, float(tl) / 3, [0, 0, 0],
|
64 |
+
thickness=tf, lineType=cv2.FONT_HERSHEY_SIMPLEX)
|
65 |
+
|
66 |
+
|
67 |
+
color_list = standard_to_bgr(STANDARD_COLORS)
|
68 |
+
|
69 |
+
|
70 |
+
def display(preds, imgs, obj_list, imshow=True, imwrite=False):
|
71 |
+
for i in range(len(imgs)):
|
72 |
+
if len(preds[i]['rois']) == 0:
|
73 |
+
continue
|
74 |
+
|
75 |
+
imgs[i] = imgs[i].copy()
|
76 |
+
|
77 |
+
for j in range(len(preds[i]['rois'])):
|
78 |
+
(x1, y1, x2, y2) = preds[i]['rois'][j].astype(np.int)
|
79 |
+
obj = obj_list[preds[i]['class_ids'][j]]
|
80 |
+
score = float(preds[i]['scores'][j])
|
81 |
+
|
82 |
+
plot_one_box(imgs[i], [x1, y1, x2, y2], label=obj, score=score,
|
83 |
+
color=color_list[get_index_label(obj, obj_list)])
|
84 |
+
if imshow:
|
85 |
+
cv2.imshow('img', imgs[i])
|
86 |
+
cv2.waitKey(0)
|
87 |
+
|
88 |
+
if imwrite:
|
89 |
+
os.makedirs('test/', exist_ok=True)
|
90 |
+
cv2.imwrite(f'test/{uuid.uuid4().hex}.jpg', imgs[i])
|