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
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
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.
Download it here GitLab ChuntaMovieAnalyzer
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.
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
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
The source of the GUI part. I will introduce the code of the part that seems to be the point.
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')
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