将lable的值画到img上

将label的值画到img上

1 首先用process_server.py 将标注好的图片images\ (jpg+json) 生成 lable\ test.txt, val.txt, train.txt 文件 – lable\ 与 images\ 文件夹结构是相同的

2 是用该脚本 将lable 中的框 画到 每一张图片上 一张图片有多少框就画多少框;

3最后该脚本会生将结果生成到一个新文件夹里,就是画好框的图片

#!/usr/bin/python3.6.10
# -*- coding: utf-8 -*-
# @Author  : C
# @Time    : 2021/12/1 11:22
# @File    : 将label的值画到img上.py
import numpy as np
import cv2
import time
from pathlib import Path
import glob

#
# def test():
#     img = cv2.imread(r"./image/20151223182909577.jpg")
#     print(img.shape)  # 图片大小
#     cv2.rectangle(img, (240, 0), (480, 375), (0, 255, 255), 2)
#     label = ['i,am']
#     box = [100, 200, 300, 400, 0.8]
#     lab = f"{label[0]}: {box[-1]:.2f}" + '_' + \
#           time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()).split(' ')[-1]
#     cv2.putText(img, lab, (240, 10,), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 123), 2)
#
#     # cv2.imshow("fff", img)
#     #
#     # cv2.waitKey(0)
#     cv2.imwrite('xxx.jpg', img)


def make_frame(img, box1, box2, label_CHS="视线与温度计持平", conf=0.888):
    """
    用于将两个框 框在一起 给一个标签
    :param img: 待画的图
    :param box1: 待画的框1
    :param box2: 待画的框2
    :param label_CHS: 标签-中文
    :param conf: 置信度
    :return:
    """
    x_min = min(box1[0], box1[2], box2[0], box2[2])
    y_min = min(box1[1], box1[3], box2[1], box2[3])
    x_max = max(box1[0], box1[2], box2[0], box2[2])
    y_max = max(box1[1], box1[3], box2[1], box2[3])

    x1, y1, x2, y2 = int(x_min), int(y_min), int(x_max), int(y_max)
    color = [0, 0, 255]  # 255,0,0  # [B,G,R]  红色
    cv2.rectangle(img, (x1, y1), (x2, y2), color, 2, cv2.LINE_AA)  # 青色框框出所需范围,
    text = f'{"  " * len(label_CHS)} {conf:.2f}'
    ###
    from PIL import Image, ImageDraw, ImageFont
    char_l = (len(label_CHS) * 2 + 7) * 9  ## length of character
    bg_ch = np.array([0, 0, 255], dtype=np.uint8)  # [B,G,R] 蓝色
    bg_ch = np.broadcast_to(bg_ch, (18, char_l, 3))  # 广播机制
    pil_bg = Image.fromarray(bg_ch)
    draw = ImageDraw.Draw(pil_bg)  # 设置背景区域大小(18, char_l, 3),背景颜色为[0,0,255]
    fontStyle = ImageFont.truetype("../font/simhei.ttf", 18)  # 设置字体
    draw.text((5, 1), label_CHS, (255, 255, 255), font=fontStyle)  # 设置字,字体颜色 为白色 (5,0)-字体在背景的显示位置
    np_bg = np.asarray(pil_bg)
    h, w, _ = img.shape
    y, x, _ = np_bg.shape  # y,x 为写中文字的背景 高宽
    px, py = x1, y1  # px,py为需要画框的左上角的点
    # px, py 重新赋值 解决框的背景解释超出图片的范围 问题
    if w - px < x:
        px = w - x
    if py < y:
        py = y
    img[py - y:py, px:px + x] = np_bg  ## Chinese characters background
    cv2.putText(img, text, (px, py - 3), 0, 0.6, [225, 255, 255], thickness=1,
                lineType=cv2.LINE_AA)


def main():
    # map_da_label =  ["hand", "head", "ruler", "stopwatch", "hand_ruler_object", "hand_stopwatch", "flag1", "flag2", "flag3", "flag4"]
    map_da = ["手", "头", "尺子", "停表", "手_尺子_物体", "手_停表", ]
    images_pa = r"C:\Users\caibaojun\Desktop\20220215\20211102105218\images\front1\*.jpg"
    lables_pa = r"C:\Users\caibaojun\Desktop\20220215\20211102105218\labels\front1\*.txt"  # -- txt 里面是中心点,宽高
    images_pa = glob.glob(images_pa)
    lables_pa = glob.glob(lables_pa)

    for index, img_pa in enumerate(images_pa):
        img_path = Path(img_pa)
        name = img_path.name
        img = cv2.imread(img_path.as_posix())
        # print(img.shape)
        lab_li = []
        with open(lables_pa[index], 'r') as f:
            while True:
                res = f.readline()
                da = res.split()
                if da:
                    lab_li.append(da)
                if not res:
                    break
        for l in lab_li:
            index = int(l[0])
            label = map_da[index]
            x,y, w,h = float(l[1:][0])*1920, float(l[1:][1])*1080, float(l[1:][2])*1920, float(l[1:][3])*1080
            x1, y1, x2, y2 = x-w/2 if x-w/2>0 else 0 , y-h/2 if y-h/2>0 else 0, x+w/2 if x+w/2 else 0, y+h/2 if y+h/2 else 0
            # for i in (x1, y1, x2, y2):
            #     if i <0:
            #         i=0
            print((x1, y1, x2, y2))
            print(img.shape)
            make_frame(img=img,box1=[x1, y1, x2, y2], box2=[x1, y1, x2, y2],label_CHS=label)
        cv2.imwrite(r'C:\Users\caibaojun\Desktop\20220215\20211102105218\res/'+str(name), img)
    pass


if __name__ == '__main__':
    main()


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值