当前位置:网站首页>labelme标记的文件转换为yolov5格式
labelme标记的文件转换为yolov5格式
2022-07-03 03:06: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保存位置
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'): # 仅适用矩形框标注
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'): # 适用点标注
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文件夹路径
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)
边栏推荐
- Add MDF database file to SQL Server database, and the error is reported
- What is the way out for children from poor families?
- Practice of traffic recording and playback in vivo
- yii2 中andWhere多个or查询 orm条件
- 超好用的日志库 logzero
- The difference between componentscan and componentscans
- Agile certification (professional scrum Master) simulation exercises
- 迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
- tensor中的append应该如何实现
- Add automatic model generation function to hade
猜你喜欢
![[principles of multithreading and high concurrency: 1_cpu multi-level cache model]](/img/c7/6b5ab4ff7379bfccff7cdbb358ff8f.jpg)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]

Didi programmers are despised by relatives: an annual salary of 800000 is not as good as two teachers

vfork执行时出现Segmentation fault

TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
![[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)](/img/04/88ce45d370a2e6052c2fce558aa531.jpg)
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)

你真的懂继电器吗?

MySql實戰45講【SQL查詢和更新執行流程】
![[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling](/img/c3/b9a614001f80345a5c1cb3c68ab27c.jpg)
[shutter] monitor the transparency gradient of the scrolling action control component (remove the blank of the top status bar | frame layout component | transparency component | monitor the scrolling

I2C subsystem (II): I3C spec

I2C 子系统(二):I3C spec
随机推荐
MySql實戰45講【SQL查詢和更新執行流程】
力扣------网格中的最小路径代价
Practice of traffic recording and playback in vivo
一文带你了解 ZigBee
Le processus de connexion mysql avec docker
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
Source code analysis | layout file loading process
用docker 連接mysql的過程
Add automatic model generation function to hade
yii2 中andWhere多个or查询 orm条件
C语言中左值和右值的区别
The difference between componentscan and componentscans
内存泄漏工具VLD安装及使用
Segmentation fault occurs during VFORK execution
二维数组中的元素求其存储地址
Kubernetes family container housekeeper pod online Q & A?
MySQL Real combat 45 [SQL query and Update Execution Process]
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
Find the storage address of the elements in the two-dimensional array
Kubernetes cluster log and efk architecture log scheme