Trouble running the Python example code with WPILib FRCVision Raspberry Pi image

I have been trying to get the python example code to run with the new WPILib FRCVision image. I am using a Raspberry Pi 3b that is connected to a Raspberry Pi Camera V2. After installing the image, I uploaded the python example code through the web viewer. When I opened up the console, I got the error below. Has anyone else run into similar problems or know how to fix it?

Starting camera 'rPi Camera 0' on /dev/video0

CS: rPi Camera 0: Connecting to USB camera on /dev/video0

CS: rPi Camera 0: SetConfigJson: setting video mode to pixelFormat 1, width 160, height 120, fps 30

Unhandled exception in _onVideoEvent
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/cscore/cameraserver.py", line 254, in _ve
    self._onVideoEvent(e)
  File "/usr/local/lib/python3.5/dist-packages/cscore/cameraserver.py", line 339, in _onVideoEvent
    choices = prop.getChoices()  # type: List[str]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xf0 in position 1: invalid continuation byte

Unhandled exception in _onVideoEvent
Traceback (most recent call last):
  File "/usr/local/lib/python3.5/dist-packages/cscore/cameraserver.py", line 254, in _ve
    self._onVideoEvent(e)
  File "/usr/local/lib/python3.5/dist-packages/cscore/cameraserver.py", line 339, in _onVideoEvent
    choices = prop.getChoices()  # type: List[str]
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 0: invalid start byte

This looks like a bug in robotpy-cscore. It appears this particular camera has non-utf8 property names and the python code doesn’t handle that case. You will likely need to edit cameraserver.py to catch the exception as a quick workaround.

EDIT: it’s harmless, see following two posts.

We do catch all exceptions when handling video events in CameraServer, they’re just not silent. Any code using CameraServer there should still work.

1 Like

Yeah, what David said was correct. The only thing that will break is access of that particular property.

1 Like

I got the exact same error last week (with a pi camera) and solved it by using an OpenCV Camera object instead of the CSCore UsbCamera object. Here is pretty bare-bones python file for capturing images and uploading to a stream from a pi camera:

*#!/usr/bin/env python3*

*#----------------------------------------------------------------------------*

*# Copyright (c) 2018 FIRST. All Rights Reserved.*

*# Open Source Software - may be modified and shared by FRC teams. The code*

*# must be accompanied by the FIRST BSD license file in the root directory of*

*# the project.*

*#----------------------------------------------------------------------------*

**import** json

**import** time

**import** sys

**import** cv2

**import** numpy **as** np

**from** cscore **import** CameraServer, VideoSource

**from** networktables **import** NetworkTablesInstance

cap = cv2.VideoCapture(0)

cap.set(10, 0.1)

cs = CameraServer.getInstance()

*# Setup a CvSource. This will send images back to the Dashboard*

outputStream = cs.putVideo("Cam1", 640, 480)

*# start NetworkTables*

ntinst = NetworkTablesInstance.getDefault()

ntinst.startClientTeam(4338)

*# Capture frame-by-frame*

ret, frame = cap.read()

*# Run forever*

**while** (True):

*# Capture frame-by-frame*

ret, frame = cap.read()

*# grayFrame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)*

*# (optional) send some image back to the dashboard*

outputStream.putFrame(frame)

Edit: You can also ignore the error and that should work (as suggested in the other posts).

I highly recommend you use UsbCamera instead of the OpenCV camera source. The warning/exception is harmless as stated above. The UsbCamera is significantly more robust than the OpenCV camera source (try unplugging the camera and plugging it back in with both), and also provides some nice features like real-time tweaking of properties via the CameraServer webpage.

1 Like

Ahh, I just checked again and it appears you were correct: just because I get the error doesn’t mean the feed doesn’t work anymore… Out of curiosity, do you know where the error originates from?

Yes I agree, given that the error is harmless it’s better to use UsbCamera.

The error occurs because the string returned from the camera is not valid unicode, but instead is some arbitrary undefined encoding. robotpy-cscore tries to convert the data to unicode, and thus it fails.

To make these errors go away, it would be really useful to know what the actual bytes are. See https://github.com/robotpy/robotpy-cscore/issues/58 to track this issue.