logo

OpenMV N6 烧录 5.0.0 固件后运行代码卡死

2026-06-06 16:43
0
0k7a7nluy4v9

OpenMV N6 烧录 5.0.0 固件后运行代码卡死

故障描述

OpenMV N6 烧录到 5.0.0 固件后,运行普通代码会卡死,只能关闭 IDE 和拔出 USB;即使手动停止能正常运行的代码也会卡死。

故障类型

硬件故障

硬件信息

  • 产品型号:OpenMV N6(其他)
  • 固件版本:5.0.0

故障现象

运行代码时设备无响应,必须强制断电或关闭 IDE 才能恢复。

相关代码

import sensor
import image
import time

# -----------------------------

# Camera Init

# -----------------------------

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)

clock = time.clock()

# -----------------------------

# LAB Color Thresholds

# 保持你当前版本

# -----------------------------

color_thresholds = {
"红": [(20, 80, 10, 80, -60, 60)],
"绿": [(20, 50, -30, 9, -10, 40)],
"蓝": [(3, 25, -63, 71, -78, -7)],
"黄": [(60, 70, -11, 11, 15, 60)],
}
DETECT_ROI = (70, 40, 150, 160)

# -----------------------------

# Blob Filter Parameters

# -----------------------------

MIN_AREA = 200
MIN_PIXELS = 180
EDGE_MARGIN = 4
MIN_DENSITY = 0.35

# -----------------------------

# Multi-frame Stability

# -----------------------------

HISTORY_LEN = 5
STABLE_NEED = 1
history = []

# -----------------------------

# Result Stability

# 新增:最终结果连续稳定若干帧后再输出

# -----------------------------

RESULT_STABLE_NEED = 3
last_result = ""
result_stable_count = 0
last_output = ""

# -----------------------------

# Blob Filter

# -----------------------------

def valid_blob(blob, img):
    x = blob.x()
    y = blob.y()
    w = blob.w()
    h = blob.h()

    area = blob.area()
    pixels = blob.pixels()
    density = blob.density()

    if area < MIN_AREA:
        return False

    if pixels < MIN_PIXELS:
        return False

    aspect_ratio = w / h if h != 0 else 999
    if aspect_ratio > 2.5 or aspect_ratio < 0.40:
        return False

    if x < EDGE_MARGIN or y < EDGE_MARGIN:
        return False
    if x + w > img.width() - EDGE_MARGIN:
        return False
    if y + h > img.height() - EDGE_MARGIN:
        return False

    if density < MIN_DENSITY:
        return False

    return True

# -----------------------------

# ROI Helpers

# -----------------------------

def blob_roi(blob):
    return (blob.x(), blob.y(), blob.w(), blob.h())

# -----------------------------

# Circle Detection

# -----------------------------

def is_circle(img, blob):
    roi = blob_roi(blob)

    r_min = max(5, min(blob.w(), blob.h()) // 4)
    r_max = max(r_min + 2, min(blob.w(), blob.h()) // 2)

    circles = img.find_circles(
        roi=roi,
        threshold=1200,
        x_margin=12,
        y_margin=12,
        r_margin=12,
        r_min=r_min,
        r_max=r_max,
        r_step=2
    )

    if circles:
        return True
    return False

# -----------------------------

# Shape Classification

# 保持你当前版本

# -----------------------------

def classify_shape(img, blob):
    aspect_ratio = blob.w() / blob.h() if blob.h() != 0 else 0
    density = blob.density()
    circle_ok = is_circle(img, blob)

    print("aspect_ratio=%.2f density=%.2f circle=%s" % (aspect_ratio, density, circle_ok))

    if circle_ok:
        if 0.85 <= aspect_ratio <= 1.15 and 0.60 <= density <= 0.88:
            return "圆"

    if density < 0.58:
        return "角"

    return "方"

# -----------------------------

# Color Recheck by Center Statistics

# -----------------------------

def color_id_from_blob(img, blob):
    cx = blob.cx()
    cy = blob.cy()

    roi_x = max(cx - 4, 0)
    roi_y = max(cy - 4, 0)
    roi_w = 9
    roi_h = 9

    stats = img.get_statistics(roi=(roi_x, roi_y, roi_w, roi_h))

    best_name = "未知"
    best_score = -1

    for color_name, thresholds in color_thresholds.items():
        for th in thresholds:
            score = 0
            if th[0] <= stats.l_mean() <= th[1]:
                score += 1
            if th[2] <= stats.a_mean() <= th[3]:
                score += 1
            if th[4] <= stats.b_mean() <= th[5]:
                score += 1

            if score > best_score:
                best_score = score
                best_name = color_name

    return best_name

# -----------------------------

# Detect Current Frame

# -----------------------------

def current_frame_results(img):
    results = []

    for _, thresholds in color_thresholds.items():
        blobs = img.find_blobs(
            thresholds,
            roi=DETECT_ROI,
            pixels_threshold=MIN_PIXELS,
            area_threshold=MIN_AREA,
            merge=False
        )

        for blob in blobs:
            if not valid_blob(blob, img):
                continue

            color_name = color_id_from_blob(img, blob)
            shape_name = classify_shape(img, blob)
            short_name = color_name + shape_name

            results.append({
                "label": short_name,
                "x": blob.x(),
                "y": blob.y(),
                "w": blob.w(),
                "h": blob.h(),
                "cx": blob.cx(),
                "cy": blob.cy(),
            })

    return results

# -----------------------------

# Multi-frame Stable Match

# -----------------------------

def same_target(a, b):
    if a["label"] != b["label"]:
        return False

    dx = abs(a["cx"] - b["cx"])
    dy = abs(a["cy"] - b["cy"])

    return dx < 20 and dy < 20

def stable_results(results):
    global history

    history.append(results)
    if len(history) > HISTORY_LEN:
        history.pop(0)

    if len(history) < STABLE_NEED:
        return []

    stable = []
    latest = history[-1]

    for item in latest:
        count = 0
        for frame in history:
            found = False
            for other in frame:
                if same_target(item, other):
                    found = True
                    break
            if found:
                count += 1

        if count >= STABLE_NEED:
            stable.append(item)

    return stable

# -----------------------------

# Sort Left -> Right

# -----------------------------

def sort_results(results):
    return sorted(results, key=lambda r: r["cx"])

def update_result_stability(merged):
    global last_result, result_stable_count, last_output

    if merged == last_result:
        result_stable_count += 1
    else:
        last_result = merged
        result_stable_count = 1

    if result_stable_count >= RESULT_STABLE_NEED:
        if merged != last_output:
            last_output = merged
            return True

    return False

# -----------------------------

# Main Loop

# -----------------------------

while True:

    clock.tick()
    img = sensor.snapshot()
    img.draw_rectangle(DETECT_ROI, color=(255, 255, 0))
    frame_results = current_frame_results(img)
    final_results = sort_results(stable_results(frame_results))

    for r in final_results:
        img.draw_rectangle(r["x"], r["y"], r["w"], r["h"], color=(255, 0, 0))
        img.draw_cross(r["cx"], r["cy"], color=(0, 255, 0))
        img.draw_string(
            r["x"],
            max(r["y"] - 10, 0),
            r["label"],
            color=(255, 255, 255),
            scale=1
        )

    if final_results:
        merged = "".join([r["label"] for r in final_results])
    else:
        merged = "无目标"

    print("FRAME:", merged)

    if merged == last_result:
        result_stable_count += 1
    else:
        last_result = merged
        result_stable_count = 1

    print("stable_count:", result_stable_count)

    if result_stable_count >= RESULT_STABLE_NEED:
        if merged != last_output:
            print("FINAL:", merged)
            last_output = merged

    print("FPS:", clock.fps())

参考文章

无

硬件照片

image
image
2026-06-07 10:38
Z
星小瞳xs

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