-
Notifications
You must be signed in to change notification settings - Fork 29
/
Main.py
107 lines (92 loc) · 3.24 KB
/
Main.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
# -*- coding: utf-8 -*-
"""
@author: Ankit Singh
"""
from ImageHandler import Imagehandler
from svmFile import svmClass
from utility import Pyramid,SlidingWindow
import yaml
import glob
import os
import random
import cv2 as cv
def App():
with open("config.yml", 'r') as ymlfile:
cfg = yaml.load(ymlfile)
IOPlaces = cfg['Main']
input = IOPlaces['Input']
output = IOPlaces['Output']
# print(output)
directorypathpos = input['positive']
os.chdir(directorypathpos)
filesTypes = cfg['FileType']
images = []
for filetype in filesTypes:
images.extend(glob.glob("*."+filetype))
DataX = []
DataY = []
paths = [directorypathpos+image for image in images]
###adding and Computing HOG Vector
# print(xrange(len(paths)))
#image sizez are 96* 160
for i in xrange(len(paths)):
# for i in xrange(100):
# Image = cv.imread(paths[i],cv.IMREAD_COLOR)
obj = Imagehandler(paths[i])
HogVector = obj.ImagesToTiles(16,16)
DataX.append(HogVector)
DataY.append(1)
directorypathneg = input['Negative']
os.chdir(directorypathneg)
images = []
for filetype in filesTypes:
images.extend(glob.glob("*."+filetype))
paths = [directorypathneg+image for image in images]
# print(xrange(len(paths)))
for i in xrange(len(paths)):
# for i in xrange(10):
Image = cv.imread(paths[i],cv.IMREAD_UNCHANGED)
for j in xrange(10):
rand = random.randint(0,50)
img = Image[rand:rand+160,rand:rand+96]
obj = Imagehandler(paths[i],img)
HogVector = obj.ImagesToTiles(16,16)
# print(len(HogVector))
DataX.append(HogVector)
DataY.append(0)
###train and test
svmObj = svmClass((DataX,DataY))
svmObj.trainData()
####predict
os.chdir(output)
images = []
for filetype in filesTypes:
images.extend(glob.glob("*."+filetype))
paths = [output+image for image in images]
# for i in xrange(len(paths)):
for i in xrange(5):
Image = cv.imread(paths[i],cv.IMREAD_UNCHANGED)
imageHeight,imageWidth = Image.shape[:2]
imageHeight = int(imageHeight/160)*160
imageWidth = int(imageWidth/96)*96
Image = cv.resize(Image,(imageWidth,imageHeight), interpolation = cv.INTER_CUBIC)
for scaledImage in Pyramid(Image,2,(128,64)):
for (x,y,window) in SlidingWindow(scaledImage,(14,14),(160,96)):
if( window.shape[:2] != (160,96)):
continue
oi = Imagehandler(paths[i],window)
Hog = oi.ImagesToTiles(16,16)
# print(len(Hog))
val = svmObj.PredictData([Hog])
val = val[0]
print(val)
if val[1]> 0.8:
clone = scaledImage.copy()
cv.rectangle(clone, (x, y), (x + 160, y + 96), (0, 255, 0), 2)
cv.imshow("Window", clone)
cv.waitKey(1)
if __name__ == "__main__":
cam = cv.VideoCapture(0)
App()
cam.release()
cv.destroyAllWindows()