-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlabels.py
More file actions
executable file
·69 lines (50 loc) · 1.93 KB
/
Copy pathlabels.py
File metadata and controls
executable file
·69 lines (50 loc) · 1.93 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/env python
# -*- coding: UTF-8 -*-
import cv2
import numpy as np
def get_transformed_labels(transformation_function, shape, labels_list):
if(not ".geometric." in transformation_function.__module__):
return labels_list
labels_image, n_labels = __get_image_from_labels(shape, labels_list)
transformed_image_labels = transformation_function(labels_image)
return __get_labels_from_image(transformed_image_labels, n_labels)
def __get_image_from_labels(shape, labels_list):
label_image = np.zeros(shape[ : 2], dtype = np.uint8)
n_rectangles = len(labels_list)
pace = int(round(255 / (n_rectangles + 1)))
color = pace
for label in labels_list:
label_image = __draw_label_rectangle(label_image, label, color)
color += pace
return label_image, n_rectangles
def __draw_label_rectangle(image, label, color = 255):
h, w = image.shape[ : 2]
center_x = int(round(float(label[1]) * w))
center_y = int(round(float(label[2]) * h))
half_width = int(round(float(label[3]) / 2 * w))
half_height = int(round(float(label[4]) / 2 * h))
image = cv2.rectangle(image, (center_x - half_width, center_y - half_height),
(center_x + half_width, center_y + half_height), color, 1)
return image
def __get_labels_from_image(image, n_labels):
labels = []
h, w = image.shape[ : 2]
pace = int(round(255 / (n_labels + 1)))
color = pace
epsilon = int(pace / 10)
for i in range(n_labels):
new_label = [i] + __get_rectangle_values(image, color, epsilon, h, w)
labels.append(new_label)
color += pace
return labels
def __get_rectangle_values(image, color, epsilon, h, w):
rows, cols = np.where(image - color < epsilon)[ : 2]
min_row = min(rows)
max_row = max(rows)
min_col = min(cols)
max_col = max(cols)
w_rectangle = (max_col - min_col) / w
h_rectangle = (max_row - min_row) / h
center_x = np.mean([max_col, min_col]) / w
center_y = np.mean([max_row, min_row]) / h
return [ center_x, center_y, w_rectangle, h_rectangle ]