ImageAI使用ResNet50检测人物时如何关闭检测框绘制功能
解决方案
方法1:直接使用官方内置参数(推荐,零侵入)
ImageAI的detectCustomObjectsFromImage方法自带控制检测框绘制的参数,你只需要在调用时添加display_box=False即可禁用所有检测框绘制,无需修改任何源码。
你原来的调用代码调整后如下:
detected_image, detections = detector.detectCustomObjectsFromImage(input_image=str(image), output_image_path=str(output_path), minimum_percentage_probability=20, input_type='file', output_type='file', display_percentage_probability=False, display_box=False, # 新增这一行即可 extract_detected_objects=True, custom_objects=person)
添加后无论是输出的整图还是单独提取的人物小图,都不会出现检测框,也不会出现框覆盖其他提取对象的问题。
方法2:自行裁剪原图(兼容性更强)
如果你使用的ImageAI版本没有display_box参数,也可以通过自行裁剪的方式规避绘图逻辑,完全绕开画框的步骤:
- 关闭自动提取对象的参数,改为获取检测坐标后自行裁剪
- 用OpenCV/PIL读取原图,按返回的检测框坐标裁剪后保存
示例代码如下:
import cv2 for idx, i in enumerate(files): print(f'Extracting images from: {i.name}') # 先获取检测结果,不输出带框图、不自动提取 _, detections = detector.detectCustomObjectsFromImage(input_image=str(i), minimum_percentage_probability=20, input_type='file', output_type='array', custom_objects=person) # 读取原图自行裁剪 original_img = cv2.imread(str(i)) for person_idx, det in enumerate(detections): x1, y1, x2, y2 = det["box_points"] # 裁剪人物区域 person_img = original_img[y1:y2, x1:x2] # 保存,命名按序号区分同一张图里的多个人物 save_path = output_folder.joinpath(f'{idx}_{person_idx}.jpg') cv2.imwrite(str(save_path), person_img)
这种方法不需要依赖ImageAI的绘图和提取逻辑,也不会产生任何多余的框线,同时还可以自定义保存的命名规则,方便区分同一张图里的多个人物。
内容的提问来源于stack exchange,提问作者NotAName
相关产品推荐
相关产品推荐

