网站整合推广,电子产品网站建设分析的摘要,自己服务器建设网站,医疗行业企业网站建设文章目录 参考链接步骤认识正确的instances_val2017.json文件格式 代码#xff08;mogui_tococo.py#xff0c;用于我自己的数据集#xff09; 参考链接
需要先在环境中安装pycocotools
pip install pycocotools魔鬼面具的代码#xff1a;objectdetection-tricks/tricks_… 文章目录 参考链接步骤认识正确的instances_val2017.json文件格式 代码mogui_tococo.py用于我自己的数据集 参考链接
需要先在环境中安装pycocotools
pip install pycocotools魔鬼面具的代码objectdetection-tricks/tricks_4.py 相关视频教学tricks_4 用于yolov5和v7中的yolo格式转换coco格式的脚本.(如何在v5和v7中输出ap_small,ap_middle,ap_large coco指标) 一些概念上的了解通过pycocotools获取每个类别的COCO指标
步骤
步骤很简单但难在第一步
得到待检测的数据集的instances_val2017.json文件参考魔鬼面具代码和视频即可我直接使用起来有错应该是因为数据集分布的问题在test.py里面定位到anno_json 然后指明1得到的instances_val2017.json文件的位置我是像下面那样直接指明的绝对位置
anno_json rG:\pycharmprojects\autodl-yolov7\yolov7-main-biyebase\TXTOCOCO\instances_val2017.json在test.py中定位到--save-json, actionstore_true,然后加上defaultTrue,加上就能检测大中小尺寸了就算json文件格式不对也不会直接报错终止但是不会出现结果
认识正确的instances_val2017.json文件格式
圈起来的就是正确信息如果annotations为0 items的话那就说明转出来的json文件内容有误下图格式是在AutoDL云服务器上看的看起来比较清楚本地上看就比较杂乱。之前一直我运行一直有错就是因为没有转正确我的image_id的value值写成了test/Czech_000037有了test/就报错了 instances_val2017.json和best_predictions.json对比 代码mogui_tococo.py用于我自己的数据集
参考魔鬼面具的应该会没有错以下是我针对我的数据集进行的一代你代码改动留作备份
import os
import cv2
import json
from tqdm import tqdm
from sklearn.model_selection import train_test_split
import argparseparser argparse.ArgumentParser()
parser.add_argument(--root_dir, default/home/hjj/Desktop/dataset/dataset_seaship, typestr, helproot path of images and labels, include ./images and ./labels and classes.txt)
parser.add_argument(--save_path, typestr, defaultinstances_val2017.json, helpif not split the dataset, give a path to a json file)arg parser.parse_args()def yolo2coco(arg):with open(rG:\pycharmprojects\autodl-yolov7\yolov7-main-biyebase\TXTOCOCO\classes.txt, r) as f:classes list(map(lambda x: x.strip(), f.readlines()))# --------------- lwd ---------------- #cities [Czech, India, Japan]indexes []for city in cities:city_imagedir fF:/A_Publicdatasets/RDD2020-1202/train_valid/{city}/images/testfor file in os.listdir(city_imagedir):indexes.append(f{city_imagedir}/{file})# --------------- lwd ---------------- #dataset {categories: [], annotations: [], images: []}for i, cls in enumerate(classes, 0):dataset[categories].append({id: i, name: cls, supercategory: mark})# 标注的idann_id_cnt 0for k, index in enumerate(tqdm(indexes)):# 支持 png jpg 格式的图片。txtPath index.replace(images, labels).replace(.jpg, .txt)# 读取图像的宽和高im cv2.imread(index)imageFile index.split(/)[-1] # img.jpgheight, width, _ im.shape# 添加图像的信息if not os.path.exists(txtPath):# 如没标签跳过只保留图片信息。continuedataset[images].append({file_name: imageFile,id: int(imageFile[:-4]) if imageFile[:-4].isnumeric() else imageFile[:-4],width: width,height: height})with open(txtPath, r) as fr:labelList fr.readlines()for label in labelList:label label.strip().split()x float(label[1])y float(label[2])w float(label[3])h float(label[4])# convert x,y,w,h to x1,y1,x2,y2H, W, _ im.shapex1 (x - w / 2) * Wy1 (y - h / 2) * Hx2 (x w / 2) * Wy2 (y h / 2) * H# 标签序号从0开始计算, coco2017数据集标号混乱不管它了。cls_id int(label[0])width max(0, x2 - x1)height max(0, y2 - y1)dataset[annotations].append({area: width * height,bbox: [x1, y1, width, height],category_id: cls_id,id: ann_id_cnt,image_id: int(imageFile[:-4]) if imageFile[:-4].isnumeric() else imageFile[:-4],iscrowd: 0,# mask, 矩形是从左上角点按顺时针的四个顶点segmentation: [[x1, y1, x2, y1, x2, y2, x1, y2]]})ann_id_cnt 1# 保存结果with open(arg.save_path, w) as f:json.dump(dataset, f)print(Save annotation to {}.format(arg.save_path))if __name__ __main__:yolo2coco(arg)