OpenMV Cam 运行几秒后摄像头卡死,IDE 左下角停止键无法响应,只能拔插 USB 重启。项目涉及二维码、物料、色环三类识别任务,此前代码可稳定运行,昨日起突发此问题。
import sensor
import image
import time
from machine import UART
# 摄像头初始化
sensor.reset()
sensor.set_pixformat(sensor.RGB565)
sensor.set_framesize(sensor.QVGA)
sensor.set_windowing((320,240))
sensor.skip_frames(time=3000)
# 固定参数
sensor.set_auto_gain(False, gain_db=18)
sensor.set_auto_whitebal(False)
sensor.set_auto_exposure(False, exposure_us=40000)
clock = time.clock()
uart = UART(1,115200)
# LAB颜色阈值
red_threshold = (10, 90, 15, 127, -10, 127)
green_threshold = (10, 90, -128, -15, -20, 80)
blue_threshold = (0, 100, -20, 50, -128, -10)
color_dict = {
1: ("RED", red_threshold),
2: ("BLUE", blue_threshold),
3: ("GREEN", green_threshold)
}
# 全局变量
qr_text = "NONE"
task_list = []
task_index = 0
task_state = 0
detecting = False
detect_start_time = 0
print("SYSTEM START")
# 寻找物料
def find_material(img, threshold):
blobs = img.find_blobs(
[threshold],
pixels_threshold=200,
area_threshold=200,
merge=True,
margin=15
)
best_blob = None
best_score = 0
for blob in blobs:
# 圆度过滤
if blob.density() < 0.35:
continue
# 长宽比过滤
ratio = blob.w() / blob.h()
if ratio < 0.65 or ratio > 1.35:
continue
area = blob.area()
score = area * blob.density()
if score > best_score:
best_score = score
best_blob = blob
return best_blob
# 寻找色环(新版)
def find_best_ring(img, threshold):
blobs = img.find_blobs(
[threshold],
pixels_threshold=120,
area_threshold=120,
merge=True,
margin=8
)
if not blobs:
return None
best_blob = None
best_score = 0
for blob in blobs:
# 圆度过滤
if blob.density() < 0.18:
continue
# 长宽比过滤
ratio = blob.w() / blob.h()
if ratio < 0.7 or ratio > 1.3:
continue
# 面积过滤
if blob.area() < 800:
continue
x = blob.x()
y = blob.y()
w = blob.w()
h = blob.h()
# 中心区域检测
cx = blob.cx()
cy = blob.cy()
center_size = int(min(w,h) *0.25)
center_roi = (
cx - center_size//2,
cy - center_size//2,
center_size,
center_size
)
center_blobs = img.find_blobs(
[threshold],
roi=center_roi,
pixels_threshold=10,
area_threshold=10
)
center_pixels = 0
for b in center_blobs:
center_pixels += b.pixels()
# 中心必须为空
if center_pixels > center_size * center_size * 0.15:
continue
score = blob.area()
if score > best_score:
best_score = score
best_blob = blob
return best_blob
# 主循环
while(True):
clock.tick()
img = sensor.snapshot()
# 镜头畸变矫正
img = img.lens_corr(1.5)
# 二维码识别
qrcodes = img.find_qrcodes()
for code in qrcodes:
qr_text = code.payload()
task_list = []
for c in qr_text:
if c == '1':
task_list.append(1)
elif c == '2':
task_list.append(2)
elif c == '3':
task_list.append(3)
task_index = 0
task_state = 0
detecting = False
uart.write("QR:{}\r\n".format(qr_text))
print("QR:", qr_text)
img.draw_rectangle(code.rect(), color=(255,0,0))
# UI显示
img.draw_string(
10,
10,
"QR:" + qr_text,
color=(255,0,0),
scale=2
)
# 当前任务
if task_index < len(task_list):
current_color_id = task_list[task_index]
color_name, threshold = color_dict[current_color_id]
img.draw_string(
10,
40,
"TARGET:" + color_name,
color=(0,255,0),
scale=2
)
# 状态0:物料识别
if task_state == 0:
img.draw_string(
10,
70,
"STATE: MATERIAL",
color=(255,255,0),
scale=2
)
blob = find_material(img, threshold)
if blob:
cx = blob.cx()
cy = blob.cy()
radius = int((blob.w()+blob.h())/4)
img.draw_circle(
cx,
cy,
radius,
color=(255,255,255),
thickness=3
)
img.draw_cross(
cx,
cy,
color=(255,0,0),
size=18
)
img.draw_string(
cx-30,
cy+12,
"{},{}".format(cx,cy),
color=(255,255,255),
scale=1.5
)
uart.write(
"MAT:{} X:{} Y:{}\r\n".format(
current_color_id,
cx,
cy
)
)
now = time.ticks_ms()
if not detecting:
detecting = True
detect_start_time = now
else:
duration = time.ticks_diff(
now,
detect_start_time
)
img.draw_string(
10,
110,
"TIME:{}s".format(
int(duration/1000)
),
color=(255,255,255),
scale=2
)
if duration >= 3000:
detecting = False
task_state = 1
time.sleep_ms(300)
else:
detecting = False
# 状态1:色环识别
elif task_state == 1:
img.draw_string(
10,
70,
"STATE: RING",
color=(0,255,255),
scale=2
)
best_blob = find_best_ring(
img,
threshold
)
if best_blob:
x = best_blob.cx()
y = best_blob.cy()
r = int((best_blob.w() + best_blob.h()) / 4)
img.draw_circle(
x,
y,
r,
color=(0,255,255),
thickness=3
)
img.draw_cross(
x,
y,
color=(0,255,0),
size=18
)
img.draw_string(
x-30,
y+12,
"{},{}".format(x,y),
color=(0,255,255),
scale=1.5
)
uart.write(
"RING:{} X:{} Y:{}\r\n".format(
current_color_id,
x,
y
)
)
now = time.ticks_ms()
if not detecting:
detecting = True
detect_start_time = now
else:
duration = time.ticks_diff(
now,
detect_start_time
)
img.draw_string(
10,
110,
"TIME:{}s".format(
int(duration/1000)
),
color=(255,255,255),
scale=2
)
if duration >= 3000:
detecting = False
task_index += 1
task_state = 0
time.sleep_ms(300)
else:
detecting = False
# 全部完成
else:
img.draw_string(
50,
100,
"ALL DONE",
color=(0,255,0),
scale=3
)
# FPS显示
img.draw_string(
10,
220,
"FPS:{:.1f}".format(clock.fps()),
color=(255,255,255),
scale=1.5
)

找颜色的程序,5.0.0的固件会报错,改回4.8.1