【问题标题】:Python starts with error in OpenCVPython 在 OpenCV 中出现错误
【发布时间】:2016-07-01 19:40:43
【问题描述】:

我刚刚在我的 Raspberry Pi 2 模型 b 上安装了 OpenCV 3(使用 this 教程),我发现这段代码应该适用于我的 OpenCV 和 python(3) 版本:

from picamera.array import PiRGBArray
from picamera import PiCamera
import time
import cv2

# initialize the camera and grab a reference to the raw camera capture
camera = PiCamera()
rawCapture = PiRGBArray(camera)

# allow the camera to warmup
time.sleep(0.1)

# grab an image from the camera
camera.capture(rawCapture, format="bgr")
image = rawCapture.array

# display the image on screen and wait for a keypress
cv2.imshow("Image", image)
cv2.waitKey(0)  

但是我收到了这个错误

TypeError: startswith first arg must be bytes or a tuple of bytes, not 字符串

camera.capture() 线上。

我已经搜索过相同错误的答案,但我找不到解决方案或遇到相同情况的人。我已经确定piCamera 正在工作并且我已经正确安装了OpenCV。什么给了?

这是完整的错误信息:

Traceback (most recent call last):
  File "test_image.py", line 14, in <module>
    camera.capture(rawCapture, format="bgr")
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/camera.py", line 1371, in capture
    camera_port, output_port, format, resize, **options)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/camera.py", line 652, in _get_image_encoder
    self, camera_port, output_port, format, resize, **options)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/encoders.py", line 1049, in __init__
parent, camera_port, input_port, format, resize, **options)
  File "/home/pi/.virtualenvs/cv/lib/python3.4/site-packages/picamera/encoders.py", line 534, in __init__
    if not resize and format != 'yuv' and input_port.name.startswith('vc.ril.video_splitter'):
TypeError: startswith first arg must be bytes or a tuple of bytes, not str

【问题讨论】:

  • 能否请您提供包括堆栈跟踪在内的整个错误消息?据我所知,您没有在代码中使用名为 startswith 的函数
  • 我已经添加了错误信息@LPK
  • 这看起来很有趣......而且这似乎不是你的错。我去看看
  • 好的,这里的问题似乎是input_port.namebytesbytearray 对象,它们在startswith 方法中也需要字节对象。我不认为这是有意的,如果尚未完成,应该向 picamera 的开发人员报告。您可以尝试自己将'vc.ril.video_splitter' 转换为bytes 对象(在文字前添加b),但是您必须手动编辑encoders 源代码
  • 作为解决方法,您可以将格式更改为 yuv(一种原始格式)或 jpeg、png、gif(它们使用不同的编码器)。

标签: python python-3.x opencv image-processing raspberry-pi


【解决方案1】:

我遇到了同样的问题。基于this answer,它似乎是 picamera/encoders.py 中的错误。将 picamera/encoders.py 的第 534 行末尾编辑为: ... .startswith(b'vc.ril.video_splitter') 我能够得到一张图像。 b 告诉startswith 将字符串视为字节文字。

【讨论】:

    猜你喜欢
    • 2020-12-23
    • 1970-01-01
    • 2018-11-29
    • 2017-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-04
    • 2021-07-12
    相关资源
    最近更新 更多