深度学习目标检测常用工具型代码:检测结果可视化(darkent形式)

# -*- coding: utf-8 -*-
"""
    作用是将检测出的类结果(如VOC是20类,也就是20个txt)画在原图上,并另存到一个路径下
    srcpath表示txt的路径 
    dstpath表示原始图片的路径
    
    txt格式如(文末有截图):  
    Task2_baseball-diamond.txt
    P0034 0.840593 1593.0 1443.0 1778.0 1600.0  
    
    
"""
import os
import cv2

#像这种常用的函数可以作为参考
def custombasename(fullname):
    return os.path.basename(os.path.splitext(fullname)[0])

def GetFileFromThisRootDir(dir,ext = None):
  allfiles = []
  needExtFilter = (ext != None)
  for root,dirs,files in os.walk(dir):
    for filespath in files:
      filepath = os.path.join(root, filespath)
      extension = os.path.splitext(filepath)[1][1:]
      if needExtFilter and extension in ext:
        allfiles.append(filepath)
      elif not needExtFilter:
        allfiles.append(filepath)
  return allfiles
  
def draw_bbox_by_det_result(srcpath, dstpath):
    filelist = GetFileFromThisRootDir(srcpath)
    for fullname in filelist:
        classname = custombasename(fullname)
        classname = classname[6:]#这个地方需要自己改,懒得再改了。
        with open(fullname, 'r') as f_in:
            nameboxdict = {}
            lines = f_in.readlines()
            splitlines = [x.strip().split(' ') for x in lines]
            for splitline in splitlines:
                 oriname = splitline[0]
                 confidence = splitline[1]
                 poly = list(map(float, splitline[2:]))
                 det = poly
                 det.append(confidence)
                 det = list(map(float, det))
                 if (oriname not in nameboxdict):
                     nameboxdict[oriname] = []
                 nameboxdict[oriname].append(det)
                #字典的for in是根据key做的
            for imgname in nameboxdict:
                im_file = os.path.join(dstpath+'/'+imgname+'.png')
                im = cv2.imread(im_file)
                for det in nameboxdict[imgname]:
                        #print('det:', det)
                    confidence = det[-1]
                    bbox = det[0:-1]
                    cv2.rectangle(im,(int(bbox[0]),int(bbox[1])),(int(bbox[2]),int(bbox[3])),(255,255,0),2)
                    cv2.putText(im, '{:s} {:.3f}'.format(classname,confidence), (int(bbox[0]), int(bbox[1] - 5)), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0,0,255), 2)
                #这里的路径表示保存图片的路径 下面有效果展示
                cv2.imwrite(os.path.join('/media/ygx/Elements/VOCdevkit2007/testsplit1.5/1','%s_%s.txt.png' % (imgname,classname)),im)

if __name__ == '__main__':
    draw_bbox_by_det_result(r'/media/ygx/参赛/最后提交的zip文件/small-vehicle最终所用/nms/nms0.3', r'/media/ygx/Elements/DOTA比赛原始下载数据/test')


darknet的可视化

target = ('r','g','y','r_l','r_r','y_l','y_r',
'g_l','g_r','r_f','g_f','y_f','h_r','h_g',
'h_y','off','sign','car','moto',
'bik','bus','truck','suv','exp','m')
id_class = {}
for i in range(25):
    id_class[i+1] = target[i]
id_class[23]
# darknet 可视化
import colorsys
import os
from PIL import Image, ImageFont, ImageDraw

hsv_tuples = [(x / 26, 1., 1.) for x in range(26)]
colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
colors = list(map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),colors))


def box_visualize(img_path, boxes ,show = False ,save_path = './visualize/'):
    # save_path放存储的文件夹路径
    #'/home/zj/mmdetection-swtx/data/VOCdevkit/VOC2007/JPEGImages/000000.jpg'
    basename = os.path.basename(img_path)
    save_path = os.path.join(save_path,basename)
    image = Image.open(img_path)
    font = ImageFont.truetype(font='DejaVu Sans Mono.ttf',
                        size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
    
    draw = ImageDraw.Draw(image)
    for box in boxes:
        if len(box) == 6:
            left, top, right , bottom, predicted_class,score = box
            label = '{} {:.2f}'.format(id_class[predicted_class], score)
        else:
            left, top, right , bottom, predicted_class = box
            label = '{}'.format(id_class[predicted_class])

        thickness = (image.size[0] + image.size[1]) // 1000 # 300

        draw = ImageDraw.Draw(image)
        label_size = draw.textsize(label, font)

        top = max(0, np.floor(top + 0.5).astype('int32'))
        left = max(0, np.floor(left + 0.5).astype('int32'))
        bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
        right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
        #print(label, (left, top), (right, bottom))

        if top - label_size[1] >= 0:
            text_origin = np.array([left, top - label_size[1]])
        else:
            text_origin = np.array([left, top + 1])

        # My kingdom for a good redistributable image drawing library.
        for i in range(thickness):
            draw.rectangle(
                [left + i, top + i, right - i, bottom - i],
                outline=colors[predicted_class])
        draw.rectangle(
            [tuple(text_origin), tuple(text_origin + label_size)],
            fill=colors[predicted_class])
        draw.text(text_origin, label, fill=(0, 0, 0), font=font)
    if show:
        image.show()
    else:
        image.save(save_path)

 

boxes = [[255,356,295,387,2,1]]
box_visualize('/home/zj/mmdetection-swtx/data/VOCdevkit/VOC2007/JPEGImages/000000.jpg', boxes)

f = open('trainval.txt','r')
lines = f.readlines()

try:
    with tqdm(lines) as t:
        for line in t:
            bboxes = []
            line = line.strip('\n').split(' ')
            img_path = os.path.join(root,'JPEGImages',line[0])
            for i in range(len(line)-2):
                #bboxes.append([int(i) for i in line[i+2].split(',')[:5]])  
                box = [int(i) for i in line[i+2].split(',')[:5]]
                bboxes.append(box)  

            box_visualize(img_path , bboxes, save_path = './visualize/')
except KeyboardInterrupt:
    t.close()
    raise
t.close()  
    
f.close()

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值