简体   繁体   中英

Grabbing Analog video into python using opencv

Well, it seems like my question had been asked many times before and unfortunately, no one replied. I hope someone will help.

I have an Easycap device that converts the analog images from my analog camera to digital signals through a USB port.

The device is identified to the system in the Device Manager under "Sound, Video and game controllers" category as "SMI Grabber Device".

I use a simple Python code to display the video from this device. I also have an embedded webcam in my laptop.

import numpy as np
import cv2
cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('frame',frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break
    if cv2.waitKey(1) & 0xFF == ord('s'):
        cv2.imwrite('screenshot.jpg',frame)



# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

First, when I unplug the Easycap, CaptureVideo(0) returns the embedded webcam video stream. However, when I plug the Easycap, an error appears:

"Traceback (most recent call last): File "C:\\Users\\DELL\\Desktop\\code\\cam.py", line 10, in cv2.imshow('frame',frame) error: ......\\src\\opencv\\modules\\highgui\\src\\window.cpp:261: error: (-215) size.width>0 && size.height>0"

Notice that, any number except 0 makes the program display the webcam image. So if I tried cap = cv2.CaptureVideo(1) , it will show the webcam, cap = cv2.CaptureVideo(20) is the same.

I also tried to enter "SMI Grabber Device" instead of 0 or 1 in the VideoCapture constructor function, but it didn't make any difference.

I'm using Windows 8, and I've installed the accompanying driver for Easycap. The software that comes with the driver (called ULead) works fine and display the CCTV camera video. I tried to display the images while I'm closing that program, and without, the result is the same.

I used before a C# program with Aforge library which had getCamList method or something which allowed me to choose the specific device I want to display from a comboBox. I can't find a similar function is opencv.

I'm using OpenCV 2.4.6. I didn't try the code on prior versions.

I really need to understand why this code doesn't work, knowing that I'm just a very beginner of opencv and image processing.

I hope someone can help.

I am using EasyCAP too. You must check that ret is True.

I am use below code

   while True:
      ret, frame = vc.read()
      if ret:
         break
      cv2.waitKey(10)
   h, w = frame.shape[:2]
   print h, w

   while True:
      ret, frame = vc.read()
      if ret:
         cv2.imshow(WID, frame)
      if cv2.waitKey(1) == 27:
        break

Let there be light!

On serious note, I struggled with the same problem and I hope this helps!

the original thread + answer

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM