当前位置:网站首页>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)
边栏推荐
- 左值右指解释的比较好的
- leetcode540
- tensorflow转pytorch笔记;tf.gather_nd(x,y)转pytorch
- Counter统计数量后,如何返回有序的key
- sql server 查询指定表的表结构
- Deep learning: multi-layer perceptron and XOR problem (pytoch Implementation)
- Xiaodi notes
- 函数栈帧的创建与销毁
- Add automatic model generation function to hade
- Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
猜你喜欢

vfork执行时出现Segmentation fault

I2C subsystem (III): I2C driver

Can netstat still play like this?
![[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]

C语言初阶-指针详解-庖丁解牛篇

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

Joking about Domain Driven Design (III) -- Dilemma

内存泄漏工具VLD安装及使用

Anhui University | small target tracking: large-scale data sets and baselines

Three. JS local environment setup
随机推荐
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
【富瀚6630编码存录像,用rtsp服务器及时间戳同步实现vlc观看录像】
Counter统计数量后,如何返回有序的key
敏捷认证(Professional Scrum Master)模拟练习题
yii2 中andWhere多个or查询 orm条件
[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
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
The difference between componentscan and componentscans
The difference between left value and right value in C language
ComponentScan和ComponentScans的区别
Docker install MySQL
TCP handshake three times and wave four times. Why does TCP need handshake three times and wave four times? TCP connection establishes a failure processing mechanism
二维数组中的元素求其存储地址
I2C subsystem (III): I2C driver
tensor中的append应该如何实现
Your family must be very poor if you fight like this!
Installation and use of memory leak tool VLD
Yiwen takes you to know ZigBee
The core idea of performance optimization, dry goods sharing
Sqlserver row to column pivot