YOLOv8水果检测示例代码换成640输入图像出现目标框绘制错误,什么原因?

Viewed 43

官网中的YOLOv8 水果检测关于图片推理的示例源代码:

from libs.YOLO import YOLOv8
import os,sys,gc
import ulab.numpy as np
import image

# 从本地读入图片,并实现HWC转CHW
def read_img(img_path):
    img_data = image.Image(img_path)
    img_data_rgb888=img_data.to_rgb888()
    img_hwc=img_data_rgb888.to_numpy_ref()
    shape=img_hwc.shape
    img_tmp = img_hwc.reshape((shape[0] * shape[1], shape[2]))
    img_tmp_trans = img_tmp.transpose()
    img_res=img_tmp_trans.copy()
    img_return=img_res.reshape((shape[2],shape[0],shape[1]))
    return img_return,img_data_rgb888

if __name__=="__main__":
    img_path="/data/test.jpg"
    kmodel_path="/data/best.kmodel"
    labels = ["apple","banana","orange"]
    confidence_threshold = 0.5
    nms_threshold=0.45
    model_input_size=[320,320]
    img,img_ori=read_img(img_path)
    rgb888p_size=[img.shape[2],img.shape[1]]
    # 初始化YOLOv8实例
    yolo=YOLOv8(task_type="detect",mode="image",kmodel_path=kmodel_path,labels=labels,rgb888p_size=rgb888p_size,model_input_size=model_input_size,conf_thresh=confidence_threshold,nms_thresh=nms_threshold,max_boxes_num=50,debug_mode=0)
    yolo.config_preprocess()
    try:
        res=yolo.run(img)
        yolo.draw_result(res,img_ori)
        gc.collect()
    except Exception as e:
        sys.print_exception(e)
    finally:
        yolo.deinit()

现在把 kmodel_path="/data/best.kmodel",中的best.kmodel替换为640输入图像,model_input_size=[640,640],就会出现目标检测的目标框位置混乱,目标框的位置完全是错的,但是检测的目标的数量是正确的。请问这个是什么原因啊,有没有什么解决方法,之前尝试过将box的位置先减掉上沿的增加量,再同比缩放,但是没什么作用,有知道的大神吗????

2 Answers

不能只改这里,模型的输入也要改,你要换一个模型

模型是用yolov8n生成的yolov8n_640,用的https://www.kendryte.com/k230_canmv/zh/dev/zh/example/ai/YOLO%E5%A4%A7%E4%BD%9C%E6%88%98.html的转化方法,转成的yolov8n_640.kmodel,大佬您看还有其他哪里可能需要调整的吗?

发一下模型,wangyan01@canaan-creative.com

模型已经发送到您邮箱了,麻烦您看一下,谢谢啦

from libs.YOLO import YOLOv8
import os,sys,gc
import ulab.numpy as np
import image

# 如果是 COCO-80 模型,请使用完整列表:
labels = [
    "person","bicycle","car","motorcycle","airplane","bus","train","truck","boat","traffic light",
    "fire hydrant","stop sign","parking meter","bench","bird","cat","dog","horse","sheep","cow",
    "elephant","bear","zebra","giraffe","backpack","umbrella","handbag","tie","suitcase","frisbee",
    "skis","snowboard","sports ball","kite","baseball bat","baseball glove","skateboard","surfboard",
    "tennis racket","bottle","wine glass","cup","fork","knife","spoon","bowl","banana","apple","sandwich",
    "orange","broccoli","carrot","hot dog","pizza","donut","cake","chair","couch","potted plant","bed",
    "dining table","toilet","tv","laptop","mouse","remote","keyboard","cell phone","microwave","oven",
    "toaster","sink","refrigerator","book","clock","vase","scissors","teddy bear","hair drier","toothbrush"
]

# —— 确保最前面就定义好 split_path ——
def split_path(path):
    parts = path.rsplit('/', 1)
    if len(parts) == 1:
        dir_path, filename = '', parts[0]
    else:
        dir_path, filename = parts
    name_parts = filename.rsplit('.', 1)
    if len(name_parts) == 1:
        name, ext = name_parts[0], ''
    else:
        name, ext = name_parts
        ext = '.' + ext
    return dir_path, name, ext

# 从本地读入图片,并实现HWC转CHW
def read_img(img_path):
    img_data = image.Image(img_path)
    img_data_rgb888=img_data.to_rgb888()
    img_hwc=img_data_rgb888.to_numpy_ref()
    shape=img_hwc.shape
    img_tmp = img_hwc.reshape((shape[0] * shape[1], shape[2]))
    img_tmp_trans = img_tmp.transpose()
    img_res=img_tmp_trans.copy()
    img_return=img_res.reshape((shape[2],shape[0],shape[1]))
    print(img_return.shape[2],"," ,img_return.shape[1])
    print(img_data_rgb888.height())
    return img_return,img_data_rgb888

if __name__=="__main__":
    # 可以根据您的模型自行修改路径参数
    img_path="/data/test_apple.jpg"
    kmodel_path="/sdcard/examples/kmodel/yolov8n_320.kmodel"
    # labels = ["apple","banana","orange"]
    # print("hello")
    confidence_threshold = 0.5
    nms_threshold = 0.45
    model_input_size=[320,320]
    img,img_ori=read_img(img_path)
    # print(img.shape[2],img.shape[1])
    rgb888p_size=[img.shape[2],img.shape[1]] #img.shape[2]=1024,img.shape[1]=1024
    # 初始化YOLOv8实例
    yolo=YOLOv8(task_type="detect",
                mode="image",
                kmodel_path=kmodel_path,
                labels=labels,
                rgb888p_size=rgb888p_size,
                model_input_size=model_input_size,
                conf_thresh=confidence_threshold,
                nms_thresh=nms_threshold,
                debug_mode=0)
    yolo.config_preprocess()


    try:
        res=yolo.run(img)

        valid_res = []
        if res is None or len(res) == 0:
            print("⚠️ 未检测到任何目标!")
        else:
            print(f"✅ 共检测到 {len(res)} 条原始结果:")
            for i, row in enumerate(res):
                cid = int(row[5])
                name = labels[cid] if 0 <= cid < len(labels) else "unknown"
                print(f"  {i}: class_id={cid} ({name}), conf={row[4]:.2f}, box={[int(x) for x in row[:4]]}")

        yolo.draw_result(res, img_ori)

        # 生成功能:拼接保存路径
        dir_path, name, ext = split_path(img_path)
        save_name = "{}_result{}".format(name, ext)
        save_path = dir_path + '/' + save_name if dir_path else save_name

        img_to_save = img_ori.to_jpeg(quality=95)

        # 保存结果图像
        img_to_save.save(save_path)
        print("检测结果已保存到:", save_path)


        gc.collect()
    except Exception as e:
        sys.print_exception(e)
    finally:
        yolo.deinit()

你这个是你yolo环境搭建的有问题,可能是onnx版本太高,导致onnx模型就不对,我邮箱给你发的这个是正常的。

那您这边onnx的版本是采用的哪一个?

yolov8之后的版本全部集中的ultralytics这里面,小版本非常多,一定要注意版本,我这里onnx版本为:
onnx 1.16.0 pypi_0 pypi
onnxruntime 1.19.2 pypi_0 pypi
onnxsim 0.4.36 pypi_0 pypi
onnx的opset为17