赞
踩
原因:可能在标注数据时,使用了图像翻转或反转批量生成新数据和新Json文件,这时再进行Json文件转txt文件时由于转换代码的原因会把转换后原本先标注的坐标点当成后标注的像素点,这时再进行矩形框长宽计算时,可能会产生负值。
原转化代码,我参考了这篇文章:https://blog.csdn.net/fjlaym/article/details/123992962?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522168722415116800180654496%2522%252C%2522scm%2522%253A%252220140713.130102334..%2522%257D&request_id=168722415116800180654496&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~all~top_positive~default-1-123992962-null-null.142%5Ev88%5Econtrol,239%5Ev2%5Einsert_chatgpt&utm_term=yolov5%E5%88%B6%E4%BD%9C%E8%87%AA%E5%B7%B1%E7%9A%84%E6%95%B0%E6%8D%AE%E9%9B%86&spm=1018.2226.3001.4187
解决:将转换结果用abs函数确保转换成正值。在原代码的基础上对convert函数做如下更改:
```python
def convert(img_size, box):
dw = 1. / (img_size[0])
dh = 1. / (img_size[1])
x = (box[0] + box[2]) / 2.0 - 1
y = (box[1] + box[3]) / 2.0 - 1
w = box[2] - box[0]
h = box[3] - box[1]
x = abs(x * dw)
w = abs(w * dw)
y = abs(y * dh)
h = abs(h * dh)
return (x, y, w, h)
```
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。