-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsliding_window.py
140 lines (110 loc) · 3.93 KB
/
sliding_window.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# IMPLEMENTATION OF SLIDING WINDOW
import cv2
import matplotlib.pyplot as plt
from matplotlib import cm
from PIL import Image,ImageDraw
import torch
import torchvision
import numpy as np
# from keras import backend as K
# from keras.models import load_model
# from keras.utils import CustomObjectScope
# from keras.initializers import glorot_uniform
"""
with CustomObjectScope({'GlorotUniform': glorot_uniform()}):
global Net_model
Net_model= load_model('test_save2.h5')
Net_model.summary()
print("smit")
# Net_model.evaluate()
"""
# LOADING THE MODEL
working_model=torch.load('model.pt')
working_model.eval()
# loading and pre-processing the image
def generate_test_img():
img = Image.open('test4.jpg')
img=img.convert('L')
print(img.size)
# img.save('rgb.jpg')
if img.size[0]>2000 and img.size[1]>1000:
img=img.resize((1000,2000),Image.ANTIALIAS)
else:
img=img.resize((img.size[0],img.size[1]),Image.ANTIALIAS)
print(img.mode)
return img
#converting the image to numpy array
def generate_test_img_array(img):
h=img.size[0]
w=img.size[1]
img=np.array(img)
img=img/255
img=img.reshape(1,1,h,w).astype('float32')
print(img.shape)
return img
# calling the function for inputting the test image
test1=generate_test_img()
test1_arr=generate_test_img_array(test1)
def sw(image,image_array, x =1.6, y=2.4, save_files = True, threshold = 0.97):
#CONVERTING NUMPY ARRAY TO TENSOR AND PUTTING IT IN MODEL
image_tensor=torch.from_numpy(image_array)
output_tensor = working_model(image_tensor)
print("out",output_tensor.shape)
output_squeeze = torch.squeeze(output_tensor,0).detach().numpy() # all dimensions of size 1 removed
print(output_squeeze.shape)
heatmap = output_squeeze[0] # the channel or the 2nd one is eliminated
print(heatmap,"HEATMAP",heatmap.shape)
heatmap_thr = heatmap.copy()
# applying threshold or a simpler version of non-max suppression
heatmap_thr[heatmap[:,:]>threshold] = 100
heatmap_thr[heatmap[:,:]<=threshold] = 0
boxes=[]
#converting PIL image to DrawImage
image_copy = image.copy()
draw = ImageDraw.Draw(image)
draw_copy = ImageDraw.Draw(image_copy)
heatmap_img = Image.fromarray(np.uint8(cm.gist_heat(heatmap)*255))
heatmap_img.show()
print("with threshold")
heatmap_img_thresh = Image.fromarray(np.uint8(cm.gist_heat(heatmap_thr)*255))
heatmap_img_thresh.show()
# print(heatmap.shape[0])
print("---------")
# print(np.arange(heatmap.shape[0]))
print("----------")
# yy-rows have the same elements, xx- columns have the same elements
xx,yy = np.meshgrid(np.arange(heatmap.shape[1]),np.arange(heatmap.shape[0]))
# x_det and y_det contain all those indices of heatmap whose value is greater than threshold.
x_det = (xx[heatmap[:,:]>threshold])
y_det = (yy[heatmap[:,:]>threshold])
print(y_det)
# Scaling of the model
shrink_ratio = (image.width/heatmap_img.width , image.height/heatmap_img.height)
# Appending the dimensions of the bounding boxes
for i,j in zip(x_det,y_det):
if not save_files :
if i>heatmap_img.width//1.95 and j>int(heatmap_img.height/1.95) :
boxes.append([int(i*13),int(j*13),int(48),int(96)])
else :
boxes.append([int(i*13),int(j*13),int(48),int(96)])
#group interecting rectanlgles
bound_boxes = cv2.groupRectangles(boxes,2,1)
bound_boxes=bound_boxes[:1]
print("Number of Objects: ",len(bound_boxes[0]))
print(bound_boxes)
# draw the box on the image and its copy
for box in bound_boxes:
for b in box:
print(b,"BOX")
draw_copy.rectangle((b[0],b[1],b[2]+b[0],b[1]+b[3]),outline='blue')#draw.draw_rectangle(xy, ink, 0, width)
for box in bound_boxes[0]:
draw.rectangle((box[0]-(x-1)*box[2]//2,box[1]-(y-1)*box[2]//2,box[2]*x+box[0]-(x-1)*box[2]//2,box[3]*y+box[1]-(y-1)*box[2]//2),outline='green')
#saving the images
if(save_files) :
image.save("Actual output.png")
image_copy.save("Copy_Output.png")
heatmap_img.save("Heatmap.png")
heatmap_img_thresh.save("Heatmap_thresh.png")
image.show()
# calling the function
sw(test1,test1_arr)