I'd like to save the videos I can get from the two webcams (camera1, camera2) as separate video files.At the same time, there was another process that I had to do (which plays music, defined as music), so I'm thinking about using threading.
When I found out that OpenCV video.write had to be done on the main thread, I thought about sending the information of each frame in queue, but maybe because of two cameras, it doesn't work very well.The code below gives you the video file you want, but the handling of camera1 and camera2 is asymmetrical and uncomfortable (see #####################################################################################
Due to the convenience of the subsequent processing, it is necessary to synchronize the time of each frame of the two images as much as possible, so we do not want to repeat the capture and writing of each image in order.
Is there any good way?Please let me know.
environment:
Windows 10
Anaconda (Python 3.6)
import time
import threading
import cv2
import pyaudio
import wave
import sys
import queue
import time
import asyncio
# VIDEO SETTING
camera1 = cv2.VideoCapture(1)
camera2 = cv2.VideoCapture(2)
fps1 = int(camera1.get(cv2.CAP_PROP_FPS))
w1 = int(camera1.get(cv2.CAP_PROP_FRAME_WIDTH))
h1 = int(camera1.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc1 = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video1 = cv2.VideoWriter('camera1.mp4', fourcc1, fps1,(w1,h1))
fps2 = int(camera2.get(cv2.CAP_PROP_FPS))
w2 = int(camera2.get(cv2.CAP_PROP_FRAME_WIDTH))
h2 = int(camera2.get(cv2.CAP_PROP_FRAME_HEIGHT))
fourcc2 = cv2.VideoWriter_fourcc('m', 'p', '4', 'v')
video2 = cv2.VideoWriter('camera2.mp4', fourcc2, fps2,(w2,h2))
frame_timing = [[], []]
init_time = time.time()
# queue generation
q = queue.Queue()
# camera, music definition of what to do in thread
def camera(q):
#base_time_0 = time.time()
# duration_0 = 0
while True:
# now_time_0 = time.time()
# duration_0 = now_time_0-base_time_0
# print(duration_0)
# print(time.time())
ret1, frame1 = camera1.read()# Get frame
frame_timing[0].append(time.time()-init_time)
ret2, frame2 = camera2.read()
frame_timing[1].append(time.time()-init_time)
cv2.imshow("camera1_side", frame1)
cv2.imshow("camera2_top", frame2)
video1.write(frame1)
########### video2.write(frame2)###########
q.put ([frame1, frame2])
if cv2.waitKey(1)&0xFF==ord('q'):
print("Ctrl+C")
break
def music():
time.sleep(10)
# I have omitted it appropriately.
# thread generation, starting
t_camera=threading.Thread(target=camera,args=(q,))
t_camera.setDaemon (True)
t_music=threading.Thread(target=music)
t_camera.start()
t_music.start()
# Continued camera save process
base_time = time.time()
duration = 0
while (duration <10):
############ video1.write(q.get()[0])###########
video2.write(q.get()[1])
now_time = time.time()
duration = now_time-base_time
camera1.release()
camera2.release()
video1.release()
video2.release()
cv2.destroyAllWindows()
Video.write() can also be done within a thread, but before starting the thread
camera1.release()
camera2.release()
video1.release()
video2.release()
cv2.destroyAllWindows()
It appears that the was not able to read the input from the camera, resulting in an unplayable file.
Therefore, it was resolved by adding t_music.join() before.
© 2024 OneMinuteCode. All rights reserved.