1
votes

I have written below code to read video from camera to display and save .

when I run the below code with option 0 in VideoCapture(0), it works fine and display my webcam video, when I change it to 1 in VideoCapture(1) to get the video from third party camera, I get the error.

I am using the 3rd party camera, with their software it plays the video , I need to capture using my python code ..

with the apbase code qt example also it plays the video

I am not able to play using the below python code

import cv2
import numpy as np
import time
def nothing(x):
    pass

cv2.namedWindow('images')
switch = 'Recording'
cv2.createTrackbar(switch, 'images',0,1,nothing)
cap = cv2.VideoCapture(0)

def writeVideo(frmae):
    pass

switchstatus = 0

currentpos = 0
fourccs = cv2.VideoWriter_fourcc(*'MJPG')
out = cv2.VideoWriter('sample.avi', fourccs, 20.0, (640,480))
created = 0
startrecord = 0



def RecordVideo(frame):
    global out
    global created;
    global startrecord
    print "In the Record video" ,created, startrecord
    if created == 0 and startrecord ==1:
        filename ='test.avi'
        filename=time.strftime("%Y-%m-%d-%H-%M-%S")+'.avi'
        print "filename", filename
        out = cv2.VideoWriter(filename,fourccs, 20.0, (640,480))
        created = 1;
        out.write(frame)
    elif created == 1 and startrecord ==1:
        out.write(frame)


def positionChanged(s):
    global currentpos
    global created
    global startrecord

    print "position changed", s
    currentpos = s
    if s==1:
        startrecord = 1
        created = 0
    else:
        startrecord = 0
    if created==1:
        created =0
        out.release()




def switchchanged(s):
    global switchstatus;
    if switchstatus != s:
        switchstatus = s
        positionChanged(s)



while(1):
    ret, frame = cap.read()
        RecordVideo(frame)
    cv2.imshow('images',frame)

    s = cv2.getTrackbarPos(switch,'images')

    switchchanged(s)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        out.release()
        cv2.destroyAllWindows()
        break

Error

File "C:\Python32Bit\video.py, lime 89, in cv2.imshow('images',frame)

eror :........\opencv\modules\hihggui\src\window.cpp:error:(-215) size.width>0 && size.height>0 in function cv::imshow

2
On Linux systems VideoCapture by default uses libv4l2 for video input devices. Install v4l-utils and do v4l2-ctl --all. See if it displays your camera. - zindarod
I am trying on windows - kcoder
For windows, it most probably uses DShow. With your display webcam, run cap = cv2.VideoCapture(0+cv2.CAP_DSHOW), if it works then it is using DSHOW. - zindarod
0+cv2.CAP_DSHOW is working for my webcam, and 1+cv2.CAP_DSHOW is giving same error . for testing purpose I cross checked, driver of that camera "aptina m031" are installed - kcoder
OK, lets give this one more try. cap = cv2.VideoCapture(0+cv2.CAP_FFMPEG). Btw, did you install OpenCV from source or opencv_python bindings? - zindarod

2 Answers

1
votes

I got this to work by disabling the built-in webcam from the windows hardware settings menu. I did this on a friend's computer so I don't have access to it now, but check this out. I believe windows won't let openCV use any other video capture device but the 0th, so you have to make any camera you want to use the first in the hardware list.

1
votes
cap = cv2.VideoCapture(0+cv2.CAP_DSHOW)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, 1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 1080)

it indeed solved my problem.