图片没法保存指定区域

Viewed 51

问题描述


我运行ai识别的代码,然后想要只保存识别到的区域,然后再保存的时候提示对应的函数提供了错误的参数,想问一下代码如何修正;代码如下:

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

if __name__=="__main__":
    # 这里仅为示例,自定义场景请修改为您自己的测试图片、模型路径、标签名称、模型输入大小
    img_path="/data/0811_00013.jpg"
    kmodel_path="/sdcard/examples/kmodel/best-0812.kmodel"
    labels = ["0","1"]
    model_input_size=[320,320]
    confidence_threshold = 0.01
    nms_threshold=0.45
    mask_threshold=0.5
    img,img_ori=read_image(img_path)
    rgb888p_size=[img.shape[2],img.shape[1]]

    # 初始化YOLOv8实例
    yolo=YOLOv8(task_type="segment",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,mask_thresh=mask_threshold,max_boxes_num=50,debug_mode=0)
    yolo.config_preprocess()
    res=yolo.run(img)
    print(res)





    # 处理检测结果
    if res != [[], [], []]:
        x_min = [arr[0] for arr in res[0] if isinstance(arr, np.ndarray)]
        y_min = [arr[1] for arr in res[0] if isinstance(arr, np.ndarray)]
        w = [arr[2] for arr in res[0] if isinstance(arr, np.ndarray)]
        h = [arr[3] for arr in res[0] if isinstance(arr, np.ndarray)]
        x_max = [x + width for x, width in zip(x_min, w)]
        y_max = [y + height for y, height in zip(y_min, h)]
        leibie = res[1]
        # 初始化边界框列表
        dui_xmin = []
        dui_xmax = []
        dui_w = []
        dui_h = []
        dui_ymin = []
        dui_ymax = []


        print(f"检测到 {len(x_min)} 个对象")
        print(f"类别信息: {leibie}")

        # 遍历每个检测结果
        for i in range(len(x_min)):
            # 提取类别为0的边界框
            if leibie[i] == 0:
                dui_xmin.append(x_min[i])
                dui_xmax.append(x_max[i])
                dui_w.append(w[i])
                dui_h.append(h[i])
                dui_ymin.append(y_min[i])
                dui_ymax.append(y_max[i])


        # 输出提取的边界框信息
        print(f"提取的边界框 (xmin): {dui_xmin}")
        print(f"提取的边界框 (xmax): {dui_xmax}")
        print(f"提取的边界框 (ymin): {dui_ymin}")
        print(f"提取的边界框 (ymax): {dui_ymax}")
        # 为每个边界框创建裁剪图像
        for j in range(len(dui_xmin)):
            x = int(dui_xmin[j])
            y = int(dui_ymin[j])
            w = int(dui_w[j])
            h = int(dui_h[j])
            
            # 创建裁剪区域 (x, y, w, h)
            roi = (x, y, w, h)
            
            # 创建裁剪图像
            cropped_img = img.copy(roi=roi)
            
            # 保存裁剪图像
            crop_path = os.path.join(output_dir, f"{img_name_no_ext}_bbox_crop_{j}.jpg")
            cropped_img.save("/data/test111.jpg")



    yolo.draw_result(res,img_ori)
    yolo.deinit()
    gc.collect()

复现步骤


问题提示 :cropped_img = img.copy(roi=roi)
File "", line 79, in
TypeError: function doesn't take keyword arguments

2 Answers

你这个代码太复杂了,用下边的吧:

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

if __name__=="__main__":
    # 这里仅为示例,自定义场景请修改为您自己的测试图片、模型路径、标签名称、模型输入大小
    img_path="/sdcard/examples/utils/test_fruit.jpg"
    kmodel_path="/sdcard/examples/kmodel/fruit_det_yolov8n_320.kmodel"
    labels = ["apple","banana","orange"]
    model_input_size=[320,320]

    confidence_threshold = 0.5
    nms_threshold=0.45
    img,img_ori=read_image(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()
    res=yolo.run(img)
    for i in range(len(res[0])):
        x,y,w,h=res[0][i][0],res[0][i][1],res[0][i][2],res[0][i][3]
        box_img=img_ori.copy(roi=(x,y,w,h)).to_rgb565()
        box_img.save(f"/sdcard/crop_{i}.jpg")
    yolo.draw_result(res,img_ori)
    yolo.deinit()
    gc.collect()

from libs.YOLO import YOLOv8找不到,咱整?我用的是k230