-- coding: utf-8 --
import cv2.cv as cv import cv2 import numpy as np
MAX_FEATURES = 500 # RADIUS2 = 25 DETECT_TURM = 20 #Determine how many frames to use WINDOW_NAME = u'object tracking'.encode ('cp932')
criteria = (cv2.TERM_CRITERIA_MAX_ITER | cv2.TERM_CRITERIA_EPS , 20 , 0.03)
class App:
def __init__(self):
cv2.namedWindow(WINDOW_NAME)
cv2.setMouseCallback(WINDOW_NAME , self.on_mouse)
self.src = cv2.VideoCapture('crossing_accident.avi')
if not self.src.isOpened():
print u'Cannot print' import sys sys.exit()
self.points_now = None
self.detect_count = DETECT_TURM Count as many as #DETECT_TURM
def auto_detect(self):
self.points_now = cv2.goodFeaturesToTrack(self.img_now , MAX_FEATURES , 0.01 , 10)
cv2.cornerSubPix(self.img_now , self.points_now , (10 , 10) , (-1 , -1) , criteria)
def remove_points(self):
index = 0
while index < len(self.points_now):
if self.status[index] == 0:
self.points_now = np.delete(self.points_now , index , 0)
self.status = np.delete(self.status , index , 0)
index -= 1
index += 1
def on_mouse(self , event , x , y , flags , param):
if event != cv2.EVENT_LBUTTONDOWN:
return
index = -1
mini = RADIUS2
count = 0
if self.points_now != None:
for point in self.points_now:
dx = x - point[0][0]
dy = y - point[0][1]
r2 = dx * dx + dy * dy
if r2 <= mini:
index = count
mini = r2
break
count += 1
if index >= 0:
self.points_now = np.delete(self.points_now , index , 0)
self.status = np.delete(self.status , index , 0)
else:
if self.points_now != None and len(self.points_now) >= MAX_FEATURES:
print u'No more feature points can be extracted' return
if self.points_now == None:
self.points_now = np.array([[[x , y]]] , np.float32)
else:
self.points_now = np.append(self.points_now , [[[x , y]]] , axis = 0).astype(np.float32)
cv2.cornerSubPix(self.img_now , self.points_now , (10 , 10) , (-1 , -1) , criteria)
def chk_detect(self):
self.detect_count-= 1 Subtract 1 from the number of DETECT_TURM assigned to # self.detect_count if self.detect_count == 0: #If self.detect_count becomes 0 self.detect_count = DETECT_TURM Return # self.detect_count to the value of the first DETECT_TURM self.auto_detect () #Automatically extract feature points
def run(self):
while True:
self.chk_detect () #While the video is playing, feature points are automatically extracted every number of frames specified by DETECT_TURM. retval , frame = self.src.read()
if frame is None:
break
self.img_now = cv2.cvtColor(frame , cv2.COLOR_BGR2GRAY)
if self.points_now != None and len(self.points_now) > 0:
points_pre = self.points_now
self.points_now , self.status , err = \
cv2.calcOpticalFlowPyrLK(self.img_pre , self.img_now , points_pre , winSize = (10 , 10) , maxLevel = 3 , criteria = criteria , flags = 0 , minEigThreshold = 0.01)
print 'aaaa'
print len(points_pre)
print len(self.points_now)
#self.remove_points()
#for point in self.points_now:
for i in range (len (points_pre)): Substitute "0 ~ number of array elements" in #points_pre into i, and repeat the process during "0 ~ number of array elements" diff_x = points_pre [i] [0] [0] --self.points_now [i] [0] [0] # Take the difference between the x-coordinates of the points in the previous frame and the points in the current frame. diff_y = points_pre [i] [0] [1] --self.points_now [i] [0] [1] # Take the difference between the y-coordinates of the points in the previous frame and the points in the current frame. point = self.points_now[i] # diff = diff_x * diff_x + diff_y * diff_y # Take the distance between the points in the previous frame and the points in the current frame in the same way as the three-square theorem. if diff> 1 and diff <2: #If the distance between the point in the previous frame and the point in the current frame is greater than 1 and less than 2. cv2.circle (frame, (point [0] [0], point [0] [1]), 4, (0, 0, 255 --255 -(1 --diff)), -1, 8, 0) #Draw a point. The color of the dots is changed according to the moving speed of the object. If it is early, it will be a red dot, and if it is late, it will be a black dot. #cv2.circle(frame , (point[0][0] , point[0][1]) , 4 , (0 , 0 , 255random.random()) , -1 , 8 , 0)
self.remove_points()
cv2.imshow(WINDOW_NAME , frame)
self.img_pre = self.img_now.copy()
key = cv2.waitKey(33)
if key == 27:
break
elif key == 67 or key == 99:
self.points_now = None
elif key == 32:
self.auto_detect()
cv2.destroyAllWindows()
self.src.release()
if name == 'main': App().run()
When I run the above program in Spyder, I get the following error.
runfile('C:/Users/tatomi/Desktop/chase/chasing.py', wdir='C:/Users/tatomi/Desktop/chase') Output not possible An exception has occurred, use %tb to see the full traceback.
SystemExit
I feel like I haven't taken a pass or something, what should I do?
Recommended Posts