【发布时间】: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.name是bytes或bytearray对象,它们在startswith方法中也需要字节对象。我不认为这是有意的,如果尚未完成,应该向 picamera 的开发人员报告。您可以尝试自己将'vc.ril.video_splitter'转换为bytes对象(在文字前添加b),但是您必须手动编辑encoders源代码 -
作为解决方法,您可以将格式更改为 yuv(一种原始格式)或 jpeg、png、gif(它们使用不同的编码器)。
标签: python python-3.x opencv image-processing raspberry-pi