-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutility.py
80 lines (49 loc) · 1.7 KB
/
utility.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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import cv2
def x_y_convert(contours):
"""
input: contours -> the contours of your image mask
output: coco -> a list of coco format cordinates
"""
coco = []
counter = 0
for segment in contours:
coco.append([])
for element in segment:
x = element[0][0]
y = element[0][1]
coco[counter].append(x)
coco[counter].append(y)
counter += 1
return coco
def Get_x_y_and_Area(img_path):
"""
input: img_path -> the coco annotations
output: contours -> a list of bbox cordinates
area -> a list of bbox cordinates
"""
img = cv2.imread(img_path)
tmp_img = img.copy()
gray = cv2.cvtColor(tmp_img,cv2.COLOR_BGR2GRAY)
ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY)
contours, hierarchy = cv2.findContours(binary,cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(tmp_img,contours,-1,(255,255,255),-1)
area = (tmp_img==255).sum()
return contours, area
def Find_the_bbox(annotations):
"""
input: annotations -> the coco format annotations
output: bbox -> a list of bbox cordinates , i.e:[0,155,23,56]
"""
bbox_0 = 1000
bbox_1 = 1000
bbox_2 = 0
bbox_3 = 0
for annotation in annotations:
bbox_0 = int( min(bbox_0, min(annotation[0::2])) )
bbox_1 = int( min(bbox_1, min(annotation[1::2])) )
bbox_2 = int( max(bbox_2, max(annotation[0::2]) ) )
bbox_3 = int( max(bbox_3, max(annotation[1::2]) ) )
bbox_2 = bbox_2 - bbox_0
bbox_3 = bbox_3 - bbox_1
bbox = [bbox_0, bbox_1, bbox_2, bbox_3]
return bbox