0

I have just installed OpenCV 3 (using this tutorial) on my Raspberry Pi 2 model b and I found this code that should work with my version of OpenCV and 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)  

But I am getting this error

TypeError: startswith first arg must be bytes or a tuple of bytes, not str

on the camera.capture() line.

I have searched for answers with the same error, but I can not find a solution or someone who is in the same situation. I have already made sure that the piCamera is working and I have installed OpenCV correctly. What gives?

Here is the full error message:

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
  • Could you please provide the whole error message including stacktrace? As far as I can see you are not using a function called `startswith` in your code –  Jul 01 '16 at 19:46
  • I have added the error message @LPK – Marc Erhardtsen Jul 01 '16 at 19:55
  • That looks very interesting... And it doesn't seems to be your fault. I'm gonna check that –  Jul 01 '16 at 20:06
  • Okay, the problem here seems to be `input_port.name` being a `bytes` or `bytearray` object which also require byte objects in their `startswith`-method. I don't think that this is intended and should be reported to the developers of picamera if not already done. You could try to transform `'vc.ril.video_splitter'` to a `bytes` object by yourself (add a `b` in front of the literals) but then you would have to edit the `encoders` source code manually –  Jul 01 '16 at 20:37
  • As workaround you can change format to yuv (one of raw formats) or to jpeg, png, gif (they use different encoder). – Arnial Jul 01 '16 at 21:05
  • Thank you for the explanation @LPK It just seems like a weird error since i can't find anyone else with the same one. Using OpenCV with the PiCamera seems to work fine for most people. – Marc Erhardtsen Jul 02 '16 at 04:52
  • Update picamera to version 1.12 to achieve the same solution https://github.com/waveform80/picamera/issues/297 – The-Duck Jul 28 '16 at 15:51

1 Answers1

0

I was having the same issue. Based on this answer, it appears to be an error in picamera/encoders.py. After editing the end of line 534 of picamera/encoders.py to: ... .startswith(b'vc.ril.video_splitter') I was able to get an image out. The b tells startswith to treat the string as a byte literal.

Community
  • 1
  • 1