Home>

I want to save the video obtained from two webcams (Camera1, Camera2) as separate video files. At the same time, we are thinking of using Threading because there was one more processing that has to be done at the same time (music to play music, defined as MUSIC).
OpenCV video.write knows that it must be done in the main thread, and I thought about sending information of each frame on Queue, but I do not get much better if there are two cameras. The lower code can get a video file as desired, but the handling of Camera1 and Camera2 has an asymmetric, discomfort and anxiety (see "##########" position.
For the convenience of this process, it is necessary to synchronize the time of every frame of two images as much as possible, so it is necessary to repeat the capture and write to each video in order.
Is there something good? I would like to teach you.

Try to be tried

  • VIDEO1.Write and Video2.Write are performed in main thread
  • Video1.Write and Video2.Write Both T_Camera Thread
    → A video file is created in the current directory, but it can not be read.
  • Camera1 and Camera2 threads, Queue also produces two for Camera1 and two for Camera2
    → Can you generate multiple Q in the first place?
    → It was difficult to synchronize time (Threading.event or Threading.condition also tried, but the accuracy did not change)

Environment:
Windows 10
Anaconda (Python 3.6)

Code

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 ()
# Processing (Camera, MUSIC) definition 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 () # 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)
    # Suitably omitted
# Thread generation, start
T_Camera= Threading.Thread (Target= Camera, ARGS= (Q,))
t_camera.setdaemon (True)
T_MUSIC= Threading.Thread (Target= MUSIC)
t_camera.start ()
T_MUSIC.START ()
# Camera Continued Save Processing
BASE_TIME= time.Time ()
duration= 0
While (Duration <
Ten):
    ############ 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.ReleAs ()
CV2.DESTROYALWINDOWS ()

"OpenCV video.write knows in the main thread" → What is the source? VideoWriter can also be generated on the sub thread, and Videowriter.Write () itself should be able to run on the sub thread (securing of thread safety is the application side responsibility). Meanwhile, imshow () or waitkey (), etc. need to be performed on the main thread (UI thread with message loop), and the behavior when calling these from the sub thread is undefined. Aren't you confusing that? Also, who is FRAME_TIMING or INIT_TIME that is suddenly emerging during the code? After posting, first read "How to write a reproducible short sample code-Help Center-Stack Overflow".

sygh2021-06-15 07:27:57

Thank you very much. I felt I saw such a description on some website, but I think I'm sure I've remembered it. Also, Frame_Timing and init_time simply forgot to erase from the previous version. I'm sorry for the lack of confirmation! I think that it was a code that is difficult to read because it is difficult to read, but thank you very much for your help!

kkkkkkk2021-06-15 07:27:57

Did you read the page of the Help Center presented properly? Before apologizing or say, let's review questions and make an effort to fix and correct minimum mistakes.

sygh2021-06-15 07:27:57

Excuse me, so it was the first time I used such a question service, so I thought this story was over! I have fixed a question. I also write that because I found a solution by myself.

kkkkkkk2021-06-15 07:27:57
  • Answer # 1

    video.write () can be done in the thread, but before starting the thread

    Camera1.Release ()
    Camera2.Release ()
    Video1.Release ()
    Video2.ReleAs ()
    CV2.DESTROYALWINDOWS ()
    
    It seems that a non-replayable file has been generated because 

    has been done and the input from the camera was not read.
    Therefore, it was solved by putting T_MUSIC.JOIN () before this before.

  • Answer # 2

    video.write () can be done in the thread, but before starting the thread

    Camera1.Release ()
    Camera2.Release ()
    Video1.Release ()
    Video2.ReleAs ()
    CV2.DESTROYALWINDOWS ()
    
    It seems that a non-replayable file has been generated because 

    has been done and the input from the camera was not read.
    Therefore, it was solved by putting T_MUSIC.JOIN () before this before.