简体   繁体   English

cv2.videocapture 不适用于 Raspberry-pi

[英]cv2.videocapture doesn't works on Raspberry-pi

How can i make the cv2.VideoCapture(0) recognize the USB camera of raspberry-pi.如何让 cv2.VideoCapture(0) 识别树莓派的 USB 摄像头。

def OnRecord(self, evt):
    capture = cv2.VideoCapture(0)
    if (not capture.isOpened()):
        print "Error"
# video recorder
    fourcc = cv2.cv.CV_FOURCC(*'XVID')  # cv2.VideoWriter_fourcc() does not exist
    video_writer = cv2.VideoWriter.open("output.mp4", fourcc, 20, (640, 480), True)

    # record video
    while (capture.isOpened()):
        ret, frame = capture.read()
        if ret==True:
            video_writer.write(frame)
            cv2.imshow('Video', frame)
        else:
            break

def OnCancel(self, evt):
    capture.release()
    video_writer.release()
    cv2.destroyAllWindows()

but it only prints Error.但它只打印错误。

So i guess capture is not opening.所以我想捕获没有打开。 What might be the reason?可能是什么原因?

I tried this code from opencv documentation but doesn't worked out for me.我从 opencv 文档中尝试了这段代码,但对我没有用。

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi',fourcc, 20.0, (640,480))

while(cap.isOpened()):
    ret, frame = cap.read()
    if ret==True:
        frame = cv2.flip(frame,0)

        # write the flipped frame
        out.write(frame)

        cv2.imshow('frame',frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    else:
        break

# Release everything if job is finished
cap.release()
out.release()
cv2.destroyAllWindows()

Any help would be greatly appreciated.任何帮助将不胜感激。

为 linux 驱动程序加载正确的视频。

sudo modprobe bcm2835-v4l2

In my experience with CV2 replacing a webcam source on linux isn't always easy.根据我在 linux 上使用 CV2 替换网络摄像头源的经验并不总是那么容易。 How OpenCV works is it automatically draws from the systems default video source, which is known (usually) as video0. OpenCV 的工作原理是它自动从系统默认视频源中提取,通常称为 video0。 Unplug your usb webcam and go into a terminal and typing ls /dev/video*拔下 USB 网络摄像头并进入终端并输入ls /dev/video*

Remember the number it says.记住它说的数字。 Now plug in your USB webcam and type in ls /dev/video* again and look for any new /video, this is your USB webcam.现在插入您的 USB 网络摄像头并再次输入ls /dev/video*并查找任何新的 /video,这就是您的 USB 网络摄像头。 Now type mv /dev/videoX videoY while X is the number of your USB webcam and Y the original number.现在输入mv /dev/videoX videoY而 X 是您的 USB 网络摄像头的编号,Y 是原始编号。 This will replace your pi's default camera.这将替换您的 pi 的默认相机。

This isn't permanent as you will need to do this every time your pi starts up, an alternative to this is creating a bash file that runs on start up.这不是永久性的,因为每次 pi 启动时都需要这样做,替代方法是创建一个在启动时运行的 bash 文件。 Create a text file and copy the following into it.创建一个文本文件并将以下内容复制到其中。

#!/bin/bash
mv /dev/videoX videoY

(replace the X and Y of course) (当然替换 X 和 Y)

and place that in /etc/init.d directory of your pi.并将其放在 pi 的/etc/init.d目录中。 Don't forget you may need to use不要忘记您可能需要使用
chmod 755 /etc/init.d/FILENAME.sh
to give it permission to run允许它运行

Go to terminal and type lsusb and check whether the USB camera is recognized or not.转到终端并输入lsusb并检查 USB 摄像头是否被识别。 If it is recognized then try to give different device ID such as 1 or 2 or 3 rather than 0.如果它被识别,则尝试提供不同的设备 ID,例如 1 或 2 或 3 而不是 0。

looks Like you might have issue with codec, try using 'MJPG' codec instead of XVID.看起来您可能对编解码器有问题,请尝试使用“MJPG”编解码器而不是 XVID。 For more details have a look here有关更多详细信息,请查看此处

确保您使用的相机与 UVC 兼容,因为在基于 linux 的系统(如 raspi)上运行的 openCV 在使用非 UVC 相机时开始做一些愚蠢的事情。

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

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