yolov3 调试(4):提取 valid 的结果,分治思想下保存识别物体在原图的位置

import os
def create_mapping_dic(mapping_txt): # 保存裁剪图片在原图中的位置,用于 save_recognize_result 的映射
    mapping_dic = {}
    txt = open(mapping_txt, 'r').readlines()

    for info in txt:
        info = info.split() # info = [图片编号, x左上, y左上, x右下, y右下]
        photo_name = info[0]

        x1 = int(float(info[1]))
        y1 = int(float(info[2]))
        x2 = int(float(info[3]))
        y2 = int(float(info[4]))

        mapping_dic[photo_name] = (x1, y1, x2, y2)

    return mapping_dic      



def save_recognize_result(detect_dir, detect_file_name, number, recognize_dir, mapping_dic, thresh = 0.20):
    '''
    detect_dir: 保存 识别一个数字的位置的 txt 文本 的目录的地址
    detect_file_name: valid 出来的 txt 文本的前缀
    number: 识别什么数字
    recognize_dir: 经映射后的原图数字位置
    thresh: 只显示置信度大于阈值的识别结果
    mapping_dic: 保存裁剪图片对应原图的位置信息,构造映射
    '''

    detect_txt = os.path.join(detect_dir, detect_file_name  + str(number) + '.txt') # valid 出来的 txt 文本的地址
    txt = open(detect_txt, 'r').readlines()

    for info in txt:
        info = info.split() # info = [图片编号, 置信值, x左上, y左上, x右下, y右下]
        ground_truth = float(info[1])

        if ground_truth > thresh: # 只裁剪出检测时置信度大于阈值的识别物体

            photo_name = info[0]
            x0, y0, _, _ = mapping_dic[photo_name]

            recognize_txt = photo_name[:-2] + '.txt'
            recognize_txt_path = os.path.join(recognize_dir, recognize_txt)

            if recognize_txt not in os.listdir(recognize_dir):
                txt_write = open(recognize_txt_path, 'w') # 若文本不存在,先创建新文本,然后写入
            else:
                txt_write = open(recognize_txt_path, 'a') # 追加模式,写入而不覆盖原内容

            x1 = x0 + int(float(info[2]))
            y1 = y0 + int(float(info[3]))
            x2 = x0 + int(float(info[4]))
            y2 = y0 + int(float(info[5]))

            recognize_result = [number, x1, y1, x2, y2]
            recognize_result = [str(x) for x in recognize_result]
            recognize_string = ' '.join(recognize_result)
            txt_write.write(recognize_string)
            txt_write.write('\n')
    print('successfully save the info of recognizing number ' + str(number) + ' !')

mapping_dic = create_mapping_dic('mapping.txt')
for number in range(10): #十个数字依次识别
    save_recognize_result('valid', 'comp4_det_test_', number, 'recognize_txt', mapping_dic, thresh = 0.20)

注意点

file = open(file_name, 'a')
表示追加模式,循环结构,写入时可以不覆盖原内容,’w’ 则会覆盖原内容
但是 ‘a’ 不会创建新文本,所以循环的第一次需要 ‘w’,或先创建好空白的文本

### YOLOv5 Detect Function Error 'fatal: Not a valid object name HEAD' with Torch 2.4.1 Solution 当使用较新的 PyTorch 版本(如 2.4.1)运行 YOLOv5 的 `detect` 函数时,可能会遇到 `'fatal: Not a valid object name HEAD'` 错误。此错误通常与 Git 存储库的状态有关,可能是因为克隆存储库时不完整或本地存储库损坏所致。 以下是针对该问题的分析和解决方案: #### 1. **Git 存储库状态修复** 如果在执行检测脚本之前未正确初始化子模块,则可能出现上述错误。可以通过重新同步子模块来解决问题: ```bash git submodule update --init --recursive ``` 这一步骤会确保所有必要的依赖项被正确加载到项目中[^3]。 #### 2. **降级 PyTorch 至兼容版本** 由于您提到的是基于较新版本的 PyTorch (2.4.1),而 YOLOv5 官方推荐使用的 PyTorch 版本为 1.8.0 或更低版本,因此建议先卸载当前版本并安装指定版本: ```bash pip uninstall torch torchvision torchaudio pip install torch==1.8.0+cpu torchvision==0.9.0+cpu -f https://download.pytorch.org/whl/torch_stable.html ``` 通过这种方式可以避免因高版本引入的新特性而导致的不兼容性问题[^1]。 #### 3. **验证 pycocotools 是否正常工作** 对于 Windows 用户来说,另一个常见问题是 `pycocotools` 的安装失败。为了防止后续出现类似的依赖冲突,请按照官方指南手动编译或者寻找预构建二进制文件进行安装[^2]: - 手动编译方式适用于熟悉 C++ 编程环境的人群; - 预构建包则可以直接从第三方资源获取,比如 Unofficial Windows Binaries for Python Extension Packages。 #### 4. **调整 detect 调用参数** 最后,在调用 `detect.py` 文件前确认输入路径、模型权重位置以及设备编号均设置无误。例如下面的例子展示了如何利用摄像头作为数据源完成目标识别操作: ```python python detect.py --source 0 --weights yolov5s.pt --device cpu ``` 这里特别注意将 `--device` 参数设定成 `"cpu"` 来适配非 GPU 场景下的推理需求[^4]。 --- ###
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值