Faster RCNN Tensorflow在测试得到result.txt文件后将标签标注在原图上

需要准备三个路径:

1、一个是进行测试时所使用的那些图片,找到其路径(cv2的路径中不能含有中文)

2、result.txt所在的路径

3、生成图像的存放路径

说明:我的faster rcnn检测任务中有三个待检测目标,分别是:figures\formulas\table,得到的result.txt文件是这样的:

POD_2176.bmp figures 0.99998903 571 108 978 380
POD_2176.bmp figures 0.9999856 96 111 505 381
POD_2176.bmp figures 0.9999801 122 485 468 817
POD_2177.bmp table 0.9998165 168 256 858 441
POD_2178.bmp figures 0.99880683 481 913 593 1021
POD_2178.bmp figures 0.99713045 493 614 590 731
POD_2178.bmp formulas 0.9938018 334 1175 744 1222
POD_2178.bmp formulas 0.9694136 187 231 433 290
POD_2178.bmp formulas 0.92181665 195 463 896 486

因此,先将归属于同一张图片的labels找出来,存放在list中,然后用这个list在原图上进行绘制

话不多说,直接上代码:

#coding=utf-8
import cv2

def addLabelsToImage(imageList):
    imgName = imageList[0].split(' ')[0]
    img = cv2.imread('D:/images/' + imgName, 1) #加载出这张图片
    for image in imageList:
        file=image.split(' ')
        if file[1]=="figures":
            img=cv2.rectangle(img, (int(file[3]), int(file[4])), (int(file[5]), int(file[6])), (0, 0, 255), 3)
        if file[1]=="formulas":
            img=cv2.rectangle(img, (int(file[3]), int(file[4])), (int(file[5]), int(file[6])), (0, 255, 0), 3)
        if file[1]=="table":
            img=cv2.rectangle(img, (int(file[3]), int(file[4])), (int(file[5]), int(file[6])), (255, 0, 0), 3)

    cv2.imwrite('D:/afterImage/' + imageList[0].split(' ')[0]+ '.png', img, [cv2.IMWRITE_PNG_COMPRESSION, 0])
    pass

with open('D:/result.txt', 'r') as f: 
    lines = f.readlines()  
imagenames = [x.strip() for x in lines]  


finishedDeal=[]
for index,imagename in enumerate(imagenames):
    file=imagename.split(' ')
    if file[0] in finishedDeal:
        continue   
    else:
        print(file[0])
        finishedDeal.append(file[0])    
    fileList=[]
    
    for i in range(index,len(imagenames)):
        file2=imagenames[i].split(' ')
        if file2[0]==file[0]: 
            fileList.append(imagenames[i])
        else:
            break   

    print("这里是第 %d 次做列表" % index)
    for i in fileList:
        print(i)
    print("第 %d 次列表 完毕" % index)

    addLabelsToImage(fileList)


发布了13 篇原创文章 · 获赞 0 · 访问量 754

猜你喜欢

转载自blog.csdn.net/zxs0222/article/details/89605300