/
main_svm.py
104 lines (81 loc) · 2.41 KB
/
main_svm.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
# verificar a feature 11 (pode ser a representação do não buraco)
from Texture import Texture
import cv2
from Slicer import Slicer
from os import listdir
from Threshold import Threshold
from tensorflow.keras.models import model_from_json
from Hog import Hog
from Boxes import Boxes, Box
from Cells import Cells
from timeit import default_timer as timer
from multiprocessing import Pool
from Config import Config
import pickle
from Network import Network
#---------------------- PARAMETERS ----------------------#
threads = 1
path = "data/frames/frames0/"
path_out = "data/frames/out_frames0/"
sizew=25
sizeh=25
thresh = 0.5
haralick_feature = 3
hog_block_size = 2
hog_cell_size = 8
hog_orientations = 2
resize_img = (100,100)
size_video = (500,200)
#---------------------- PARAMETERS ----------------------#
#------------------- CONST & OBJECTS --------------------#
files = list(listdir(path))
batch_size = int(len(files)/threads)
#------------------- CONST & OBJECTS --------------------#
def tracker(i):
boxes = Boxes(len(files))
tex = Texture()
ims = Slicer()
th = Threshold()
hog = Hog()
cells = Cells(sizeh,sizew)
names = list(listdir(path))
names = names[i:i+batch_size]
f = open('data/net/dataset_184_nets_svm/svm_c_2_p_8_o_2.svm','rb')
clf = pickle.load(f)
f.close()
while(len(names)>0):
index = len(names)-1
name = names.pop()
img = cv2.imread(path+name)
img_grey = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
images,coords = ims.split(img_grey,sizeh,sizew,sizeh,sizew)
signal_p = tex.analize(images,haralick_feature)
indexes = th.threshold_mean(signal_p)
coords_f = []
for x in indexes:
coords_f.append(coords[x])
coords = cells.group(coords_f)
coords = cells.reorganize_coords(coords)
for c in coords:
part_img = img_grey[c[2]:c[3],c[0]:c[1]]
part_img = cv2.resize(part_img,resize_img)
part = hog.runOne(part_img,orient=hog_orientations,pixels=hog_cell_size,cells=hog_block_size)
part = part.reshape((1,part.shape[0]))
Y = clf.predict(part)
if(Y>thresh and c[0]!=0 and c[1]!=500 and abs(c[0]-c[1])*abs(c[2]-c[3])>625 ):
cells.box2(img,c)
box = Box()
box.box = [c[0],c[2],c[1],c[3]]
boxes.add(index,box)
cv2.imwrite(path_out+name,img)
boxes.save('boxes_predicted_svm_25.b')
if __name__ == '__main__':
i=0
pos = []
for t in range(threads):
pos.append(i*batch_size)
i+=1
ti = timer()
with Pool(threads) as p:
p.map(tracker, pos)
print(timer()-ti)