-
Notifications
You must be signed in to change notification settings - Fork 0
/
projekat.py
225 lines (166 loc) · 6.53 KB
/
projekat.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
import numpy as np
import cv2
from vector import *
import os
from scipy import ndimage
from load import model_create
import tensorflow as tf
from vector import distance, pnt2line
#model = model_create()
#detektujemo liniju sve ok
def houghLinesTransformation(img):
# img = cv2.imread('dave.jpg')
# edges = cv2.Canny(gray, 50, 150, apertureSize=3)
# gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# erosion = cv2.erode(gray, kernel_line, iterations=1)
# edges = cv2.Canny(erosion, 50, 150, 3)
#blur = cv2.GaussianBlur(img, (5, 5), 1)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
kernel_line = np.ones((2, 2), np.uint8)
erosion = cv2.erode(gray, kernel_line, iterations=1)
#edges = cv2.Canny(erosion, 50, 150, 3)
#edges = cv2.Canny(erosion, 50, 150, 3)
image_bin = cv2.threshold(erosion,20, 255, cv2.THRESH_BINARY)[1]
minLineLength = 100
maxLineGap = 100
lines= cv2.HoughLinesP(image_bin, 1, np.pi / 180, 100, minLineLength, maxLineGap)
return lines
def image_bin(image_gs):
height, width = image_gs.shape[0:2]
image_binary = np.ndarray((height, width), dtype=np.uint8)
ret,image_bin = cv2.threshold(image_gs, 127, 255, cv2.THRESH_BINARY)
return image_bin
def find_liness_coord(liness):
if liness is not None:
for x1, y1, x2, y2 in liness[0]:
line = x1, y1, x2, y2
return line
else:
return None
def resize_region(region):
#'''Transformisati selektovani region na sliku dimenzija 28x28'''
return cv2.resize(region,(28,28), interpolation = cv2.INTER_NEAREST)
def inRange(r, item):
retVal = []
for obj in global_contours:
x1, y1, w1, h1= cv2.boundingRect(item)
x2, y2, w2, h2 = cv2.boundingRect(obj)
mdist = distance((x1, y1),(x2, y2))
if(mdist<r):
return True
global_contours.append(item)
return False
global_contours=[]
def select_roi(image_orig, image_bin):
contours, hierarchy = cv2.findContours(image_bin.copy(), cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)
regions_array = []
elements = [] # lista sortiranih regiona po x osi (sa leva na desno)
digits = []
#lines = houghLinesTransformation(image_orig)
for contour in contours:
x, y, w, h = cv2.boundingRect(contour) # koordinate i velicina granicnog pravougaonika
area = cv2.contourArea(contour)
cv2.rectangle(image_orig, (x, y), (x + w, y + h), (0, 255, 0), 2)
if area > 6 and h < 48 and h > 4 and w > 2:
center = (x + w / 2, y + h / 2)
# kopirati [y:y+h+1, x:x+w+1] sa binarne slike i smestiti u novu sliku
# označiti region pravougaonikom na originalnoj slici (image_orig) sa rectangle funkcijom
#image_bin = cv2.medianBlur(image_bin, 5)
region = image_bin[y-7:y + h + 7, x-7:x + w+ 7]
lines = houghLinesTransformation(image_orig)
if lines is not None:
for x1, y1, x2, y2 in lines[0]:
#cv2.line(image_orig, (x1, y1), (x2, y2), (192, 0, 182), 2)
#cv2.line(image_orig, (x1, y1), (x2, y2), (192, 0, 182), 2)
dist, pt, r = pnt2line2((x + w, y + h), (x1, y1), (x2, y2))
#print(dist)
#cv2.imshow('imgreg', region)
#cv2.waitKey(0)
if dist < 6 and r==1:
if(inRange(18, contour)==False):
#print(dist)
#print(regions_array)
#cv2.imshow('imgreg', region)
#cv2.waitKey(0)
regions_array.append([resize_region(region), (x, y, w, h)])
# detected_digit = detect_digits(digits, digit)
# sortirati sve regione po x osi (sa leva na desno) i smestiti u promenljivu sorted_regions
return image_orig, regions_array
def scale_to_range(image): # skalira elemente slike na opseg od 0 do 1
#''' Elementi matrice image su vrednosti 0 ili 255.
# Potrebno je skalirati sve elemente matrica na opseg od 0 do 1
#'''
return image/255
def matrix_to_vector(image):
#'''Sliku koja je zapravo matrica 28x28 transformisati u vektor sa 784 elementa'''
return image.flatten()
numbers = range(0, 40)
evens = numbers[2::2]
img = cv2.imread('houghlines5.jpg')
lines=houghLinesTransformation(img)
for x1, y1, x2, y2 in lines[0]:
cv2.line(img, (x1, y1), (x2, y2), (192, 0, 182), 2)
cv2.imwrite('houghlines66.jpg', img)
#ucitavanje modela
model = tf.keras.models.load_model('model/model2.h5')
results = []
videos = range(8, 10)
for video in videos:
cap = cv2.VideoCapture('videos/video-{0}.avi'.format(video))
#for i in range(10):
# if not i == 0:
# continue
#i=3
video_name = 'video-' + str(video) + '.avi'
#path_to_video = 'videos/' + video_name
#cap = cv2.VideoCapture(path_to_video)
#cap2 = cv2.VideoCapture("videos/video-9.avi")
#ret, frame = cap.read()
frame_num = 0
#b, g, r = cv2.split(frame)
print(video_name)
konacno = []
numbers = []
sum_digits = 0
while (True):
ret, frame = cap.read()
frame_num += 1
if ret is not True:
break
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
imgb = image_bin(gray)
cv2.imwrite('houghlines6637.jpg', imgb)
#digits_frame = get_digits_frame(frame)
#contours = cv2.findContours(digits_frame, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
#za frame detektujem liniju
#sum_digits=get_sum_of_digits(frame)
picture, region = select_roi(frame, imgb)
ready_for_ann = []
#display_image(picture)
for r in region:
broj = r[0].reshape(28, 28)
#cv2.imshow('frame2r', broj)
#cv2.waitKey(0)
priprema=matrix_to_vector(scale_to_range(broj))
pred=model.predict(priprema.reshape(1,28,28,1))
print(pred.argmax())
sum_digits+=pred.argmax()
cv2.imshow('frame2', frame)
if cv2.waitKey(1) == 30:
break
results.append('video-{0}.avi\t{1}\n'.format(video, sum_digits))
print(frame_num)
print(video_name)
print(sum_digits)
cap.release()
# sum = get_sum_of_digits(path_to_video)
# prediction_results.append({ 'video': video_name, 'sum': sum })
with open('out.txt', 'w') as file:
file.write('RA 70/2014 Ivana Antic\nfile\tsum\n')
for res in results:
file.write(res)
file.close()
os.system('python test.py')
print(evens)
#cap2.release()
cv2.destroyAllWindows()