当前位置:网站首页>Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
Visualisation de l'ensemble de données au format yolov5 (fichier labelme json)
2022-07-03 01:53:00 【Athrunsunny】
Dans son propre projet,Il arrive souvent qu'il y ait peu d'ensembles de données,Mais il y a des données bien marquées sur Internet,Plus ou moins différent des exigences d'étiquetage de votre projet,Et je ne veux pas re - étiqueter,Je veux juste le peaufiner.,Maisyolov5Le format natif de n'est pas intuitif,C'est le moment deyolov5Données formatées converties enlabelmeDejsonFormat,Cela facilite le réglage fin des dimensions des données,En même temps, il n'y a pas besoin de beaucoup d'attention pour étiqueter les mégadonnées,Réduire les coûts de main - d'oeuvre.
# -*- coding: utf-8 -*-
"""
Time: 2021.10.26
Author: Athrunsunny
Version: V 0.1
File: yolotolabelme.py
Describe: Functions in this file is change the dataset format to labelme json file
"""
import base64
import io
import os
import numpy as np
import json
from glob import glob
import cv2
import shutil
import yaml
from tqdm import tqdm
import PIL.Image
ROOT_DIR = os.getcwd()
VERSION = '4.5.7' # Selonlabelme Pour modifier
def img_arr_to_b64(img_arr):
img_pil = PIL.Image.fromarray(img_arr)
f = io.BytesIO()
img_pil.save(f, format="PNG")
img_bin = f.getvalue()
if hasattr(base64, "encodebytes"):
img_b64 = base64.encodebytes(img_bin)
else:
img_b64 = base64.encodestring(img_bin)
return img_b64
def process_point(points, cls):
info = list()
for point in points:
shape_info = dict()
shape_info['label'] = cls[int(point[0])]
if point is None:
shape_info['points'] = [[], []]
else:
shape_info['points'] = [[point[1], point[2]],
[point[3], point[4]]]
shape_info['group_id'] = None
shape_info['shape_type'] = 'rectangle'
shape_info['flags'] = dict()
info.append(shape_info)
return info
def create_json(img, imagePath, filename, info):
data = dict()
data['version'] = VERSION
data['flags'] = dict()
data['shapes'] = info
data['imagePath'] = imagePath
height, width = img.shape[:2]
data['imageData'] = img_arr_to_b64(img).decode('utf-8')
data['imageHeight'] = height
data['imageWidth'] = width
jsondata = json.dumps(data, indent=4, separators=(',', ': '))
f = open(filename, 'w')
f.write(jsondata)
f.close()
def read_txt(path):
assert os.path.exists(path)
with open(path, mode='r', encoding="utf-8") as f:
content = f.readlines()
content = np.array(content)
res = []
for index, item in enumerate(content):
string = item.split(' ')
res.append(list(map(np.float64, string)))
return np.array(res)
def load_dataset_info(path=ROOT_DIR):
yamlpath = glob(path + "\\*.yaml")[0]
with open(yamlpath, "r", encoding="utf-8") as f:
data = yaml.load(f, Loader=yaml.FullLoader)
return data
def reconvert_list(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = box[0] / dw
w = box[2] / dw
y = box[1] / dh
h = box[3] / dh
x1 = ((x + 1) * 2 - w) / 2.
y1 = ((y + 1) * 2 - h) / 2.
x2 = ((x + 1) * 2 + w) / 2.
y2 = ((y + 1) * 2 + h) / 2.
return x1, y1, x2, y2
def reconvert_np(size, box):
dw = 1. / (size[0])
dh = 1. / (size[1])
x = box[:, :1] / dw
w = box[:, 2:3] / dw
y = box[:, 1:2] / dh
h = box[:, 3:4] / dh
box[:, :1] = ((x + 1) * 2 - w) / 2.
box[:, 2:3] = ((x + 1) * 2 + w) / 2.
box[:, 1:2] = ((y + 1) * 2 - h) / 2.
box[:, 3:4] = ((y + 1) * 2 + h) / 2.
return box
def txt2json(proctype, cls, path=ROOT_DIR):
process_image_path = os.path.join(path, proctype, 'images')
process_label_path = os.path.join(path, proctype, 'labels')
externs = ['png', 'jpg', 'JPEG', 'BMP', 'bmp']
imgfiles = list()
for extern in externs:
imgfiles.extend(glob(process_image_path + "\\*." + extern))
createfile = os.path.join(ROOT_DIR, 'createjson', proctype)
if not os.path.exists(createfile):
os.makedirs(createfile)
for image_path in tqdm(imgfiles):
frame = cv2.imread(image_path)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
height, width = frame.shape[:2]
size = (width, height)
imgfilename = image_path.replace("\\", "/").split("/")[-1]
imgname = '.'.join(imgfilename.split('.')[:-1])
jsonpath = os.path.join(createfile, imgname + '.json')
txtpath = os.path.join(process_label_path, imgname + '.txt')
label_and_point = read_txt(txtpath)
label_and_point[:, 1:] = reconvert_np(size, label_and_point[:, 1:])
info = process_point(label_and_point, cls)
create_json(frame, imgname, jsonpath, info)
shutil.copy(image_path, createfile)
def yolotolabelme(path=ROOT_DIR):
pathtype = list()
if 'train' in os.listdir(path):
pathtype.append('train')
if 'valid' in os.listdir(path):
pathtype.append('valid')
if 'test' in os.listdir(path):
pathtype.append('test')
cls = load_dataset_info()['names']
for file_type in pathtype:
print("Processing image type {} \n".format(file_type))
txt2json(file_type, cls)
if __name__ == "__main__":
yolotolabelme()
Nommez le code ci - dessus comme suit:yolotolabelme.py Et stocké dans le Répertoire racine de l'ensemble de données

Mettez le code ci - dessus avant d'exécuter le programme import Quelques bibliothèques de ,Plus tard.

Après l'exécution, il est généré sous ce chemin createjsonDossiers

Les données converties seront basées sur trainOuvalidProduit àcreatejsonSous le dossier,Par la suitelabelmeOuvre.

A cause de montest L'ensemble de données est vide , Donc C'est aussi vide après la conversion ,UtiliserlabelmeOuvre ça.train Les fichiers sous le chemin peuvent voir les dimensions correspondantes

边栏推荐
- What are the differences between software testers with a monthly salary of 7K and 25K? Leaders look up to you when they master it
- 2022 financial product revenue ranking
- word插入公式/endnote
- 网络安全-ACL访问控制列表
- Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance o
- CF1617B Madoka and the Elegant Gift、CF1654C Alice and the Cake、 CF1696C Fishingprince Plays With Arr
- Huakaiyun (Zhiyin) | virtual host: what is a virtual host
- STM32 - switch of relay control lamp
- 网络安全-信息收集
- VIM 9.0 is officially released! The execution speed of the new script can be increased by up to 100 times
猜你喜欢

The technology boss is ready, and the topic of position C is up to you

¢ growth path and experience sharing of getting an offer

STM32 - switch of relay control lamp

【Camera专题】OTP数据如何保存在自定义节点中

In 2022, 95% of the three most common misunderstandings in software testing were recruited. Are you that 5%?

NCTF 2018 part Title WP (1)

"Jetpack - livedata parsing"

Anna: Beibei, can you draw?

Vant implements a simple login registration module and a personal user center

Analysis, use and extension of open source API gateway apisex
随机推荐
Storage basic operation
Network security - dynamic routing protocol rip
File class (add / delete)
小程序開發的部分功能
One of the C language practical projects is greedy snake
网络安全-信息收集
[data mining] task 4:20newsgroups clustering
How to refresh the opening amount of Oracle ERP
word插入公式/endnote
Summary of interval knowledge
String splicing function of MySQL
[keil5 debugging] debug is stuck in reset_ Handler solution
Modify table structure
Huakaiyun | virtual host: IP, subnet mask, gateway, default gateway
Tâche 6: regroupement DBSCAN
【數據挖掘】任務6:DBSCAN聚類
小程序开发的部分功能
Learn BeanShell before you dare to say you know JMeter
A simple tool for analyzing fgui dependencies
VIM 9.0 is officially released! The execution speed of the new script can be increased by up to 100 times