从 pyside/opencv 访问网络摄像头 [英] Accessing a webcam from pyside / opencv

查看:161
本文介绍了从 pyside/opencv 访问网络摄像头的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在访问 pyside/opencv 项目中的网络摄像头时遇到问题.

I have problems accessing a webcam in a pyside / opencv project.

这是一个精简的例子,产生了我面临的问题:

This is a stripped down example that produces the problem I face:

from PySide import QtCore, QtGui
import cv, cv2, time, ImageQt

app = QtGui.QApplication([])

while True:
    camcapture = cv.CaptureFromCAM(0)
    cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
    cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720);

    frame = cv.QueryFrame(camcapture)
    image = QtGui.QImage(frame.tostring(), frame.width, frame.height, QtGui.QImage.Format_RGB888).rgbSwapped()
    pixmap = QtGui.QPixmap.fromImage(image)

app.exec_()

我在这里看到两个问题.首先:显然我发现使用 v4l2 访问网络摄像头存在问题,否则在 python 中可以正常工作(使用其他应用程序):

I see two problems here. First: aparently I see an issue using v4l2 to access the webcam which otherwise works fine from python (using other applications):

python ./test.py 
VIDIOC_QUERYMENU: Invalid argument
[... and countless more entries which don#t worry me ...]
VIDIOC_QUERYMENU: Invalid argument
libv4l2: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl S_FMT
libv4l2: error setting pixformat: Device or resource busy
libv4l1: error setting pixformat: Device or resource busy
HIGHGUI ERROR: libv4l unable to ioctl VIDIOCSPICT

然后有一个不清楚的关机问题,但不是我现在的主要问题:

Then there is a shutdown problem that is unclear, but not my main problem right now:

*** glibc detected *** python: double free or corruption (fasttop): 0x00000000029368f0 ***
======= Backtrace: =========
/lib64/libc.so.6(+0x78b66)[0x7f3539f06b66]
/usr/lib64/tls/libnvidia-tls.so.304.64(+0x1cc1)[0x7f352e448cc1]
======= Memory map: ========
00400000-00401000 r-xp 00000000 08:02 3937950                            /usr/bin/python2.7
00600000-00601000 r--p 00000000 08:02 3937950                            /usr/bin/python2.7
00601000-00602000 rw-p 00001000 08:02 3937950                            /usr/bin/python2.7
01631000-03cd3000 rw-p 00000000 00:00 0                                  [heap]
7f351b6dc000-7f351b6dd000 rw-p 00000000 00:00 0 
7f351b6dd000-7f351b773000 rw-s 001c2000 00:05 5759                       /dev/video0
7f351b773000-7f351b809000 rw-s 0012c000 00:05 5759                       /dev/video0
[... and so on ... and so on ... ]

我的问题?我无法从输出中理解.我尝试了代码的许多变体,但通常会遇到同样的问题.所以我想这不是一个特定的代码细节,而是我的设置或我的方法的一些普遍问题.这是环境:- x86-64 平台 (MacBook Pro) 上的 Linux 操作系统 (openSUSE-12.2)- opencv 2.4.3- libqt4 4.8.4- 蟒蛇 2.7.3

My question? I fail to make sense from the output. I tried many variants of the code, but usually run into the same problem. So I guess it is not a specific code detail, but some general issue with my setup or my approach. This is the environment: - Linux OS (openSUSE-12.2) on an x86-64 platform (MacBook Pro) - opencv 2.4.3 - libqt4 4.8.4 - python 2.7.3

这里有什么问题?我的代码就像所有这些示例的所有变体一样.我想念什么?

What's wrong here? My code is like all those examples out there in all their variants. What do I miss?

推荐答案

首先,你不应该在 while 循环中使用 cv.CaptureFromCAM(0),因为这就是导致资源繁忙"冲突和内存转储.

To start with, you should not have cv.CaptureFromCAM(0) inside the while loop, as that is what is causing the "resource busy" collisions and the memory dump.

您可能需要减慢 while 循环的速度.您可以实现 cv2.waitKey() 或使用 time.sleep().

You will likely need to slow down your while loop. You can implement cv2.waitKey() or use time.sleep().

之后,您将需要完成 Qt 实现.(这似乎是一个单独的问题.)

After that, you will need to finish your Qt implementation. (which seems to be a separate issue.)

这是对您的示例进行的简单重写:

Here is a bare bones re-write of your example:

import cv, cv2, time, sys

camcapture = cv.CaptureFromCAM(0)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_WIDTH, 1280)
cv.SetCaptureProperty(camcapture,cv.CV_CAP_PROP_FRAME_HEIGHT, 720)

while True:
    frame = cv.QueryFrame(camcapture)
    ... GUI stuff ...
    time.sleep(.05)

使用 cv2 代替:

camcapture = cv2.VideoCapture(0)
while True:
    _, frame = camcapture.read()
    ... GUI stuff ...
    time.sleep(.05)

这篇关于从 pyside/opencv 访问网络摄像头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆