yolov3 调试(5):valid 识别出的内容在原图进行标注

import cv2
import os

def draw_box(original_photo_dir, recognize_txt_dir):
    '''
    original_photo_dir 保存待标注原图的目录
    recognize_txt_dir  保存从 valid 结果处理得到的 标注位置信息,目录下一个 txt 文件对应一张图片,对应 txt 和图片同名
    '''

    recognize_txt_list = os.listdir(recognize_txt_dir) 

    for recognize_txt in recognize_txt_list:
        recognize_txt_path = os.path.join(recognize_txt_dir, recognize_txt)

        orignial_photo_name = recognize_txt[:-3] + 'jpg'
        original_photo_path = os.path.join(original_photo_dir, orignial_photo_name)

        box_info = open(recognize_txt_path, 'r').readlines()

        image = cv2.imread(original_photo_path)

        for info in box_info: 
            info = info.split()

            number = int(info[0])
            x1 = round(float(info[1]))
            y1 = round(float(info[2]))
            x2 = round(float(info[3]))
            y2 = round(float(info[4]))

            cv2.rectangle(image,(x1, y1),(x2, y2),(0,255,0),3)  
            # (图片,矩形框左上、右下坐标,矩形框颜色, 矩形框轮廓粗细)    

            cv2.putText(image, str(number), (x2, y1), cv2.FONT_HERSHEY_COMPLEX_SMALL,1.8, (0, 255, 0) ) 
            # (图片,标注文本,显示位置,字体,字体大小,字体颜色),这里,我们让标注文本显示在矩形框的右上角外侧
    return None

你可能感兴趣的:(CV)