-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathlivetest.py
146 lines (132 loc) · 4.14 KB
/
livetest.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
#Import libraries
import cv2
import cPickle
import numpy as np
#start Video capture
cap = cv2.VideoCapture(0)
'''
Trainer File
'''
#initialize an empty image to check for all contour position throughout
last=np.zeros((480,640,1),np.uint8)
#Open file for moments
fmina = open("minm.txt","r")
fmaxa = open("maxm.txt","r")
#Extract the training sets to two list mina and maxa
mina = fmina.read().split(" ")
maxa = fmaxa.read().split(" ")
#close the connections
fmina.close()
fmaxa.close()
#Open file for histograms
fmina = open("minh.txt","r")
fmaxa = open("maxh.txt","r")
#Extract the training sets to two list mina and maxa
minh = fmina.read().split(" ")
maxh = fmaxa.read().split(" ")
#Find the largest contour
count=0
finaltl=[]
finalbr=[]
while True:
top_left = (0,0)
bottom_right = (0,0)
ret,im = cap.read()
img = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
(thresh,imgg)=cv2.threshold(img,10,255,cv2.THRESH_BINARY | cv2.THRESH_OTSU)
ret,thresh = cv2.threshold(imgg,214,255,0)
contours,hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
tmp = cPickle.dumps(contours)
contours = cPickle.loads(tmp)
areas=[]
areatmp=[]
for c in contours:
area = cv2.contourArea(c, False)
areas.append(area)
areatmp.append(area)
areas.sort()
areas.reverse()
try:
ind = areatmp.index(areas[1])
ctrs = contours[ind]
except IndexError:
pass
img1=np.zeros((480,640,1),np.uint8)
cv2.drawContours(img1,ctrs,-1,(255,255,255),3)
flat = ctrs.flatten()
ptsx=[]
ptsy=[]
for i in range(0,len(flat),2):
ptsx.append(flat[i])
ptsy.append(flat[i+1])
top_left = (min(ptsx),min(ptsy))
bottom_right = (max(ptsx),max(ptsy))
#Till here we have identified the bounding box for the object of interest
#Calculate histogram values for the bounded box
mask=np.zeros((bottom_right[1]-top_left[1]+1,bottom_right[0]-top_left[0]+1,1),np.uint8)
for i in range(top_left[1],bottom_right[1]+1):
for j in range(top_left[0],bottom_right[0]+1):
mask[i-top_left[1]][j-top_left[0]] = img[i][j]
vals=mask.mean(axis=2).flatten()
counts,bins=np.histogram(vals,range(257))
#plot histograms between 0...255
sumh=counts[0]
g=0
arr=[]
for i in range(1,256):
if i%8!=0:
sumh+=counts[i]
counts[i]=0
else:
t=counts[i]
counts[i]=sumh
arr.append(sumh)
sumh=t
#Calculate moments for the bounded box
mat = np.zeros((bottom_right[1]-top_left[1]+1,bottom_right[0]-top_left[0]+1,1),np.uint8)
## cv2.rectangle(img1,top_left,bottom_right,(127,127,127))
## cv2.rectangle(im,top_left,bottom_right,(127,127,127))
for i in range(top_left[1],bottom_right[1]+1):
for j in range(top_left[0],bottom_right[0]+1):
mat[i-top_left[1]][j-top_left[0]] = img[i][j]
moms=[]
moms = cv2.HuMoments(cv2.moments(mat)).flatten()
sum1=0
sum2=0
# print arr
for i in range(7):
sum1 += (moms[i]-np.float64(mina[i]))*(np.float64(maxa[i])-moms[i])
for i in range(31):
# print int(np.float64(minh[i]))
sum2 += (arr[i]-int(np.float64(minh[i])))*int((np.float64(maxh[i]))-arr[i])
# print sum1
# print sum2
if sum1>0 and sum2>0:
#print top_left,bottom_right
x=(bottom_right[0]+top_left[0])/2
y=(bottom_right[1]+top_left[1])/2
if(y>250 and y <350 ):
print " Correct "
elif(y<250):
print " Go left "
elif(y>350):
print "Go right "
sum1=0
sum2=0
cv2.rectangle(img,top_left,bottom_right,(127,127,127))
cv2.rectangle(im,top_left,bottom_right,(127,127,127))
cv2.rectangle(last,top_left,bottom_right,(127,127,127))
cv2.imshow("PICK",mat)
cv2.imshow("BIN",img1)
cv2.imshow("EXE",im)
cv2.imshow("asdas",last)
## if cv2.waitKey(10) == 120:
## continue
## else:
## break
#### continue
if cv2.waitKey(10)==27:
cv2.destroyAllWindows()
break
print finaltl,"top left"
print finalbr,"bottom right"