简体   繁体   English

使用opencv将模拟视频捕获到python中

[英]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. 我有一个Easycap设备,可以通过USB端口将模拟摄像机中的模拟图像转换为数字信号。

The device is identified to the system in the Device Manager under "Sound, Video and game controllers" category as "SMI Grabber Device". 该设备在“设备管理器”中的“声音,视频和游戏控制器”类别下被标识为“ SMI抓取器设备”。

I use a simple Python code to display the video from this device. 我使用一个简单的Python代码来显示来自此设备的视频。 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. 首先,当我拔下Easycap时,CaptureVideo(0)返回嵌入式网络摄像头视频流。 However, when I plug the Easycap, an error appears: 但是,当我插入Easycap时,出现错误:

"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" “跟踪(最近一次通话最近):文件“ C:\\ Users \\ DELL \\ Desktop \\ code \\ cam.py”,cv2.imshow('frame',frame)中的第10行错误:...... src \\ opencv \\ modules \\ highgui \\ src \\ window.cpp:261:错误:(-215)size.width> 0 && size.height> 0“

Notice that, any number except 0 makes the program display the webcam image. 请注意,除0以外的任何数字都会使程序显示网络摄像头图像。 So if I tried cap = cv2.CaptureVideo(1) , it will show the webcam, cap = cv2.CaptureVideo(20) is the same. 因此,如果我尝试使用cap = cv2.CaptureVideo(1) ,它将显示网络摄像头, cap = cv2.CaptureVideo(20)相同。

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. 我还尝试在VideoCapture构造函数中输入“ SMI Grabber Device”而不是0或1,但这没有任何区别。

I'm using Windows 8, and I've installed the accompanying driver for Easycap. 我正在使用Windows 8,并且已经安装了Easycap随附的驱动程序。 The software that comes with the driver (called ULead) works fine and display the CCTV camera video. 驱动程序附带的软件(称为ULead)可以正常工作,并显示CCTV摄像机视频。 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. 我Aforge库中的C#程序,它收到使用getCamList方法或一些东西,让我选择我想从一个ComboBox显示特定设备。 I can't find a similar function is opencv. 我找不到类似的功能是opencv。

I'm using OpenCV 2.4.6. 我正在使用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. 我真的很想了解为什么这段代码不起作用,因为我只是opencv和图像处理的初学者。

I hope someone can help. 我希望有人能帮帮忙。

I am using EasyCAP too. 我也在使用EasyCAP。 You must check that ret is True. 您必须检查ret为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 原始主题+答案

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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