当前位置:网站首页>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)
边栏推荐
- MySQL practice 45 lecture [transaction isolation]
- What happens between entering the URL and displaying the page?
- open file in 'w' mode: IOError: [Errno 2] No such file or directory
- Vs Code configure virtual environment
- Vs 2019 configure tensorrt to generate engine
- [combinatorics] Application of exponential generating function (multiple set arrangement problem | different balls in different boxes | derivation of exponential generating function of odd / even sequ
- Vs 2019 configuration du moteur de génération de tensorrt
- Model transformation onnx2engine
- VS 2019 配置tensorRT生成engine
- Super easy to use logzero
猜你喜欢
Le processus de connexion mysql avec docker
Creation and destruction of function stack frame
The idea cannot be loaded, and the market solution can be applied (pro test)
[principles of multithreading and high concurrency: 1_cpu multi-level cache model]
VS 2019安装及配置opencv
你真的懂继电器吗?
用docker 連接mysql的過程
MySql实战45讲【全局锁和表锁】
Use of El tree search method
I2C subsystem (IV): I2C debug
随机推荐
VS 2019配置tensorRT
Add some hard dishes to the interview: how to improve throughput and timeliness in delayed task scenarios!
迅雷chrome扩展插件造成服务器返回的数据js解析页面数据异常
Getting started | jetpack hilt dependency injection framework
从C到Capable-----利用指针作为函数参数求字符串是否为回文字符
TCP 三次握手和四次挥手机制,TCP为什么要三次握手和四次挥手,TCP 连接建立失败处理机制
Sqlserver row to column pivot
Use optimization | points that can be optimized in recyclerview
[C language] MD5 encryption for account password
Creation and destruction of function stack frame
MySQL practice 45 [global lock and table lock]
Serious security vulnerabilities reported by moxa mxview network management software
BigVision代码
MySql实战45讲【事务隔离】
45 lectures on MySQL [index]
Anhui University | small target tracking: large-scale data sets and baselines
I2C subsystem (III): I2C driver
Update and return document in mongodb - update and return document in mongodb
ComponentScan和ComponentScans的区别
Variable declarations following if statements