DOTA数据集 | 测试结果画回原图

将DOTA测试结果画回原图中

PS:一定一定要输入正确的路径,还有明白数据集的格式,到底是 (x,y,w,h) 还是 (x1,y1,x2,y2)!!!!

STEP1: 将json格式转为txt格式

(法一)

import json

f = open("../output/vgg16/coco_2014_minival/default/vgg16_faster_rcnn_iter_190000/detections_minival2014_results_06295b33-1f2d-40a3-9b6e-5974f6eaeddd.json", 'r')
arr = json.loads(f.read())
f.close()

f = open("../output/vgg16/coco_2014_minival/default/vgg16_faster_rcnn_iter_190000/coco2014-relation.txt", 'w')
for i in arr:
    f.write(
        "%08d %d %f %f %f %f %f\n" % (
        i['image_id'], i['category_id'], i['score'],
        i['bbox'][0], i['bbox'][1], i['bbox'][2] + i['bbox'][0], i['bbox'][3] + i['bbox'][1]))
f.close()

(法二)

# -*- coding: UTF-8 -*-
import json
# json文件转成txt文件
f = open(r"C:\Users\xxx\Desktop\detections_test2017__results_yolov5l.json", 'r', encoding='utf-8')

# papers = []
# for line in f.readlines():
#     dic = json.loads(line)
#     papers.append(dic)

arr = json.loads(f.read(), strict=False)
# print(80*'=')
# print(arr)
# print(80*'=')
f.close()

file_ = open(r"C:\Users\xxx\Desktop\score_pre.txt", 'w')
# x1, y1, x2, y2
# for i in arr:
#     f.write(
#         "%08d %d %f %f %f %f %f\n" % (
#         i['image_id'], i['category_id'], i['score'],
#         i['bbox'][0], i['bbox'][1], i['bbox'][2] + i['bbox'][0], i['bbox'][3] + i['bbox'][1]))

for i in arr:
    k = i['image_id']
    file_.write(
        "%08d %d %f %f %f %f %f\n" % (
        i['image_id'], i['category_id'], i['score'],
        i['bbox'][0], i['bbox'][1], i['bbox'][2], i['bbox'][3]))

# 下面的代码是获取gt中id,category,bbox
# for i in arr['annotations']:  
#     k = i['image_id']
#     file_.write(
#         "%08d %d %f %f %f %f\n" % (
#         i['image_id'],  i['category_id'],
#         i['bbox'][0], i['bbox'][1], i['bbox'][2], i['bbox'][3]))

# 下面的代码是获取预测文件中的id,category,bbox
# for i in arr:  
#     k = i['image_id']
#     file_.write(
#         "%08d %d %f %f %f %f\n" % (
#         i['image_id'],  i['category_id'],
#         i['bbox'][0], i['bbox'][1], i['bbox'][2], i['bbox'][3]))

file_.close()

STEP2:使用txt格式画回原图中

# -*- coding: UTF-8 -*-
'''
将测试生成的txt文件,把文件中对应的box的坐标画回原图
'''
import cv2
import numpy as py
import os

# def drawBBox(txt_path, img_path, save_path):
#     global img_id
#     img_id = "00101785"  #换成第一张图片id
#     with open(txt_path,'r')as fp:
#         while(1):
#             line = fp.readline()
#             if not line:
#                 print("txt is over!!!")
#                 break
#             str = line.split(" ")
#             x = round(float(str[2]))
#             y = round(float(str[3]))
#             w = round(float(str[4])) + x
#             h = round(float(str[5])) + y
#             # ap = round(float(str[2]), 5)
#             # print(ap)
#             if ap >= 0.5:
#                 if str[0] != img_id or img_id == "00101785":
#                     img = cv2.imread(img_path + str[0] + ".png")
#                 else:
#                 # 换成你自己的类别
#                     img = cv2.imread(save_path + str[0] + ".png")
#                 if str[1] == '0':
#                     cv2.rectangle(img,(x,y-22),(x+50,y),(0,255,0), thickness = -1)
#                     cv2.putText(img, "plane", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(0,255,0),3,4,0)
                
#                 elif str[1] == '1':
#                     cv2.rectangle(img,(x,y-22),(x+100,y),(0,0,255), thickness = -1)
#                     cv2.putText(img, "ship", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(0,0,255),3,4,0)

#                 elif str[1] == '2':
#                     cv2.rectangle(img,(x,y-22),(x+40,y),(172,172,0), thickness = -1)             
#                     cv2.putText(img, "stroage-tank", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(172,172,0),3,4,0)
                
#                 elif str[1] == '3':
#                     cv2.rectangle(img,(x,y-22),(x+35,y),(172,0,172), thickness = -1)                  
#                     cv2.putText(img, "baseball-diamond", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(172,0,172),3,4,0)
                
#                 elif str[1] == '4':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "tennis-court", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '5':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "basketball-court", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '6':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "ground-track-field", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
                
#                 elif str[1] == '7':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "harbor", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '8':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "bridge", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
                
#                 elif str[1] == '9':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "small-vehicle", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '10':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "large-vehicle", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '11':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "helicopter", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '12':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "roundabout", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

#                 elif str[1] == '13':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "soccer-ball-field", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
                
#                 elif str[1] == '14':
#                     cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
#                     cv2.putText(img, "swimming-pool", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
#                     cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
#                 # cv2.imwrite(img_path+str[0]+".jpg",img)

#                 img_id = str[0]
#                 cv2.imwrite(save_path + img_id+".png", img)
#                 print(str[0]+".png is save....OK!!!")

# gt drawbox
def drawBBox(txt_path, img_path, save_path):
    global img_id
    img_id = "00101785"  #换成第一张图片id
    with open(txt_path,'r')as fp:
        while(1):
            line = fp.readline()
            if not line:
                print("txt is over!!!")
                break
            str = line.split(" ")
            x = round(float(str[2]))
            y = round(float(str[3]))
            w = round(float(str[4])) + x
            h = round(float(str[5])) + y
            if str[0] != img_id or img_id == "00101785":
                img = cv2.imread(img_path + str[0] + ".png")
            else:
            # 换成你自己的类别
                img = cv2.imread(save_path + str[0] + ".png")
            if str[1] == '0':
                cv2.rectangle(img,(x,y-22),(x+50,y),(0,255,0), thickness = -1)
                cv2.putText(img, "plane", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(0,255,0),3,4,0)
            
            elif str[1] == '1':
                cv2.rectangle(img,(x,y-22),(x+100,y),(0,0,255), thickness = -1)
                cv2.putText(img, "ship", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(0,0,255),3,4,0)

            elif str[1] == '2':
                cv2.rectangle(img,(x,y-22),(x+40,y),(172,172,0), thickness = -1)             
                cv2.putText(img, "stroage-tank", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(172,172,0),3,4,0)
            
            elif str[1] == '3':
                cv2.rectangle(img,(x,y-22),(x+35,y),(172,0,172), thickness = -1)                  
                cv2.putText(img, "baseball-diamond", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(172,0,172),3,4,0)
            
            elif str[1] == '4':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "tennis-court", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '5':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "basketball-court", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '6':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "ground-track-field", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
            
            elif str[1] == '7':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "harbor", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '8':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "bridge", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
            
            elif str[1] == '9':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "small-vehicle", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '10':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "large-vehicle", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '11':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "helicopter", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '12':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "roundabout", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)

            elif str[1] == '13':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "soccer-ball-field", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
            
            elif str[1] == '14':
                cv2.rectangle(img,(x,y-22),(x+90,y),(255,0,255), thickness = -1)               
                cv2.putText(img, "swimming-pool", (x,y-5), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0,0,0))
                cv2.rectangle(img,(x,y),(w,h),(255,0,255),3,4,0)
            # cv2.imwrite(img_path+str[0]+".jpg",img)

            img_id = str[0]
            # import pdb
            # pdb.set_trace()
            cv2.imwrite(save_path + img_id+".png", img)
            print(str[0]+".png is save....OK!!!")

if __name__ == '__main__':
    # txt存放的路径
    txt_path="/home/xxx/code/yolov5/output/gt_dota.txt"
    # 原图片路径
    img_path="/images/val/"
    # 画出来的图片保存的路径
    save_path="/home/xxx/code/yolov5/output/gt_output/"
    drawBBox(txt_path, img_path, save_path)
    print("All Done....")

遇到的bug

1. TypeError: list indices must be integers or slices, not str(类型错误:列表索引必须是整数或切片,而不是str)

json格式:
JSON可以有两种格式,一种是对象格式的,另一种是数组对象。

{“name”:“JSON”,“address”:“北京市西城区”,“age”:25}//JSON的对象格式的字符串

[{“name”:“JSON”,“address”:“北京市西城区”,“age”:25}]//数据对象格式

下图是数组对象格式。
在这里插入图片描述
JSON数组的读取:https://www.cnblogs.com/cisum/p/8004141.html

修改位置:改为获取数组就ok了。

for i in arr:  

在这里插入图片描述

2. libpng warning: Image width is zero in IHDR libpng warning: Image height is zero in IHDR libpng error: Invalid IHDR data

报错原因可能是以下几个:

  1. 图片路径中含有中文字符;
  2. 文件没读取到,读取的图片路径或者路径格式写的是不是有问题,可以直接将路径改成绝对路径;
  3. 使用的路径有空的,仔细检查一遍有没有在代码中使用到了空路径。

笔者根据上面的原因进行排查,首先图片路径没有中文,其次图片路径和保存路径都已经保存为绝对路径。查到最后发现是第三种原因。

代码中获取的图片名字和json文件中id对应不上,修改两处代码为:
这样做得目的是获取正确的图片名字!!
在这里插入图片描述
在这里插入图片描述

总结:需要熟悉代码,了解代码的语法,才能更好地修改bug。

  • 5
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值