-
Notifications
You must be signed in to change notification settings - Fork 57
/
Utils.py
112 lines (64 loc) · 2.32 KB
/
Utils.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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
#output dims -> (1,x,x,1,5)
# boxes = decode_to_boxes(output) output to boxes
# corner_boxes = boxes_to_corners(boxes) boxes to corners
# final_out = non_max_suppress(corner_boxes)
# iou()
import numpy as np
import os
import tensorflow as tf
from scipy.io import loadmat
import cv2
import matplotlib.pyplot as plt
def decode_to_boxes(output , ht , wd):
#output : (x,x,1,5)
#x,y,h,w
img_ht = ht
img_wd = wd
threshold = 0.5
grid_h,grid_w = output.shape[:2]
final_boxes = []
scores = []
for i in range(grid_h):
for j in range(grid_w):
if output[i,j,0,0] > threshold:
temp = output[i,j,0,1:5]
x_unit = ((j + (temp[0]))/grid_w)*img_wd
y_unit = ((i + (temp[1]))/grid_h)*img_ht
width = temp[2]*img_wd*1.3
height = temp[3]*img_ht*1.3
final_boxes.append([x_unit - width/2,y_unit - height/2 ,x_unit + width/2,y_unit + height/2])
scores.append(output[i,j,0,0])
return final_boxes,scores
def iou(box1,box2):
x1 = max(box1[0],box2[0])
x2 = min(box1[2],box2[2])
y1 = max(box1[1] ,box2[1])
y2 = min(box1[3],box2[3])
inter = (x2 - x1)*(y2 - y1)
area1 = (box1[2] - box1[0])*(box1[3] - box1[1])
area2 = (box2[2] - box2[0])*(box2[3] - box2[1])
fin_area = area1 + area2 - inter
iou = inter/fin_area
return iou
def non_max(boxes , scores , iou_num):
scores_sort = scores.argsort().tolist()
keep = []
while(len(scores_sort)):
index = scores_sort.pop()
keep.append(index)
if(len(scores_sort) == 0):
break
iou_res = []
for i in scores_sort:
iou_res.append(iou(boxes[index] , boxes[i]))
iou_res = np.array(iou_res)
filtered_indexes = set((iou_res > iou_num).nonzero()[0])
scores_sort = [v for (i,v) in enumerate(scores_sort) if i not in filtered_indexes]
final = []
for i in keep:
final.append(boxes[i])
return final
def decode(output , ht , wd , iou):
boxes , scores = decode_to_boxes(output ,ht ,wd)
boxes = non_max(boxes,np.array(scores) , iou)
return boxes