当前位置:网站首页>The file marked by labelme is converted to yolov5 format
The file marked by labelme is converted to yolov5 format
2022-07-03 03:09:00 【AphilGuo】
import json
import os
def convert(img_size, box):
x1 = box[0]
y1 = box[1]
x2 = box[2]
y2 = box[3]
center_x = (x1 + x2) * 0.5 / img_size[0]
center_y = (y1 + y2) * 0.5 / img_size[1]
w = abs((x2 - x1)) * 1.0 / img_size[0]
h = abs((y2 - y1)) * 1.0 / img_size[1]
return (center_x, center_y, w, h)
def convert1(img_size, box):
x1 = box[0]
y1 = box[1]
center_x = x1 / img_size[0]
center_y = y1 / img_size[1]
return (center_x, center_y)
def decode_json(jsonfloder_path, json_name):
txt_name = '/yolov5_labels/' + json_name[0:-5] + '.txt'
# txt Save the location
txt_file = open(txt_name, 'w') # te files
json_path = os.path.join(json_folder_path, json_name)
data = json.load(open(json_path, 'r'))
img_w = data['imageWidth']
img_h = data['imageHeight']
for i in data['shapes']:
if (i['shape_type'] == 'rectangle'): # Rectangular box dimensions only
x1 = float(i['points'][0][0])
y1 = float(i['points'][0][1])
x2 = float(i['points'][1][0])
y2 = float(i['points'][1][1])
if x1 < 0 or x2 < 0 or y1 < 0 or y2 < 0:
continue
else:
bb = (x1, y1, x2, y2)
bbox = convert((img_w, img_h), bb)
if i['label'] == "jc_re":
txt_file.write("0 " + " ".join([str(a) for a in bbox])+" ")
# elif i['label'] == "jc_point":
# txt_file.write("1 " + " ".join([str(a) for a in bbox]) + '\n')
# elif i['label'] == "Computer":
# txt_file.write("2 " + " ".join([str(a) for a in bbox]) + '\n')
# else:
# txt_file.write("3 " + " ".join([str(a) for a in bbox]) + '\n')
elif (i['shape_type'] == 'point'): # Applicable point annotation
x1 = float(i['points'][0][0])
y1 = float(i['points'][0][1])
if x1 < 0 or y1 < 0:
continue
else:
bb = (x1, y1)
bbox = convert1((img_w, img_h), bb)
if i['label'] == "jc_point":
# txt_file.write(" ".join([str(a) for a in bbox]) + "\n")
txt_file.write(" ".join([str(a) for a in bbox])+" ")
if __name__ == "__main__":
json_folder_path = 'json_path' # json Folder path
json_names = os.listdir(json_folder_path) # file name
for json_name in json_names: # output all files
if json_name[-5:] == '.json': # just work for json files
decode_json(json_folder_path, json_name)
边栏推荐
- 处理数据集,使用LabelEncoder将所有id转换为从0开始
- MySql实战45讲【索引】
- Super easy to use logzero
- Force deduction ----- the minimum path cost in the grid
- How to limit the size of the dictionary- How to limit the size of a dictionary?
- 当lambda没有输入时,是何含义?
- TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
- L'index des paramètres d'erreur est sorti de la plage pour les requêtes floues (1 > Nombre de paramètres, qui est 0)
- Tensorflow to pytorch notes; tf. gather_ Nd (x, y) to pytorch
- Notifydatasetchanged not applicable to recyclerview - notifydatasetchanged not working on recyclerview
猜你喜欢

函数栈帧的创建与销毁

The process of connecting MySQL with docker

Force deduction ----- the minimum path cost in the grid

Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)

docker安装redis

VS code配置虚拟环境

idea 加载不了应用市场解决办法(亲测)

vfork执行时出现Segmentation fault

一文带你了解 ZigBee

I2C 子系统(二):I3C spec
随机推荐
Thunderbolt Chrome extension caused the data returned by the server JS parsing page data exception
内存泄漏工具VLD安装及使用
模糊查詢時報錯Parameter index out of range (1 > number of parameters, which is 0)
分布式事务
文件重命名
MySql实战45讲【索引】
Agile certification (professional scrum Master) simulation exercise-2
Serious security vulnerabilities reported by moxa mxview network management software
PHP constructor with parameters - PHP constructor with a parameter
基于Qt的yolov5工程
Installation and use of memory leak tool VLD
Process the dataset and use labelencoder to convert all IDs to start from 0
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
Three.js本地环境搭建
tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
Kubernetes family container housekeeper pod online Q & A?
How to select the minimum and maximum values of columns in the data table- How to select min and max values of a column in a datatable?
Getting started | jetpack hilt dependency injection framework
About HTTP cache control
How to limit the size of the dictionary- How to limit the size of a dictionary?