首页 > 解决方案 > Raspberry PI 上的 USB 摄像头显示黑屏

问题描述

我正在做一个项目,我必须使用必须在 Raspberry PI(操作系统:Debian Raspberry PI Desktop)上访问的工业相机(型号 ID:Imaginsource DFK 33UX178)。相机以前工作正常。但是,一旦我连接到 Windows 机器并使用默认的 Windows 相机应用程序调整其设置,然后尝试使用 Raspberry PI OS 访问,它只会捕获黑色图像。

以下是我尝试过的命令。

  1. 命令:sudo fswebcam test.jpg 命令 1 的输出

  2. 命令:sudo fswebcam -S 1000 test.jpg 命令2的输出

此外,我尝试使用 OpenCV 脚本(如下),但它抛出错误。

import cv2


with cv2.VideoCapture(0) as camera:
    ret = camera.set(3,640)
    ret = camera.set(4,480)
    
    try:
        stream = io.BytesIO()
        while(True):
            t1 = cv2.getTickCount()
            
            ret, frame = camera.read()
            frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            frame_expanded = np.expand_dims(frame_rgb, axis=0)
            
            start_time = time.time()
            results = classify_image(interpreter, frame)
            elapsed_ms = (time.time() - start_time) * 1000
            label_id, prob = results[0]
            stream.seek(0)
            stream.truncate()
            
            # camera.annotate_text = '%s %.2f\n%.1fms' % (labels[label_id], prob,
            #                                         elapsed_ms)
            cv2.putText(frame, labels[label_id],(30,50),font,1,(255,255,0),2,cv2.LINE_AA)
            
            cv2.imshow('Object detector', frame)
            
            if cv2.waitKey(1) == ord('q'):
                break
            
    finally:
        camera.release()

错误:

Traceback (most recent call last):
  File "usb_api.py", line 4, in <module>
    with cv2.VideoCapture(0) as camera:
AttributeError: __enter__

虽然命令“lsusb”检测相机如下,它是“The Imaging Source Europe GmbH”

Bus 002 Device 004: ID 199e:9082 The Imaging Source Europe GmbH 
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 046d:c52b Logitech, Inc. Unifying Receiver
Bus 001 Device 002: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

我对相机的功能有疑问,所以我再次尝试使用 Windows 机器,它运行良好。

有人可以帮我解决这个问题吗?

谢谢

标签: pythonopencvraspberry-piwebcamv4l2

解决方案


推荐阅读