opencv-python 相机读取错误

Posted

技术标签:

【中文标题】opencv-python 相机读取错误【英文标题】:opencv-python camera read erroes 【发布时间】:2018-08-06 07:48:40 【问题描述】:

我遇到了错误

VIDEOIO 错误:V4L2:OpenCV 不支持传入图像的像素格式

无法停止流:设备或资源繁忙

VIDEOIO ERROR: V4L: can't open camera by index 0

尝试运行代码时

import time
import datetime
import cv2
import boto3

rekog = boto3.client('rekognition')
video = cv2.VideoCapture(0)


class VideoCamera(object):
    def __init__(self):
        # Using OpenCV to capture from device 0. If you have trouble capturing
        # from a webcam, comment the line below out and use a video file
        # instead.
        # self.video = cv2.VideoCapture(0)
        # If you decide to use video.mp4, you must have this file in the folder
        # as the main.py.
        # self.video = cv2.VideoCapture('video.mp4')
        print('camera loaded')

    def __del__(self):
        self.video.release()

    def get_frame(self):
        # success, image = self.video.read()
        success, image = video.read()
        overlay = image.copy()
        overlayTxt = image.copy()

        h, w = image.shape[:2]
        regImg = cv2.resize(image, (int(0.2 * w), int(0.2 * h)))
        _, newjpeg = cv2.imencode('.jpg', regImg)
        imgbytes = newjpeg.tobytes()
        t0 = time.time()
        resp = rekog.detect_labels(Image='Bytes': imgbytes)

        cv2.rectangle(overlay, (10, 10), (300, 50 + 50 * len(resp['Labels'])), (0, 0, 0), -1)
        alpha = 0.3
        cv2.addWeighted(overlay, alpha, image, 1 - alpha, 0, image)

        cnt = 1
        now = str(datetime.datetime.now())
        cv2.putText(image, now, (20, 40 * cnt), cv2.FONT_HERSHEY_SIMPLEX, 0.4, (255, 255, 255), 1)
        cnt = cnt + 1
        for label in resp['Labels']:
            outTxt = label['Name'] + ' (' + str(int(label['Confidence'])) + '%)'
            cv2.putText(image, outTxt, (20, 40 * cnt), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (255, 255, 255), 1)
            cnt = cnt + 1

        # We are using Motion JPEG, but OpenCV defaults to capture raw images,
        # so we must encode it into JPEG in order to correctly display the
        # video stream.
        ret, jpeg = cv2.imencode('.jpg', image)
        t0 = time.time()
        resp = rekog.detect_labels(Image='Bytes': imgbytes)
        print(" ---> ".format((time.time() - t0), resp["Labels"]))

        return jpeg.tobytes()

相机工作正常,在其他应用程序中对其进行了测试。

这些错误的原因是什么?

【问题讨论】:

【参考方案1】:

试试这个命令:

sudo modprobe bcm2835-v4l2

此命令帮助启动和触发相机。

【讨论】:

确保您的相机已启用(打开)。

以上是关于opencv-python 相机读取错误的主要内容,如果未能解决你的问题,请参考以下文章

OpenCV-python视频入门

OpenCV-Python实战(番外篇)——OpenCV中绘制模拟时钟显示当前时间

OpenCV - Python 断言错误:SAD 算法 - 立体相机视差图计算

OpenCV-Python实战(12)——一文详解AR增强现实

OpenCV-Python 视频读取

Opencv-Python:图像尺寸图像的读取显示保存与复制