Opencv-python Save Double Cam Video

Story: Hello I am Jayson - I want to use a double sided twin camera on a cell phone to record front and back on my surfboard, right now i am using background record. One sided. I use an ip68 cam in a bag with a mount here in so cal surfing and well, i want it to be double sided footage sooooo…

Goal: surf with a recording in progress double sided and a blank screen. two 720x720 make for 720x1440

Linux Code: Includes GTK that android has a tough time supporting…soo i cut it out for the blank screen android final product…

import cgitb
cgitb.enable()
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
out = cv2.VideoWriter(‘output.mp4’,cv2.VideoWriter_fourcc(*‘H264’), cap.get(cv2.CAP_PROP_FPS), (1440,720))

while(cap.isOpened()):

ret, frame = cap.read()
ret1, frame1 = cap1.read()
if ret == True:
    print (frame)
    both = np.column_stack((frame, frame1))
    out.write(both)
    cv2.imshow('Frame', both)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
else: 
    break

cap.release()
cap1.release()
out.release()
cv2.waitKey(0)
cv2.destroyAllWindows()

Android Code: with out GTK gui preview pane

import cgitb
cgitb.enable()
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)
out = cv2.VideoWriter(‘output.mp4’,cv2.VideoWriter_fourcc(*‘H264’), cap.get(cv2.CAP_PROP_FPS), (1440,720))

while(cap.isOpened()):

ret, frame = cap.read()
ret1, frame1 = cap1.read()
if ret == True:
    print (frame)
    both = np.column_stack((frame, frame1))
    out.write(both)
    cv2.imshow('Frame', both)

cap.release()
cap1.release()
out.release()

Issue: working toward android having opencv in termux and it CURRENTLY SAVES AN EMPTY VIDEO??? any help would be greatly appreciated…

please check return value (might be -1 for some webcams)

try another codec first (not sure, if it can write that (license problem))

congratulations, if you can run this (python!) even on android, but afaik, VideoWriter there has only support for MJPG /.avi, due to missing backends (no ffmpeg or gstreamer by default)

Thanks for the quick info and direction, Luckily I have it working and saving on linux right now with some updates, android tomorrow.

WORKING LINUX CODE…
`import cgitb
cgitb.enable()
import cv2
import numpy as np

cap = cv2.VideoCapture(0)
cap1 = cv2.VideoCapture(1)

out = cv2.VideoWriter(‘output.avi’,cv2.VideoWriter_fourcc(*‘XVID’), cap.get(cv2.CAP_PROP_FPS), (1440,720))

while(cap.isOpened()):

ret, frame = cap.read()
ret1, frame1 = cap1.read()
if ret == True:

    print (frame)
    reframe = cv2.resize(frame,(720, 720), interpolation = cv2.INTER_AREA)
    reframe1 = cv2.resize(frame1,(720, 720), interpolation = cv2.INTER_AREA)
    both = np.column_stack((reframe, reframe1))
    out.write(both)
    cv2.imshow('Frame', both)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
else: 
    break

cap.release()
cap1.release()
out.release()
cv2.waitKey(0)
cv2.destroyAllWindows()

`

Now using avi, working great…

1 Like

Well, building the dev environment on android is hard. I now have no errors, however I save an empty output.avi after…

  1. installing itspointless repo
  2. Pkg install cmake gcc ffmpeg libx264 gstreamer numpy
  3. Build opencv w cmake :
    cmake -D WITH_FFMPEG=ON -D WITH_GSTREAMER=ON -D BUILD_opencv_apps=OFF -D BUILD_opencv_python2=OFF OPENCV_FFMPEG_SKIP_BUILD_CHECK=ON -D BUILD_JAVA=OFF -D BUILD_ANDROID_EXAMPLES=OFF ../
  4. make -j4
  5. make install -B

Next, I run python android-twocam2.py

import cgitb
cgitb.enable()
import cv2
import numpy as np
#print (cv2.getBuildInformation())
cap = cv2.VideoCapture(0, cv2.CAP_FFMPEG)
#cap1 = cv2.VideoCapture(1, cv2.CAP_FFMPEG)
cap1 = cap
#out = cv2.VideoWriter('output.avi',cv2.VideoWriter_fourcc(*'XVID'), cap.get(cv2.CAP_PROP_FPS), (1440,720))                                  
out = cv2.VideoWriter('output2.mp4',cv2.VideoWriter_fourcc(*'mp4v'),25, (1440,720))

while(cap.isOpened()):

    ret, frame = cap.read()
    ret1, frame1 = cap1.read()
    if ret == True:

        print (frame)                                                                                                                                
        reframe = cv2.resize(frame,(720, 720), interpolation = cv2.INTER_AREA)

        reframe1 = cv2.resize(frame1,(720, 720), interpolation = cv2.INTER_AREA)                                                                     
        both = np.column_stack((reframe, reframe1))
        print ('''both:'''+both)
        out.write(both)
#        cv2.imshow('Frame', both)
#        if cv2.waitKey(1) & 0xFF == ord('q'):
#           break
#    else:
#        break

cap.release()
cap1.release()
out.release()
#cv2.waitKey(0)
#cv2.destroyAllWindows()

It runs and exits making a video that has no frames? Without an error…

check out.isOpened()

Thank you, the addition has made my code run much better…still a 44kb file output with empty frames though…i will work other options like avi with your advice…and update the forum question.

Hello all… the code works with video however not with cams…

code:

import cgitb
cgitb.enable()
import cv2
import numpy as np
#print (cv2.getBuildInformation())
cap = cv2.VideoCapture('/storage/emulated/0/DCIM/Camera/VID_20210502_110443.mp4', cv2.CAP_FFMPEG)
cap1 = cv2.VideoCapture('/storage/emulated/0/DCIM/Camera/VID_20210502_110552.mp4', cv2.CAP_FFMPEG)
out = cv2.VideoWriter('/storage/emulated/0/DCIM/output.mp4',cv2.VideoWriter_fourcc(*'mp4v'),25, (640,320))

while(cap1.isOpened()):

    ret, frame = cap.read()
    ret1, frame1 = cap1.read()
    if ret == True:

        print (frame)
        reframe = cv2.resize(frame,(320,320), interpolation = cv2.INTER_AREA)
        reframe1 = cv2.resize(frame1,(320, 320), interpolation = cv2.INTER_AREA)
        both = np.column_stack((reframe, reframe1))
        print (both)
        out.write(both)

cap.release()
cap1.release()
out.release()

the command:
termux-camera-info
Says they are @ 0 and 1

here is a link to the video on google drive:
opencv video product

again, on android you can only save videos as MJPG codec, with avi container

(unless you have built ffmpeg or gstreamer on the device, and linked opencv libs against it)

take another look at the build information (VideoIO section)

(and in most cases, it will write the container(header), but no images, if the codec is not supported, thus the “empty” video)

Thanks, I have one good thing to say, I have been able to progress already because of the help…I have the working linux version…and am close in the goal: android opencv with cams.
As it turns out opencv program is correct for my opencv build instructions and outputs have a smaller size for its creation value with mp4v.

new issue: on android cams are in /dev listed as 0 and 1 but are root permissioned and i do not know of a good way to use sudo or root the phone.