-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
164 lines (121 loc) · 5.39 KB
/
application.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#ez a program a szakdolgozatomhoz keszult, hogy tesztelni tudjam a tanitott konvolucios neurelis halot
#futtatasahoz szuksegesek az alabb importalt konyvtarak, bele ertve a Caffe telepiteset is
#futtatas: python application.py <halot leiro .prototxt file> <tanitott ertekeket tartalmazo .caffemodel file> <kepek forrasat tartalmazo konyvtar> <feldolgozott kepek celja>
import cv2
import time
import caffe
import numpy as np
import glob
import ntpath
from Box import Box
import sys
winW = 195
winH = 195
treshold = 0.9992
def set_caffe(net_arch, model):
caffe.set_device(0)
caffe.set_mode_gpu()
net = caffe.Net(net_arch, model, caffe.TEST)
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_mean('data', np.load('/media/gabor/save_1TB/szakdoga/caffenet/train_14/data/szakdolg_mean.npy').mean(1).mean(1))
transformer.set_transpose('data', (2,0,1))
transformer.set_channel_swap('data', (2,1,0))
transformer.set_raw_scale('data', 255.0)
net.blobs['data'].reshape(1,3,227,227)
return (transformer, net)
def sliding_window(image, stepSize, winSize):
for y in xrange(0, image.shape[0] - winSize[0], stepSize):
for x in xrange(0, image.shape[1] - winSize[1], stepSize):
yield(x, y, image[y:y + winSize[1], x:x + winSize[0]])
def merge(boxArray):
boxes = []
length = len(boxArray)
while (len(boxArray) > 0):
actualBox = boxArray[0]
print "main box num: " + str(len(boxes))
j = 1
while (j < length):
print "j: " + str(j)
if(j == len(boxArray)):
break
xOK = 0
yOK = 0
# cast ellenorzese
if (actualBox.cast == boxArray[j].cast):
if (actualBox.x <= boxArray[j].x and (actualBox.x + actualBox.w) >= boxArray[j].x):
xOK = 1
if (actualBox.x >= boxArray[j].x and actualBox.x <= (boxArray[j].x + boxArray[j].w)):
xOK = 1
if (actualBox.y <= boxArray[j].y and (actualBox.y + actualBox.h) >= boxArray[j].y):
yOK = 1
if (actualBox.y >= boxArray[j].y and actualBox.y <= (boxArray[j].y + boxArray[j].h)):
yOK = 1
if (xOK and yOK):
minX = min(actualBox.x, boxArray[j].x)
maxX = max(actualBox.x + actualBox.w, boxArray[j].x + boxArray[j].w)
minY = min(actualBox.y, boxArray[j].y)
maxY = max(actualBox.y + actualBox.h, boxArray[j].y + boxArray[j].h)
actualBox = Box(minX, minY, maxX-minX, maxY-minY, actualBox.cast, actualBox.prob)
del boxArray[j]
length -= 1
print "MERGE"
if not(xOK and yOK):
j += 1
else:
j = 1
boxes.append(actualBox)
del boxArray[0]
return boxes
def run(net_arch, model, image_dir, output_dir):
(transformer, net) = set_caffe(net_arch, model)
for filename in glob.glob(image_dir + '*.png'):
boxes = []
image = cv2.imread(filename)
caffeImage = caffe.io.load_image(filename)
for (x, y, window) in sliding_window(caffeImage, stepSize=97, winSize=(winW, winH)):
im = window
net.blobs['data'].data[...] = transformer.preprocess('data', im)
out = net.forward()
probability = net.blobs['prob'].data.max()
cast = out['prob'].argmax()
if probability > treshold and cast != 0:
boxes.append(Box(x, y, winW, winH, cast, probability))
labels = np.loadtxt("/home/gabor/deep-learning/szakdoga/image_data/pairs.txt", str, delimiter=' ')
top_k = net.blobs['prob'].data[0].flatten().argsort()[-1:-6:-1]
print labels[top_k]
person = 0
bike = 0
car = 0
print "length boxes" + str(len(boxes))
boxes2 = merge(boxes)
output = image.copy()
for box in boxes2:
color = (0, 0, 0)
cast = 'none'
if box.cast == 1:
color = (255, 0, 0)
cast = 'person'
person += 1
elif box.cast == 2:
color = (0, 255, 0)
cast = 'bike'
bike += 1
elif box.cast == 3:
color = (0, 0, 255)
cast = 'car'
car += 1
cv2.rectangle(output, (box.x, box.y), (box.x + box.w, box.y + box.h), color, 2)
cv2.putText(output, str(box.prob), (box.x, box.y+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
cv2.putText(output, cast, (box.x+200, box.y+10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (255, 255, 255), 2)
basename = str(ntpath.basename(filename))
cv2.imwrite(output_dir + basename, output)
if __name__ == "__main__":
#halo leiro .prototxt
net = sys.argv[1]
#tanitott modell file
model = sys.argv[2]
#feldolgozando kepetet tartalmazo konyvtar
image_dir = sys.argv[3]
#feldolgozott kepek celja
output_dir = sys.argv[4]
run(net, model, image_dir, output_dir)