Create a simple video analysis tool with python wxpython + openCV

agenda

1.First of all 2. Demo of the finished tool 3. Video analysis with openCV 4. Create GUI with wxpython 5. Make an exe with pyinstaller

Introduction

I once wanted to know "at what time" and "what kind of person will come out" in the video. I tried to create a simple video analysis tool with wxpython and openCV that I was studying. I will introduce it here as a memo for myself.

Download it here GitLab ChuntaMovieAnalyzer

Demo of the finished tool

First of all, from the demo screen of the tool. Specify the video and output the html report. A human face is extracted and displayed every second of the video. You can change the interval between seconds and the maximum number of people to extract in the GUI.

capture_1.jpg capture_2.jpg

Download it here GitLab ChuntaMovieAnalyzer

Video analysis with openCV

The library used to analyze the video is openCV. This time, one frame of the video is used as image data. The face is detected from the image data and trimmed. The trimmed data is output as an image for html. I will introduce the code of the part that seems to be the point.

Load a video and output an image

The video is loaded and processed at intervals of the specified number of seconds using the total number of frames of the video and fps (number of frames per second).

def get_sampling_data_list(self, movie_path, report_path, interval_time, max_human, is_advance):
    sampling_data_list = []
    
    cap = cv2.VideoCapture(movie_path)
    
    if not cap.isOpened():
        print('cannot open movie file.')
        return []
    
    #Get the total number of frames
    all_frame_cnt = cap.get(cv2.CAP_PROP_FRAME_COUNT)
    
    #Calculate the number of frames to sample from the frame rate
    fps = cap.get(cv2.CAP_PROP_FPS)
    sampling_frame_num = math.floor( fps * interval_time )
    
    frame_cnt = 1
    
    while True:
        cap.set(cv2.CAP_PROP_POS_FRAMES, frame_cnt)
        ret, frame = cap.read()
        
        if ret:
            sampling_data = self.get_sampling_data(frame, frame_cnt, fps, report_path, max_human, is_advance)
            sampling_data_list.append(sampling_data)
            
            frame_cnt = frame_cnt + sampling_frame_num
        else:
            break
    
    return sampling_data_list

Processing for each frame

Convert 1 frame to image data ⇒ Detect face from image data ⇒ Trim ⇒ Output as image for html Processing is performed in the order of.

def get_sampling_data(self, frame, frame_cnt, fps, report_path, max_human, is_advance):
    data = sampling_data(frame_cnt, fps)
    
    face_cascade = cv2.CascadeClassifier(resourcePath('cascade/haarcascade_frontalface_default.xml'))
    src_gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faceInfoList = face_cascade.detectMultiScale(src_gray)
    
    human_cnt = 0
    
    for x, y, w, h in faceInfoList:
        #Exit the loop if the maximum number of people is exceeded
        if human_cnt >= max_human:
            break
        
        #trimming
        img_trim = frame[y:(y + h), x:(x + w)]
        
        data2 = None
        
        data2 = human_data()
        
        #Create a folder for image output
        if False == os.path.exists(report_path + '/img'):
            os.mkdir(report_path + '/img')
        
        #Image output
        file_name = str(frame_cnt) + '_' + str(human_cnt) + '.jpg'
        cv2.imwrite(report_path + '/img/' + file_name, img_trim)
        data2.set_img_src(report_path, file_name)
        
        data.append_human_data(data2)
        
        human_cnt = human_cnt + 1
    
    return data

GUI creation with wxpython

The source of the GUI part. I will introduce the code of the part that seems to be the point.

Video analysis processing Call processing

Since the video analysis process takes time, it will be busy unless it is called asynchronously. To avoid this, the calling part is asynchronous.

import threading
#Thread kick to prevent the screen from freezing because it takes a long time to process
analysis_thread = threading.Thread(target=exec_analyze, args=(self.analysis, self.btn, movie_path, interval_time, max_human, is_advance, token, report_path))
analysis_thread.start()
def exec_analyze(analysis, btn, movie_path, interval_time, max_human, is_advance, token, report_path):
    analysis.analyze(movie_path, int(interval_time), int(max_human), is_advance, token, report_path)
    
    wx.MessageBox('The html file was output to the specified folder.', 'Processing completed')

exe conversion

I used pyinstaller when converting to exe. It was necessary to devise when enclosing the external resource in the exe. It was carried out with reference to the following site.

Referenced URL Create an exe including resources with Pyinstaller

Execute the following at the command prompt

pyinstaller --onefile --icon=icon.ico -n chuntaMovieAnalyzer view_gui.py

Edit the created spec file and execute the following at the command prompt again

pyinstaller chuntaMovieAnalyzer.spec

Download it here GitLab ChuntaMovieAnalyzer

Recommended Posts

Create a simple video analysis tool with python wxpython + openCV
Create a directory with python
Create a virtual environment with Python!
Create a simple Python development environment with VSCode & Docker Desktop
Create a striped illusion with gamma correction for Python3 and openCV3
Make a simple OMR (mark sheet reader) with Python and OpenCV
Create a video player with PySimpleGUI + OpenCV 3 Addition of mask function
Create a simple Python development environment with VS Code and Docker
Create a USB boot Ubuntu with a Python environment for data analysis
Creating a simple PowerPoint file with Python
Make a video player with PySimpleGUI + OpenCV
Create a dummy image with Python + PIL.
Save video frame by frame with Python OpenCV
Machine learning with python (2) Simple regression analysis
Create miscellaneous Photoshop videos with Python + OpenCV ③ Create miscellaneous Photoshop videos
Create a simple web app with flask
Create a word frequency counter with Python 3.4
I made a simple blackjack with Python
Create a tool to automatically furigana with html using Mecab from Python3
Create a frame with transparent background with tkinter [Python]
Create a LINE BOT with Minette for Python
Create a virtual environment with conda in Python
Create a page that loads infinitely with python
Start a simple Python web server with Docker
[Note] Create a one-line timezone class with python
You can easily create a GUI with Python
Create a color bar with Python + Qt (PySide)
Create a simple momentum investment model in Python
Create a decision tree from 0 with Python (1. Overview)
Create a new page in confluence with Python
Create a color-specified widget with Python + Qt (PySide)
A simple to-do list created with Python + Django
Create a Photoshop format file (.psd) with python
Simple sales tool creation with Python GUI: Quote creation
Create a Python console application easily with Click
Create a translation tool with the Translate Toolkit
Create a simple reception system with the Python serverless framework Chalice and Twilio
Data analysis with python 2
Binarization with OpenCV / Python
Voice analysis with python
Create a Python environment
Voice analysis with python
Data analysis with Python
Try to make a command standby tool with python
[OpenCV / Python] I tried image analysis of cells with OpenCV
[Python] Create a ValueObject with a complete constructor using dataclasses
Make a simple Slackbot with interactive button in python
Why not create a stylish table easily with Python?
Create a python development environment with vagrant + ansible + fabric
Let's make a simple game with Python 3 and iPhone
Create miscellaneous Photoshop videos with Python + OpenCV ④ Deal with issues
Calculate the regression coefficient of simple regression analysis with python
Problems when creating a csv-json conversion tool with python
Fill the background with a single color with OpenCV2 + Python
Make a CSV formatting tool with Python Pandas PyInstaller
Create a Layer for AWS Lambda Python with Docker
[python] Create a date array with arbitrary increments with np.arange
Create miscellaneous Photoshop videos with Python + OpenCV ② Create still image Photoshop
[Python] How to create a 2D histogram with Matplotlib
[Python] Create a Tkinter program distribution file with cx_Freeze
What is God? Make a simple chatbot with python