forked from ematiss/EyeSee
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
173 lines (145 loc) · 5.31 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
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
import numpy as np
import cv2
import os
import sys
import subprocess
import copy
from multiprocessing import Process, Queue
import time
#from common import clock, draw_str, StatValue
#import video
class FLANN(Process):
def __init__(self,frame_queue, output_queue, descriptor, trainKP, height, width, threshold):
Process.__init__(self)
self.frame_queue = frame_queue
self.output_queue = output_queue
self.height = height
self.width = width
self.stop = False
self.descriptor = descriptor
self.trainKP = trainKP
self.threshold = threshold
def get_frame(self):
if not self.frame_queue.empty():
return True, self.frame_queue.get()
else:
return False, None
def stopProcess(self):
self.stop = True
def search(self, gray):
queryKP,queryDesc=detector.detectAndCompute(gray,None)
bf = cv2.BFMatcher()
matches=bf.knnMatch(queryDesc, self.descriptor, k=2)
goodMatch=[]
for m,n in matches:
if(m.distance<0.75*n.distance):
goodMatch.append(m)
MIN_MATCH_COUNT=self.threshold
if(len(goodMatch)>=MIN_MATCH_COUNT):
tp=[]
qp=[]
for m in goodMatch:
tp.append(self.trainKP[m.trainIdx].pt)
qp.append(queryKP[m.queryIdx].pt)
tp,qp=np.float32((tp,qp))
H,status=cv2.findHomography(tp,qp,cv2.RANSAC,3.0)
trainBorder=np.float32([[[0,0],[0,self.height-1],[self.width-1,self.height-1],[self.width-1,0]]])
queryBorder=cv2.perspectiveTransform(trainBorder,H)
if self.output_queue.full():
self.output_queue.get_nowait()
self.output_queue.put(queryBorder)
return True
def run(self):
while not self.stop:
ret, frame = self.get_frame()
if ret:
self.search(frame)
if __name__ == '__main__':
frame_sum = 0
init_time = time.time()
def put_frame(frame):
if Input_Queue.full():
try:
Input_Queue.get( True, 0.2 )
except Queue.Empty:
print("Queue was empty")
Input_Queue.put(frame)
cap = cv2.VideoCapture(1)
#training section
detector=cv2.xfeatures2d.SIFT_create()
FLANN_INDEX_KDITREE=0
flannParam=dict(algorithm=FLANN_INDEX_KDITREE,tree=5)
flann=cv2.FlannBasedMatcher(flannParam,{})
trainImg=cv2.imread(sys.argv[1])
label = sys.argv[2]
threshold = int(sys.argv[3])
h,w,c = trainImg.shape
trainImgGray = cv2.cvtColor(trainImg,cv2.COLOR_BGR2GRAY)
trainKP,trainDesc=detector.detectAndCompute(trainImgGray,None)
threadn = cv2.getNumberOfCPUs()
if(threadn > 4):
threadn = 2
threaded_mode = True
process_list = []
Input_Queue = Queue(maxsize = 3)
Output_Queue = Queue(maxsize = 3)
for x in range((threadn -1)):
ft = FLANN(frame_queue = Input_Queue, output_queue = Output_Queue, descriptor = trainDesc, trainKP = trainKP, height = h, width = w, threshold = threshold)
ft.daemon = True
ft.start()
process_list.append(ft)
ch = cv2.waitKey(1)
cv2.namedWindow('Threaded Video', cv2.WINDOW_NORMAL)
found = False
announced = False
lastAnnounce = 0
delay = 0
points = np.array([])
while True:
ret, frame = cap.read()
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
put_frame(gray)
contour = cv2.Canny(gray, 100, 200, apertureSize = 3)
contour = cv2.dilate(contour, np.ones((5, 5)))
combined = cv2.addWeighted(contour, 0.5, gray, 0.5, 0)
combined = cv2.cvtColor(combined, cv2.COLOR_GRAY2BGR)
if not Output_Queue.empty():
result = Output_Queue.get()
points = copy.deepcopy(result)
cv2.polylines(combined,[np.int32(points)],True,(0,0,255),5)
font = cv2.FONT_HERSHEY_SIMPLEX
coordinates = (int(points[0][0][0] - 50), int(points[0][0][1] - 50))
cv2.putText(combined, label, coordinates, font, 2, (200,255,155), 5)
found = True
if not announced:
call = subprocess.Popen(['say', label])
announced = True
else:
lastAnnounce += 1
if lastAnnounce > 3:
lastAnnounce = 0
announced = False
else:
if found:
cv2.polylines(combined,[np.int32(points)],True,(0,0,255),5)
font = cv2.FONT_HERSHEY_SIMPLEX
coordinates = (int(points[0][0][0] - 50), int(points[0][0][1] - 50))
cv2.putText(combined, label, coordinates, font, 2, (200,255,155), 5)
delay = delay + 1
if delay == 15:
found = False
delay = 0
h,w,c = combined.shape
target = 400
if(h > target):
combined = cv2.resize(combined, (0,0), fx=target/h, fy=target/h)
h,w,c = combined.shape
l = int((w-h)/2)
r = h+l
combined = combined[0:h, l:r] #crop
stream = np.concatenate((combined, combined), axis=1)
cv2.imshow('frame',stream)
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
cv2.destroyAllWindows()