mmdetection 是时下比较常用的目标检测工具箱,集成了faster ,cascade,yolo 等性能比较好的检测算法,熟练使用mmdetection进行自定义数据的目标检测就成为了很多AIer的必经之路。
项目仓库地址:https://github.com/open-mmlab/mmdetection
安装,装环境就略过了。
装完之后,想要训练自己的数据集,首先要将自己的数据整理成COCO的格式:
--------data
---------------coco
-----------------------annotations
---------------------------------------train.json,val.json
------------------------images
---------------------------------------......jpg
annotations文件夹下包含了train.json和val.json,images文件夹下则为全部图像数据。
自己的数据标签格式需要整理为json才行,具体的转换方法可以直接去搜程序,也可以自己写。这里给出一部分,可能不适应直接用。
1.xml转coco
import os.path as osp
import xml.etree.ElementTree as ET
import mmcv
from mmdet.core import sea_classes
from glob import glob
from tqdm import tqdm
from PIL import Image
label_ids = {name: i + 1 for i, name in enumerate(sea_classes())}
def get_segmentation(points):
return [points[0], points[1], points[2] + points[0], points[1],
points[2] + points[0], points[3] + points[1], points[0], points[3] + points[1]]
def parse_xml(xml_path, img_id, anno_id):
tree = ET.parse(xml_path)
# print(str(xml_path))
root = tree.getroot()
annotation = []
for obj in root.findall('object'):
try:
name = obj.find('name').text
# if name in ['lighter','powerbank','zippooil','firecrackers','handcuffs','pressure','slingshot','nailpolish']:
# continue
category_id = label_ids[name]
bnd_box = obj.find('bndbox')
xmin = int(bnd_box.find('xmin').text)
ymin = int(bnd_box.find('ymin').text)
xmax = int(bnd_box.find('xmax').text)
ymax = int(bnd_box.find('ymax').text)
if xmin >= xmax or ymin >= ymax:
continue
w = xmax - xmin + 1
h = ymax - ymin + 1
area = w * h
segmentation = get_segmentation([xmin, ymin, w, h])
annotation.append({
"segmentation": segmentation,
"area": area,
"iscrowd": 0,
"image_id": img_id,
"bbox": [xmin, ymin, w, h],
"category_id": category_id,
"id": anno_id,
"ignore": 0})
anno_id += 1
except:
continue
return annotation, anno_id
def cvt_annotations(img_path, xml_p

最低0.47元/天 解锁文章
1121

被折叠的 条评论
为什么被折叠?



