mirror of
https://github.com/MoreTore/openpilot.git
synced 2026-10-01 03:43:51 +08:00
openpilot v0.9.7 release
date: 2024-06-11T01:36:39 master commit: f8cb04e4a8b032b72a909f68b808a50936184bee
This commit is contained in:
@@ -0,0 +1,37 @@
|
||||
# Run openpilot with webcam on PC
|
||||
|
||||
What's needed:
|
||||
- Ubuntu 20.04
|
||||
- GPU (recommended)
|
||||
- Two USB webcams, at least 720p and 78 degrees FOV (e.g. Logitech C920/C615)
|
||||
- [Car harness](https://comma.ai/shop/products/comma-car-harness) with black panda to connect to your car
|
||||
- [Panda paw](https://comma.ai/shop/products/panda-paw) or USB-A to USB-A cable to connect panda to your computer
|
||||
That's it!
|
||||
|
||||
## Setup openpilot
|
||||
```
|
||||
cd ~
|
||||
git clone https://github.com/commaai/openpilot.git
|
||||
```
|
||||
- Follow [this readme](https://github.com/commaai/openpilot/tree/master/tools) to install the requirements
|
||||
- Install [OpenCL Driver](https://registrationcenter-download.intel.com/akdlm/irc_nas/vcp/15532/l_opencl_p_18.1.0.015.tgz)
|
||||
|
||||
## Build openpilot for webcam
|
||||
```
|
||||
cd ~/openpilot
|
||||
USE_WEBCAM=1 scons -j$(nproc)
|
||||
```
|
||||
|
||||
## Connect the hardware
|
||||
- Connect the road facing camera first, then the driver facing camera
|
||||
- (default indexes are 1 and 2; can be modified in system/camerad/cameras/camera_webcam.cc)
|
||||
- Connect your computer to panda
|
||||
|
||||
## GO
|
||||
```
|
||||
cd ~/openpilot/system/manager
|
||||
NOSENSOR=1 USE_WEBCAM=1 ./manager.py
|
||||
```
|
||||
- Start the car, then the UI should show the road webcam's view
|
||||
- Adjust and secure the webcams (you can run tools/webcam/front_mount_helper.py to help mount the driver camera)
|
||||
- Finish calibration and engage!
|
||||
@@ -0,0 +1,33 @@
|
||||
import cv2 as cv
|
||||
import numpy as np
|
||||
|
||||
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
|
||||
|
||||
self.cap = cv.VideoCapture(camera_id)
|
||||
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):
|
||||
yuv = cv.cvtColor(bgr, cv.COLOR_BGR2YUV_I420)
|
||||
uv_row_cnt = yuv.shape[0] // 3
|
||||
uv_plane = np.transpose(yuv[uv_row_cnt * 2:].reshape(2, -1), [1, 0])
|
||||
yuv[uv_row_cnt * 2:] = uv_plane.reshape(uv_row_cnt, -1)
|
||||
return yuv
|
||||
|
||||
def read_frames(self):
|
||||
while True:
|
||||
sts , frame = self.cap.read()
|
||||
if not sts:
|
||||
break
|
||||
yuv = Camera.bgr2nv12(frame)
|
||||
yield yuv.data.tobytes()
|
||||
self.cap.release()
|
||||
@@ -0,0 +1,73 @@
|
||||
#!/usr/bin/env python3
|
||||
import threading
|
||||
import os
|
||||
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
|
||||
|
||||
DUAL_CAM = os.getenv("DUAL_CAMERA")
|
||||
CameraType = namedtuple("CameraType", ["msg_name", "stream_type", "cam_id"])
|
||||
CAMERAS = [
|
||||
CameraType("roadCameraState", VisionStreamType.VISION_STREAM_ROAD, os.getenv("CAMERA_ROAD_ID", "0")),
|
||||
CameraType("driverCameraState", VisionStreamType.VISION_STREAM_DRIVER, os.getenv("CAMERA_DRIVER_ID", "1")),
|
||||
]
|
||||
if DUAL_CAM:
|
||||
CAMERAS.append(CameraType("wideRoadCameraState", VisionStreamType.VISION_STREAM_WIDE_ROAD, DUAL_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 = Camera(c.msg_name, c.stream_type, c.cam_id)
|
||||
assert cam.cap.isOpened(), f"Can't find camera {c}"
|
||||
self.cameras.append(cam)
|
||||
self.vipc_server.create_buffers(c.stream_type, 20, False, 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)
|
||||
while cam.cap.isOpened():
|
||||
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()
|
||||
@@ -0,0 +1,14 @@
|
||||
#!/bin/bash
|
||||
|
||||
# export the block below when call manager.py
|
||||
export BLOCK="${BLOCK},camerad"
|
||||
export USE_WEBCAM="1"
|
||||
|
||||
# Change camera index according to your setting
|
||||
export CAMERA_ROAD_ID="0"
|
||||
export CAMERA_DRIVER_ID="1"
|
||||
export DUAL_CAMERA="2" # camera index for wide road camera
|
||||
|
||||
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd )"
|
||||
|
||||
$DIR/camerad.py
|
||||
Reference in New Issue
Block a user