当前位置:网站首页>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)
边栏推荐
- Privatization lightweight continuous integration deployment scheme -- 01 environment configuration (Part 2)
- Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
- Distributed transaction
- MySQL practice 45 [global lock and table lock]
- [fluent] future asynchronous programming (introduction | then method | exception capture | async, await keywords | whencomplete method | timeout method)
- 销毁Session和清空指定的属性
- js根据树结构查找某个节点的下面的所有父节点或者子节点
- C#通用接口调用
- Add automatic model generation function to hade
- Spark on yarn资源优化思路笔记
猜你喜欢

Kubernetes family container housekeeper pod online Q & A?

MySql实战45讲【事务隔离】

Check log4j problems using stain analysis
![MySQL Real combat 45 [SQL query and Update Execution Process]](/img/cd/3a635f0c3bb4ac3c8241cb77285cc8.png)
MySQL Real combat 45 [SQL query and Update Execution Process]

Kubernetes cluster log and efk architecture log scheme

Docker install MySQL

Do you really understand relays?

Segmentation fault occurs during VFORK execution

Practice of traffic recording and playback in vivo

力扣------网格中的最小路径代价
随机推荐
Source code analysis | resource loading resources
[fluent] futurebuilder asynchronous programming (futurebuilder construction method | asyncsnapshot asynchronous calculation)
How do you adjust the scope of activerecord Association in rails 3- How do you scope ActiveRecord associations in Rails 3?
Two dimensional format array format index subscript continuity problem leads to return JSON format problem
MySql实战45讲【索引】
Add automatic model generation function to hade
leetcode540
yii2 中andWhere多个or查询 orm条件
How to limit the size of the dictionary- How to limit the size of a dictionary?
sql server数据库添加 mdf数据库文件,遇到的报错
Practice of traffic recording and playback in vivo
Reset or clear NET MemoryStream - Reset or Clear . NET MemoryStream
疫情当头,作为Leader如何进行代码版本和需求开发管控?| 社区征文
Force deduction ----- the minimum path cost in the grid
The core idea of performance optimization, dry goods sharing
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 left value and right value in C language
I2C 子系统(一):I2C spec
SQL Server Query spécifie la structure de la table
What does it mean when lambda is not entered?