openpilot v0.11.1 release
date: 2026-06-04T09:49:56 master commit: c0ab3550eca2e9daf197c46b7e4b24aa9637cf2e
This commit is contained in:
@@ -0,0 +1,31 @@
|
||||
# Run openpilot with webcam on PC
|
||||
|
||||
What's needed:
|
||||
- Ubuntu 24.04 ([WSL2 is not supported](https://github.com/commaai/openpilot/issues/34216)) or macOS
|
||||
- GPU (recommended)
|
||||
- One USB webcam, at least 720p and 78 degrees FOV (e.g. Logitech C920/C615, NexiGo N60)
|
||||
- [Car harness](https://comma.ai/shop/products/comma-car-harness)
|
||||
- [panda](https://comma.ai/shop/panda)
|
||||
- USB-A to USB-A cable to connect panda to your computer
|
||||
|
||||
## Setup openpilot
|
||||
- Follow [this readme](../README.md) to install and build the requirements
|
||||
|
||||
## Connect the hardware
|
||||
- Connect the camera first
|
||||
- Connect your computer to panda
|
||||
|
||||
## GO
|
||||
```
|
||||
USE_WEBCAM=1 system/manager/manager.py
|
||||
```
|
||||
- Start the car, then the UI should show the road webcam's view
|
||||
- Adjust and secure the webcam
|
||||
- Finish calibration and engage!
|
||||
|
||||
## Specify Cameras
|
||||
|
||||
Use the `ROAD_CAM` (default 0) and optional `DRIVER_CAM`, `WIDE_CAM` environment variables to specify which camera is which (ie. `ROAD_CAM=1` uses `/dev/video1`, on Ubuntu, for the road camera):
|
||||
```
|
||||
USE_WEBCAM=1 ROAD_CAM=1 system/manager/manager.py
|
||||
```
|
||||
@@ -0,0 +1,39 @@
|
||||
import av
|
||||
import cv2 as cv
|
||||
|
||||
class Camera:
|
||||
def __init__(self, cam_type_state, stream_type, camera_id):
|
||||
try:
|
||||
camera_id = int(camera_id)
|
||||
except ValueError: # allow strings, ex: /dev/video0
|
||||
pass
|
||||
self.cam_type_state = cam_type_state
|
||||
self.stream_type = stream_type
|
||||
self.cur_frame_id = 0
|
||||
|
||||
print(f"Opening {cam_type_state} at {camera_id}")
|
||||
|
||||
self.cap = cv.VideoCapture(camera_id)
|
||||
|
||||
self.cap.set(cv.CAP_PROP_FRAME_WIDTH, 1280.0)
|
||||
self.cap.set(cv.CAP_PROP_FRAME_HEIGHT, 720.0)
|
||||
self.cap.set(cv.CAP_PROP_FPS, 25.0)
|
||||
|
||||
self.W = self.cap.get(cv.CAP_PROP_FRAME_WIDTH)
|
||||
self.H = self.cap.get(cv.CAP_PROP_FRAME_HEIGHT)
|
||||
|
||||
@classmethod
|
||||
def bgr2nv12(self, bgr):
|
||||
frame = av.VideoFrame.from_ndarray(bgr, format='bgr24')
|
||||
return frame.reformat(format='nv12').to_ndarray()
|
||||
|
||||
def read_frames(self):
|
||||
while True:
|
||||
ret, frame = self.cap.read()
|
||||
if not ret:
|
||||
break
|
||||
# Rotate the frame 180 degrees (flip both axes)
|
||||
frame = cv.flip(frame, -1)
|
||||
yuv = Camera.bgr2nv12(frame)
|
||||
yield yuv.data.tobytes()
|
||||
self.cap.release()
|
||||
Executable
+78
@@ -0,0 +1,78 @@
|
||||
#!/usr/bin/env python3
|
||||
import threading
|
||||
import os
|
||||
import platform
|
||||
from collections import namedtuple
|
||||
|
||||
from msgq.visionipc import VisionIpcServer, VisionStreamType
|
||||
from cereal import messaging
|
||||
|
||||
from openpilot.tools.webcam.camera import Camera
|
||||
from openpilot.common.realtime import Ratekeeper
|
||||
|
||||
ROAD_CAM = os.getenv("ROAD_CAM", "0")
|
||||
WIDE_CAM = os.getenv("WIDE_CAM")
|
||||
DRIVER_CAM = os.getenv("DRIVER_CAM")
|
||||
|
||||
CameraType = namedtuple("CameraType", ["msg_name", "stream_type", "cam_id"])
|
||||
|
||||
CAMERAS = [
|
||||
CameraType("roadCameraState", VisionStreamType.VISION_STREAM_ROAD, ROAD_CAM)
|
||||
]
|
||||
if WIDE_CAM:
|
||||
CAMERAS.append(CameraType("wideRoadCameraState", VisionStreamType.VISION_STREAM_WIDE_ROAD, WIDE_CAM))
|
||||
if DRIVER_CAM:
|
||||
CAMERAS.append(CameraType("driverCameraState", VisionStreamType.VISION_STREAM_DRIVER, DRIVER_CAM))
|
||||
|
||||
class Camerad:
|
||||
def __init__(self):
|
||||
self.pm = messaging.PubMaster([c.msg_name for c in CAMERAS])
|
||||
self.vipc_server = VisionIpcServer("camerad")
|
||||
|
||||
self.cameras = []
|
||||
for c in CAMERAS:
|
||||
cam_device = f"/dev/video{c.cam_id}" if platform.system() != "Darwin" else c.cam_id
|
||||
cam = Camera(c.msg_name, c.stream_type, cam_device)
|
||||
self.cameras.append(cam)
|
||||
self.vipc_server.create_buffers(c.stream_type, 20, cam.W, cam.H)
|
||||
|
||||
self.vipc_server.start_listener()
|
||||
|
||||
def _send_yuv(self, yuv, frame_id, pub_type, yuv_type):
|
||||
eof = int(frame_id * 0.05 * 1e9)
|
||||
self.vipc_server.send(yuv_type, yuv, frame_id, eof, eof)
|
||||
dat = messaging.new_message(pub_type, valid=True)
|
||||
msg = {
|
||||
"frameId": frame_id,
|
||||
"transform": [1.0, 0.0, 0.0,
|
||||
0.0, 1.0, 0.0,
|
||||
0.0, 0.0, 1.0]
|
||||
}
|
||||
setattr(dat, pub_type, msg)
|
||||
self.pm.send(pub_type, dat)
|
||||
|
||||
def camera_runner(self, cam):
|
||||
rk = Ratekeeper(20, None)
|
||||
for yuv in cam.read_frames():
|
||||
self._send_yuv(yuv, cam.cur_frame_id, cam.cam_type_state, cam.stream_type)
|
||||
cam.cur_frame_id += 1
|
||||
rk.keep_time()
|
||||
|
||||
def run(self):
|
||||
threads = []
|
||||
for cam in self.cameras:
|
||||
cam_thread = threading.Thread(target=self.camera_runner, args=(cam,))
|
||||
cam_thread.start()
|
||||
threads.append(cam_thread)
|
||||
|
||||
for t in threads:
|
||||
t.join()
|
||||
|
||||
|
||||
def main():
|
||||
camerad = Camerad()
|
||||
camerad.run()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in New Issue
Block a user