用户在使用OpenMV4 H7时遇到了代码报错问题,需要帮助进行故障排查。
故障代码涉及小车控制、循迹检测和红绿灯检测功能:
import sensor
import time
from car import CarController
from track_detector import TrackDetector
from traffic_light import TrafficLightDetector
# 初始化摄像头
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.skip_frames(time=2000)
sensor.set_auto_gain(False)
sensor.set_auto_whitebal(False)
# 初始化控制器
car = CarController()
track_detector = TrackDetector()
traffic_light = TrafficLightDetector()
# 主循环
while True:
img = sensor.snapshot()
# 检测红绿灯
light_state = traffic_light.detect_traffic_light(img)
# 根据红绿灯状态控制小车
if light_state == 1: # 红灯
car.stop()
else: # 绿灯或无灯,进行循迹
line_position = track_detector.find_track_center(img)
car.send_control_packet(line_position)
time.sleep(50) # 毫秒

去掉报错那条代码后重试