Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

match coco evaluate shape and creat own get_image_id function #345

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
![](https://img.shields.io/static/v1?label=pytorch&message=1.4&color=<COLOR>)
[![](https://img.shields.io/static/v1?label=license&message=Apache2&color=green)](./License.txt)

-Add own xml to yolov4 train.txt.

-match the coco evaluate shape requirement.

A minimal PyTorch implementation of YOLOv4.
- Paper Yolo v4: https://arxiv.org/abs/2004.10934
- Source code:https://github.com/AlexeyAB/darknet
Expand Down
2 changes: 2 additions & 0 deletions dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -423,6 +423,7 @@ def get_image_id(filename:str) -> int:
>>> no = f"{int(no):04d}"
>>> return int(lv+no)
"""

# raise NotImplementedError("Create your own 'get_image_id' function")
# lv, no = os.path.splitext(os.path.basename(filename))[0].split("_")
# lv = lv.replace("level", "")
Expand All @@ -437,6 +438,7 @@ def get_image_id(filename:str) -> int:
return id



if __name__ == "__main__":
from cfg import Cfg
import matplotlib.pyplot as plt
Expand Down
80 changes: 80 additions & 0 deletions xml2yolov4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# -*- coding: utf-8 -*-
"""
Created on Wed Aug 26 15:05:40 2020

@author: Liu qi
"""


from xml.dom.minidom import parse
import xml.dom.minidom
import os
import shutil

def get_file_path(file_path):
'''
:param filename:修改后路径
:param file_path: 图片所在label文件夹目录
:param h,w: 图片的长宽
:return:图片的路径列表
'''
img_paths = []
img_names = os.listdir(file_path)
for img_name in img_names:
img_path = os.path.join(file_path, img_name)
img_paths.append(img_path)
return img_paths
def mkdir_if_not_exist(path):
if not os.path.exists(os.path.join(*path)):
os.makedirs(os.path.join(*path))
return os.path.join(*path)
path = r'D:\bonc\DPcase\yolov4_yolov5\nonmotor\new_data_1_label'
mkdir_if_not_exist(['./images/train/'])
xml_paths = get_file_path(path)

label_dict = {"bicycle":0,
"express_bicycle":1,
"express_tricycle":2,
"tricycle":3,
"electric_bicycle":4,}
f = open('train_1.txt','w+')

for xml_path in xml_paths:
domTree = parse(xml_path)
rootNode = domTree.documentElement
#img_name
filename = rootNode.getElementsByTagName("filename")
img_name = filename[0].childNodes[0].data
#move image
#shutil.copy('D:/bonc/DPcase/non-motor vehicle/new_data_1/'+img_name,'../data/'+img_name)

rm = True
xmlobjects = rootNode.getElementsByTagName("object")
for xmlobject in xmlobjects:
obname = xmlobject.getElementsByTagName("name")
label_name = obname[0].childNodes[0].data
if label_name in label_dict:
if rm:
f.write('\n')
f.write('../nonmotor/images/train/'+img_name)
rm =False
label = label_dict[label_name]

obbox = xmlobject.getElementsByTagName("bndbox")

obxmin = obbox[0].getElementsByTagName("xmin")
xmin = obxmin[0].childNodes[0].data

obymin = obbox[0].getElementsByTagName("ymin")
ymin = obymin[0].childNodes[0].data

obxmax = obbox[0].getElementsByTagName("xmax")
xmax = obxmax[0].childNodes[0].data

obymax = obbox[0].getElementsByTagName("ymax")
ymax = obymax[0].childNodes[0].data


bbox_info=" %d,%d,%d,%d,%d" % (int(xmin), int(ymin), int(xmax), int(ymax), label)
f.write(bbox_info)
f.close()