运行官方风格的颜色+圆形同时识别代码时,空场景可长期稳定运行;一旦画面中出现目标色块(红色圆),数秒内即发生 IDE 断连 —— 串口终端持续刷屏乱码(含重复 Traceback 与 statistics 输出),FPS 仍可打印;设备未从 Windows 设备管理器消失,但 IDE 无法通信,必须拔插 USB 才能恢复。实测板子明显发热,疑似过热保护或供电异常触发。



提供一下运行的代码
import csi, image, time
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QQVGA)
csi0.snapshot(time = 2000)
csi0.auto_gain(False) # must be turned off for color tracking
csi0.auto_whitebal(False) # must be turned off for color tracking
clock = time.clock()
while(True):
clock.tick()
img = csi0.snapshot().lens_corr(1.8)
for c in img.find_circles(threshold = 3500, x_margin = 10, y_margin = 10, r_margin = 10,
r_min = 2, r_max = 100, r_step = 2):
area = (c.x-c.r, c.y-c.r, 2*c.r, 2*c.r)
#area为识别到的圆的区域,即圆的外接矩形框
statistics = img.get_statistics(roi=area)#像素颜色统计
print(statistics)
#(0,100,0,120,0,120)是红色的阈值,所以当区域内的众数(也就是最多的颜色),范围在这个阈值内,就说明是红色的圆。
#l_mode,a_mode,b_mode是L通道,A通道,B通道的众数。
if 0<statistics.l_mode<100 and 0<statistics.a_mode<127 and 0<statistics.b_mode<127:#if the circle is red
img.draw_circle((c.x, c.y, c.r), color = (255, 0, 0))#识别到的红色圆形用红色的圆框出来
else:
img.draw_rectangle(area, color = (255, 255, 255))
#将非红色的圆用白色的矩形框出来
print("FPS %f" % clock.fps())就是这个,是官网找的直接复制过去了
import csi
import time
import gc
csi0 = csi.CSI()
csi0.reset()
csi0.pixformat(csi.RGB565)
csi0.framesize(csi.QQVGA)
csi0.snapshot(time=2000)
csi0.auto_gain(False)
csi0.auto_whitebal(False)
clock = time.clock()
last_print = time.ticks_ms()
# 调试时每帧最多检查 20 个候选圆,超过的暂时忽略
MAX_CHECK = 20
while True:
clock.tick()
img = csi0.snapshot()
# 先关闭畸变校正以排查性能;确实需要时再开启
# img.lens_corr(1.8)
t0 = time.ticks_ms()
circles = img.find_circles(
threshold=2000,
x_stride=2,
y_stride=2,
x_margin=10,
y_margin=10,
r_margin=10,
r_min=8,
r_max=40,
r_step=4
)
t1 = time.ticks_ms()
red_circles = []
checked = 0
for c in circles:
if checked >= MAX_CHECK:
break
checked += 1
# 保留原来的外接矩形取样方式,并做边界保护
x0 = max(0, c.x - c.r)
y0 = max(0, c.y - c.r)
x1 = min(img.width(), c.x + c.r)
y1 = min(img.height(), c.y + c.r)
if x1 <= x0 or y1 <= y0:
continue
s = img.get_statistics(
roi=(x0, y0, x1 - x0, y1 - y0)
)
# 暂时沿用你的颜色条件
if (0 < s.l_mode < 100
and 0 < s.a_mode < 127
and 0 < s.b_mode < 127):
red_circles.append((c.x, c.y, c.r))
t2 = time.ticks_ms()
# 先完成所有颜色统计,再画图,避免红色框影响后续统计
for circle in red_circles:
img.draw_circle(circle, color=(255, 0, 0))
now = time.ticks_ms()
if time.ticks_diff(now, last_print) >= 1000:
print("circles=%d checked=%d detect=%dms stats=%dms "
"FPS=%.1f heap=%d" %
(len(circles), checked,
time.ticks_diff(t1, t0),
time.ticks_diff(t2, t1),
clock.fps(), gc.mem_free()))
last_print = now
del circles, red_circles
谢谢!!现在识别很准确,也不断连了,温度也不怎么高了