-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathtracking.py
172 lines (128 loc) · 3.85 KB
/
tracking.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
import cv2
import cv
import uuid
import sys
import glob
import numpy as np
import random
from matplotlib import pyplot as plt
######################################
availablecolors = [(255, 0, 0), (0,0,255), (0,255,0), (255,255,0), (0,255,255), (0,0,0), (255,255,255), (125,220,221), (30,100,30)]
availablenames = ["batata","cenoura","joseph","maria","ferrugem","abacate","banana","laranja","alface","prego","cadeira","gandalf","frodo",
"gloin","toin","saruman","legolas"]
class People():
#New pedestrian detected
def __init__(self, x, y, w, h, color=None, label=None):
self.x = x
self.y = y
self.h = h
self.myarray = []
self.w = w
if color is None:
self.color = availablecolors[random.randint(0,len(availablecolors)-1)]
else:
self.color = color
if label is None:
self.label = availablenames[random.randint(0,len(availablenames)-1)]
else:
self.label = label
def get_label(self):
return self.label
def drawRect(self, image):
cv2.rectangle(image, (self.x, self.y), (self.x+self.w, self.y+self.h), self.color, 2)
return True
def drawLabel(self, image):
cv2.putText(image, self.label, (self.x, self.y-10), cv2.FONT_HERSHEY_PLAIN, 1, self.color, 2);
return True
def cropImg(self,image):
self.myarray = image[self.x:self.x+self.w, self.y:self.y+self.h]
self.generateHistogram(self.myarray)
def generateHistogram(self,img):
hist,bins = np.histogram(img.flatten(),256,[0,256])
self.hist = hist
return hist
def getHistogram(self):
return self.hist
# If True = New pedestrian
# Else, returns (x, y, w, h, color=None, label=None)
def compareHistograms(image,x,y,w,h, ppl):
#temporary crop detected target
tempCrop = image[x:x+w, y:y+h]
#generate temporary histogram to compare to existant ones
tempHist = generateHistogram(tempCrop)
if(len(ppl) > 0):
b = checkSimilarity(tempHist, ppl, image)
if(b):
return (b.x, b.y, b.w, b.h, b.color, b.label)
else:
return None
else:
return None
return None
def checkSimilarity(temphist, ppl, image):
results = {}
for i in ppl:
hist1 = i.generateHistogram(image)
distance = chi2_distance(temphist, hist1)
results[distance] = i
return i
results = sorted(results.items())
return None
# quisquare distance calculation for histograms
def chi2_distance(histA, histB, eps = 1e-10):
# compute the chi-squared distance
d = 0.5 * np.sum([((a - b) ** 2) / (a + b + eps)
for (a, b) in zip(histA, histB)])
# return the chi-squared distance
return d
def generateHistogram(img):
hist,bins = np.histogram(img.flatten(),256,[0,256])
return hist
def normalize_grayimage(image):
image = cv2.equalizeHist(image)
cv2.imshow("Equalized img", image)
return image
ppl = []
def main():
#Values for statistical evaluation
num_ppl = 0
num_pos_detections = 0
num_false_positivos = 0
num_false_negatives = 0
#IMG PATHS
images = glob.glob("videotracking/*.jpg")
print images
for filename in images:
imagePath = filename
cascPath = "cascades/haarcascade_pedestrian.xml"
pplCascade = cv2.CascadeClassifier(cascPath)
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = normalize_grayimage(gray)
pedestrians = pplCascade.detectMultiScale(
gray,
scaleFactor=1.2,
minNeighbors=10,
minSize=(32,96),
flags = cv2.cv.CV_HAAR_SCALE_IMAGE
)
#print "Found {0} ppl!".format(len(pedestrians))
#Draw a rectangle around the detected objects
for (x, y, w, h) in pedestrians:
a = compareHistograms(image,x,y,w,h,ppl)
if(a):
ppl.pop()
ppl.append(People(x,y,w,h, a[4], a[5]))
else:
ppl.append(People(x,y,w,h))
for i in ppl:
i.drawRect(image)
i.drawLabel(image)
i.cropImg(image)
outputname = "testoutput/output_"+filename.split("/")[1]
cv2.imwrite(outputname, image)
#cv2.imshow("Ppl found", image)
#cv2.waitKey(0)
return 0
if __name__ == '__main__':
main()