From 8df4dc1e6a7675e8e06f21ed7accbb3760be0043 Mon Sep 17 00:00:00 2001 From: haotian <2421912570@qq.com> Date: Wed, 25 Jun 2025 09:31:41 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9--=E5=B0=81=E8=A3=85030?= =?UTF-8?q?=E8=84=9A=E6=9C=AC=E5=86=85=E5=AE=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- 008yolov8预测.py | 5 +- ...yolo预测结果写为label_studio_json格式2.py | 145 ++++++++++++++---- 2 files changed, 119 insertions(+), 31 deletions(-) diff --git a/008yolov8预测.py b/008yolov8预测.py index 9d119b0..0f68d03 100644 --- a/008yolov8预测.py +++ b/008yolov8预测.py @@ -11,9 +11,10 @@ model = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HelmetHea # images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images' # images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images_20250623' -images_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/test_images/duan8_real' +# images_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/test_images/duan8_real' +images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images_20250624105822' # save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputHardHatsV1HelmetHeadShoe2_20250623' -save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputHardHatsV1HelmetHeadShoe2_20250623_1' +save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputHardHatsV1HelmetHeadShoe2_202506624105822' all_images = [os.path.join(images_path, t) for t in os.listdir(images_path)] diff --git a/030将yolo预测结果写为label_studio_json格式2.py b/030将yolo预测结果写为label_studio_json格式2.py index 3dcbdad..7e3932b 100644 --- a/030将yolo预测结果写为label_studio_json格式2.py +++ b/030将yolo预测结果写为label_studio_json格式2.py @@ -56,6 +56,62 @@ def yolo_to_labelstudio(results, class_names, model_version="yolov8_1.0"): "model_version": model_version, "result": predictions } + +def main(model_list, class_name_list, images_path, save_file, pre_root, conf=0.3): + """_summary_ + + Args: + model_list (_type_): 模型列表 + class_name_list (_type_): 模型输出类名称 + images_path (_type_): 图像路径 + save_file (_type_): 保存json文件路径 + pre_root (_type_): json中文件路径前缀 + """ + + + image_names = os.listdir(images_path) + + all_images = [os.path.join(images_path, t) for t in image_names] + + json_list = list() + + + for i in range(len(all_images)): + + predictions = None + for j in range(len(model_list)): + results = model_list[j](all_images[i], + conf = conf, device=0) + + predictions_t = yolo_to_labelstudio(results, class_name_list[j]) + + if predictions is None: + predictions = predictions_t + else: + predictions["result"] += predictions_t["result"] + + t_d = dict() + # "劳保鞋识别/dataset/new_all/train/images/dataset2_-01-15-2-2-2-2-25_jpg.rf.267991c87898d90733d5c7623941a999.jpg" + # t_d["data"] = {"image": f"/data/local-files/?d=劳保鞋识别/dataset/new_all/train/images/{image_names[i]}"} + # t_d["data"] = {"image": f"/data/local-files/?d=images_20250624105811/{image_names[i]}"} + t_d["data"] = {"image": f"/data/local-files/?d={pre_root}/{image_names[i]}"} + t_d["predictions"] = list() + + t_d["predictions"].append(predictions) + + json_list.append(t_d) + + print(len(json_list)) + # return + with open(save_file, "w", encoding="utf-8") as f: + + json.dump(json_list,f, + indent=4, # 4空格缩进 + sort_keys=True, # 按键名字典序排序 + ensure_ascii=False # 支持非ASCII字符 + ) + + # 使用示例 if __name__ == "__main__": @@ -77,55 +133,86 @@ if __name__ == "__main__": "shoe", ] + model_2 = YOLO('/home/admin-root/haotian/python哈汽锻8安全帽识别/HelmetHeadBAC/trainHardHatsV1HelmetHeadShoe2/weights/best.pt') + CLASS_NAMES_2 = [ + "helmet", + "head", + "shoe" + ] - images_path = '/home/admin-root/haotian/劳保鞋识别/dataset/new_all/train/images' + model_list = list() + model_list.append(model_2) + + class_name_list = list() + class_name_list.append(CLASS_NAMES_2) + + + # images_path = '/home/admin-root/haotian/劳保鞋识别/dataset/new_all/train/images' + + # images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images_20250624105811' + + images_path = '/home/admin-root/haotian/锻8/tensorrtx/yolov8/images_20250624105822' # save_path = '/home/admin-root/haotian/python哈汽锻8安全帽识别/output/outputShoeV2' - - image_names = os.listdir(images_path) - - all_images = [os.path.join(images_path, t) for t in image_names] - json_list = list() - for i in range(len(all_images)): + main(model_list, class_name_list, images_path, "duan8_22.json", "images_20250624105822") + + + + - # conf 置信度在. - results_0 = model_0(all_images[i], - conf = 0.5, device=0) + + + + +#-----------------------------------------------------------------旧版代码------------------------------------------------------------------------------- + +# image_names = os.listdir(images_path) + +# all_images = [os.path.join(images_path, t) for t in image_names] + +# json_list = list() + +# for i in range(len(all_images)): + +# # conf 置信度在. +# results_0 = model_0(all_images[i], +# conf = 0.5, device=0) - predictions_0 = yolo_to_labelstudio(results_0, CLASS_NAMES_0) +# predictions_0 = yolo_to_labelstudio(results_0, CLASS_NAMES_0) - results_1 = model_1(all_images[i], - conf=0.5, device=0) +# results_1 = model_1(all_images[i], +# conf=0.5, device=0) - predictions_1 = yolo_to_labelstudio(results_1, CLASS_NAMES_1) +# predictions_1 = yolo_to_labelstudio(results_1, CLASS_NAMES_1) - predictions_0["result"] += predictions_1["result"] +# predictions_0["result"] += predictions_1["result"] - t_d = dict() - # "劳保鞋识别/dataset/new_all/train/images/dataset2_-01-15-2-2-2-2-25_jpg.rf.267991c87898d90733d5c7623941a999.jpg" - t_d["data"] = {"image": f"/data/local-files/?d=劳保鞋识别/dataset/new_all/train/images/{image_names[i]}"} - t_d["predictions"] = list() +# t_d = dict() +# # "劳保鞋识别/dataset/new_all/train/images/dataset2_-01-15-2-2-2-2-25_jpg.rf.267991c87898d90733d5c7623941a999.jpg" +# # t_d["data"] = {"image": f"/data/local-files/?d=劳保鞋识别/dataset/new_all/train/images/{image_names[i]}"} +# t_d["data"] = {"image": f"/data/local-files/?d=images_20250624105811/{image_names[i]}"} +# t_d["predictions"] = list() - t_d["predictions"].append(predictions_0) +# t_d["predictions"].append(predictions_0) - json_list.append(t_d) +# json_list.append(t_d) - # if i == 10: - # break +# # if i == 10: +# # break -with open("label_studio_new_all.json", "w", encoding="utf-8") as f: +# with open("label_studio_new_all.json", "w", encoding="utf-8") as f: - json.dump(json_list,f, - indent=4, # 4空格缩进 - sort_keys=True, # 按键名字典序排序 - ensure_ascii=False # 支持非ASCII字符 - ) +# json.dump(json_list,f, +# indent=4, # 4空格缩进 +# sort_keys=True, # 按键名字典序排序 +# ensure_ascii=False # 支持非ASCII字符 +# ) \ No newline at end of file