当前位置:网站首页>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)
边栏推荐
- TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
- I2C subsystem (III): I2C driver
- Pytest (6) -fixture (Firmware)
- MySql实战45讲【行锁】
- Docker install redis
- Source code analysis | layout file loading process
- Kubernetes family container housekeeper pod online Q & A?
- Can I use read-only to automatically implement properties- Is read-only auto-implemented property possible?
- Your family must be very poor if you fight like this!
- Update and return document in mongodb - update and return document in mongodb
猜你喜欢

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

力扣------网格中的最小路径代价

I2C subsystem (II): I3C spec

Opengauss database development and debugging tool guide

函数栈帧的创建与销毁
![[error record] the parameter 'can't have a value of' null 'because of its type, but the im](/img/1c/46d951e2d0193999f35f14d18a2de0.jpg)
[error record] the parameter 'can't have a value of' null 'because of its type, but the im

Joking about Domain Driven Design (III) -- Dilemma

Kubernetes family container housekeeper pod online Q & A?

I2C subsystem (I): I2C spec

Segmentation fault occurs during VFORK execution
随机推荐
leetcode540
How to use asp Net MVC identity 2 change password authentication- How To Change Password Validation in ASP. Net MVC Identity 2?
Can netstat still play like this?
Andwhere multiple or query ORM conditions in yii2
JS finds all the parent nodes or child nodes under a node according to the tree structure
ASP. Net core 6 framework unveiling example demonstration [02]: application development based on routing, MVC and grpc
As a leader, how to control the code version and demand development when the epidemic comes| Community essay solicitation
当lambda没有输入时,是何含义?
Last update time of all sqlserver tables
Xiaodi notes
The base value is too large (the error is marked as "08") [duplicate] - value too great for base (error token is'08') [duplicate]
Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
超好用的日志库 logzero
MySQL Real combat 45 [SQL query and Update Execution Process]
Update and return document in mongodb - update and return document in mongodb
js根据树结构查找某个节点的下面的所有父节点或者子节点
[flutter] example of asynchronous programming code between future and futurebuilder (futurebuilder constructor setting | handling flutter Chinese garbled | complete code example)
力扣------网格中的最小路径代价
Spark on yarn资源优化思路笔记
Kubernetes cluster log and efk architecture log scheme