Video cannot be loaded with Spyder in Python development environment

-- 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')

Termination condition of iterative algorithm

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

Video cannot be loaded with Spyder in Python development environment
Development environment in Python
Japanese can be used with Python in Docker environment
De-VBA Excel tool! Eliminate VBA tool with Python in environment where Python cannot be installed
Prepare Python development environment with Atom
Django cannot be installed in the development environment of pipenv + pyenv
Jupyter Notebook 6.0.2 cannot be installed in the Python 2.7 environment created in Anaconda
[Development environment] Python with Xcode [With screen transition]
Python modules with "-(hyphen)" cannot be removed
Introduced sip-4.14 in python3.2.2 environment with MacOS 10.7.4
Get started with Python in 30 minutes! Development environment construction & learn basic grammar
Switch the module to be loaded for each execution environment in Python
Project cannot be created with Python3.5 (Windows) + django1.7.1
Build Python development environment with Visual Studio Code
Create a virtual environment with conda in Python
[Python] Build a Django development environment with Docker
Python environment settings (virtualenv + pip cannot be done)
Investigation when import cannot be done with python
Work in a virtual environment with Python virtualenv.
Use Python in Anaconda environment with VS Code
Matplotlib etc. cannot be imported in VirtualEnv environment
Get a quick Python development environment with Poetry
About Python development environment
virtual environment in python
Python environment with docker-compose
python2.7 development environment construction
Framework development in Python
Virtual environment with Python 3.6
Slackbot development in Python
Virtualize (isolate) IBM i python development environment with chroot
Periodically execute python script with cron in venv environment
Allow real-time code checking in Python development with VS Code
Create a python development environment with vagrant + ansible + fabric
[Memorandum] Japanese keys cannot be used in python string.Template.substitute
list comprehension because operator.methodcaller cannot be used in python 2.5
Build a machine learning application development environment with Python
Operators ++,-cannot be used in python (difference from php)
When import matplotlib.pyplot cannot be done in pyenv-virtualenv environment
Python development environment with Windows + Anaconda3 + Visual Studio Code
QGIS3 Python plugin development environment construction with VSCode (macOS)
Manage Python runtime packages and development environment packages with Poetry
Python development environment with Windows + Python + PipEnv + Visual Studio Code
[Redash] Standard library cannot be used in python function
Set up a Python development environment with Sublime Text 2
Try running python in a Django environment created with pipenv
Scraping with selenium in Python
Unify the environment of the Python development team starting with Poetry
Working with LibreOffice in Python
Install Python environment with Anaconda
Scraping with chromedriver in python
Manage python environment with virtualenv
Debugging with pdb in Python
Solution when the image cannot be displayed with tkinter [python]
YouTube video management with Python 3
Commands often used in the development environment during Python implementation
Handle environment variables in Python
Scraping with Selenium in Python
Building a development environment for Android apps-creating Android apps in Python
Build python3 environment with ubuntu 16.04
Prepare Python development environment with Mac + Windows + VisualStudio Code (Windows version)
Prepare python3 environment with Docker