这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 板卡试用 » 08树莓派5CSI摄像头目标追踪

共2条 1/1 1 跳转至

08树莓派5CSI摄像头目标追踪

菜鸟
2025-06-28 16:12:25     打赏

树莓派5 CSI摄像头目标追踪

一,环境及硬件连接

CSI摄像头模块 OV5647

fpc金属这边朝向网卡方向

image-20250628143611608.png添加配置文件

sudo nano /boot/firmware/config.txt

如图

image-20250628143951409.png

重启树莓派

sudo reboot

安装三个库

sudo apt install libcamera-apps libcamera-dev python3-picamera2

软件包用途普通用户是否需要?




libcamera-apps命令行拍照/录像工具✅ 推荐安装(测试用)
python3-picamera2Python 摄像头控制库✅ 推荐安装(Python 开发)
libcamera-devC++ 开发库Qt中要用到

CSI摄像头测试

sudo libcamera-hello -t 0 --camera 0

image-20250628152650215.png安装opencv和opencontrib,因为目标追踪使用到的是csrt算法,他是在contrib库中

sudo apt install libopencv-dev python3-opencv
sudo apt install libopencv-contrib-dev libopencv-contrib406

二,代码部分


import cv2
import time
from picamera2 import Picamera2
from libcamera import Transform

class CameraSystem:
    def __init__(self, use_csi=True, camera_index=0):
        self.use_csi = use_csi
        self.camera_index = camera_index
        self.cap = None
        self.picam2 = None
        
    def initialize(self):
        """........"""
        if self.use_csi:
            try:
                # ...CSI...
                self.picam2 = Picamera2()
                config = self.picam2.create_preview_configuration(
                    main={"format": "RGB888", "size": (640, 480)},
                    raw={"format": "SRGGB12", "size": (1920, 1080)},
                    transform=Transform(hflip=0, vflip=1)
                )
                self.picam2.configure(config)
                self.picam2.set_controls({"FrameRate": 60})
                self.picam2.start(show_preview=False)
                print("CSI........")
                return True
            except Exception as e:
                print(f"CSI........: {e}")
                self.use_csi = False
                return self._initialize_usb()
        else:
            return self._initialize_usb()
    
    def _initialize_usb(self):
        """...USB..."""
        self.cap = cv2.VideoCapture(self.camera_index, cv2.CAP_V4L2)
        if not self.cap.isOpened():
            print(f"....USB... (.. {self.camera_index})")
            return False
        
        # ..USB.....
        self.cap.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc('M', 'J', 'P', 'G'))
        self.cap.set(cv2.CAP_PROP_FRAME_WIDTH, 640)
        self.cap.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
        self.cap.set(cv2.CAP_PROP_FPS, 30)
        
        # .....
        for _ in range(5):
            self.cap.read()
        time.sleep(0.5)
        print(f"USB... (.. {self.camera_index}) .....")
        return True
    
    def read(self):
        """......"""
        if self.use_csi and self.picam2:
            return True, self.picam2.capture_array("main")
        elif self.cap:
            return self.cap.read()
        return False, None
    
    def release(self):
        """......."""
        if self.picam2:
            self.picam2.stop()
        if self.cap:
            self.cap.release()
        cv2.destroyAllWindows()

def main():
    # ....CSI...........USB...
    camera_system = CameraSystem(use_csi=True, camera_index=0)
    if not camera_system.initialize():
        print(".........")
        return
    
    # ROI....
    ret, frame = camera_system.read()
    if not ret:
        print("..........")
        camera_system.release()
        return
    
    cv2.namedWindow("ROI Select", cv2.WINDOW_NORMAL)
    cv2.imshow("ROI Select", frame)
    print("..........Enter")
    bbox = cv2.selectROI("ROI Select", frame, showCrosshair=True)
    cv2.destroyAllWindows()
    
    if sum(bbox) == 0:
        print("........")
        camera_system.release()
        return
    
    # ......
    tracker = cv2.TrackerCSRT_create()
    tracker.init(frame, bbox)
    
    print(".... (.ESC..)...")
    while True:
        ret, frame = camera_system.read()
        if not ret:
            print("......")
            break
        
        # ....
        success, bbox = tracker.update(frame)
        if success:
            x, y, w, h = [int(i) for i in bbox]
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
        else:
            cv2.putText(frame, "LOST!", (20, 40), 
                       cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        
        # ....
        display = cv2.resize(frame, (640, 480))
        cv2.imshow("Tracking", display)
        
        if cv2.waitKey(30) == 27:  # ESC...
            break
    
    camera_system.release()

if __name__ == "__main__":
    main()

成果演示

python csrt_csi_usb.py

框选要追踪的物体,按下回车

image-20250628155443048.png

ezgif-2e1870a076d868.gif


菜鸟
2025-06-28 21:01:13     打赏
2楼

图片过期了,看这个

ezgif-2e1870a076d868.gif


共2条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]