import os
import numpy
import cv2

bad_label_file = open("bad_valid.list",'r')

names = []
for line in open('data/coco.names','r'):
    names.append(line.strip('\n'))
classes = len(names)

colors = []
for i in range(80):
    #color = [random.uniform(0, 255),random.uniform(0, 255),random.uniform(0, 255)]
    color = [(i)*255/classes,(classes-i)*255/classes,(classes+i)*128/classes]
    colors.append(color)


lines = []
for line in bad_label_file:
    lines.append(line)

mount = len(lines)

oper=0
i=0

#for i in range(mount):
while(i<mount):
    i+=oper
    
    words = lines[i].split(':')
    imagepath = words[0]
    #print('reading "{}: No. {}"'.format(path,indexes))
    print('{}\\{}'.format(mount,i))
    
    src = cv2.imread(imagepath)
    #cv2.imshow("src",src)
    h,w = src.shape[:2]


    labelpath = imagepath.split('-')[0]+'-labels/'+os.path.basename(imagepath).split('.')[0]+'.txt'
    try:
        labelfile = open(labelpath,'r')
        labels = []
        for line in labelfile:
            #print('get label {}'.format(line))
            labels.append(line)
    
        for label in labels:
            ti,tx,ty,tw,th = map(float,label.split(' ')[:5])
            #print('get label {} {} {} {} {}'.format(ti,tx,ty,tw,th))
            x0=w*(tx-tw/2)
            y0=h*(ty-th/2)
            x1=w*(tx+tw/2)
            y1=h*(ty+th/2)
            w0=w*tw
            h0=h*th
            ti,x0,y0,x1,y1,w0,h0 = map(int,[ti,x0,y0,x1,y1,w0,h0])
            #print('<({},{}),({},{})>,'.format(x0,y0,x1,y1))
            cv2.rectangle(src,(x0,y0),(x1,y1),colors[ti],2)
            y0+=15
            #cv2.putText(src,str(ti)+' '+names[ti],(x0+1,y0+1),cv2.FONT_HERSHEY_COMPLEX,0.5,colors[ti][::-1],1)
            #cv2.putText(src,str(ti)+' '+names[ti],(x0-1,y0-1),cv2.FONT_HERSHEY_COMPLEX,0.5,colors[ti][::-1],1)
            cv2.putText(src,str(ti)+' '+names[ti],(x0,y0),cv2.FONT_HERSHEY_COMPLEX,0.5,colors[ti],1)
    except :
        print('guess what? ERROR!(show_bad_valid.py::67)')

    
    words = words[1].strip('\n')
    probs = words.split(' ')[2:]
    #print(words)
    #print(probs)
    if len(probs) > 0:
        for prob in probs:
            values = prob.strip('<').strip('>').split(',')
            print(values)
            ti,tx,ty,tw,th = map(float,values[2:])
            x0=w*(tx-tw/2)
            y0=h*(ty-th/2)
            x1=w*(tx+tw/2)
            y1=h*(ty+th/2)
            ti,x0,y0,x1,y1 = map(int,[ti,x0,y0,x1,y1])
            y1-=10
            cv2.rectangle(src,(x0,y0),(x1,y1),colors[ti][::-1],1)
            cv2.putText(src,str(ti)+' '+names[ti],(x0,y1),cv2.FONT_HERSHEY_COMPLEX,0.5,colors[ti],1)


    cv2.imshow("image",src)

    key = cv2.waitKey(0)
    '''
    A 65
    a 97
    D 68
    d 100
    S 83
    s 115
    '''
    if key == 97:
        if i>0:
            oper=-1
    elif key == 100:
        if i<len(lines):
            oper=1
    elif key == 115:
        oper=0
        cv2.imwrite('workingspace.jpg',src)
    else:
        break
bad_label_file.close()

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。