|
-
- import cv2
- import numpy as np
- from PIL import Image, ImageDraw, ImageFont
-
-
- def get_label_array(color=None, label=None, font=None, fontSize=None):
- x, y, width, height = font.getbbox(label)
- text_image = np.zeros((height, width, 3), dtype=np.uint8)
- text_image = Image.fromarray(text_image)
- draw = ImageDraw.Draw(text_image)
- draw.rectangle((0, 0, width, height), fill=tuple(color))
- draw.text((0, -3), label, fill=(255, 255, 255), font=font)
- im_array = np.asarray(text_image)
- scale = fontSize / height
- im_array = cv2.resize(im_array, (0, 0), fx=scale, fy=scale)
- return im_array
-
-
- def get_label_arrays(labelNames, colors, fontSize=40, fontPath="platech.ttf"):
- font = ImageFont.truetype(fontPath, fontSize, encoding='utf-8')
- label_arraylist = []
- for i, label_name in enumerate(labelNames):
- color = colors[i % 20]
- label_arraylist.append(get_label_array(color, label_name, font, fontSize))
- return label_arraylist
-
-
- def draw_painting_joint(box, img, label_array, score=0.5, color=None, score_location="leftTop"):
- # 识别问题描述图片的高、宽
- lh, lw = label_array.shape[0:2]
- # 图片的长度和宽度
- imh, imw = img.shape[0:2]
- if not isinstance(box[0], (list, tuple, np.ndarray)):
- xc = int(box[0])
- yc = int(box[1])
- w = int(box[2])
- h = int(box[3])
- bw = int(w/2)
- bh = int(h/2)
- tl = (xc - bw, yc - bh)
- tr = (xc + bw, yc - bh)
- br = (xc + bw, yc + bh)
- bl = (xc - bw, yc + bh)
- box = [tl, tr, br, bl]
- # 框框左上的位置
- if score_location == 'leftTop':
- x0, y1 = box[0][0], box[0][1]
- # 框框左下的位置
- elif score_location == 'leftBottom':
- x0, y1 = box[3][0], box[3][1]
- else:
- x0, y1 = box[0][0], box[0][1]
- # x1 框框左上x位置 + 描述的宽
- # y0 框框左上y位置 - 描述的高
- x1, y0 = x0 + lw, y1 - lh
- # 如果y0小于0, 说明超过上边框
- if y0 < 0:
- y0 = 0
- # y1等于文字高度
- y1 = y0 + lh
- # 如果y1框框的高大于图片高度
- if y1 > imh:
- # y1等于图片高度
- y1 = imh
- # y0等于y1减去文字高度
- y0 = y1 - lh
- # 如果x0小于0
- if x0 < 0:
- x0 = 0
- x1 = x0 + lw
- if x1 > imw:
- x1 = imw
- x0 = x1 - lw
- img[y0:y1, x0:x1, :] = label_array
- pts_cls = [(x0, y0), (x1, y1)]
-
- # 把四边形的框画上
- # box_tl = font['boxLine_thickness'] or round(0.002 * (imh + imw) / 2) + 1
- box_tl = max(int(round(imw / 1920 * 3)), 1) or round(0.002 * (imh + imw) / 2) + 1
- '''
- 1. img(array) 为ndarray类型(可以为cv.imread)直接读取的数据
- 2. box(array):为所画多边形的顶点坐标
- 3. 所画四边形是否闭合,通常为True
- 4. color(tuple):BGR三个通道的值
- 5. thickness(int):画线的粗细
- 6. shift:顶点坐标中小数的位数
- '''
- box1 = np.asarray(box, np.int32)
- cv2.polylines(img, [box1], True, color, box_tl)
-
- # 把英文字符score画到类别旁边
- tl = max(int(round(imw / 1920 * 3)), 1) or round(0.002 * (imh + imw) / 2) + 1
- label = ' %.2f' % score
- tf = max(tl, 1)
- fontScale = float(format(imw / 1920 * 1.1, '.2f')) or tl * 0.33
- '''
- 1. text:要计算大小的文本内容,类型为字符串。
- 2. fontFace:字体类型,例如cv2.FONT_HERSHEY_SIMPLEX等。
- 3. fontScale:字体大小的缩放因子,例如1.2表示字体大小增加20%。
- 4. thickness:文本线条的粗细,以像素为单位。
- 5. (text_width, text_height):给定文本在指定字体、字体大小、线条粗细下所占用的像素宽度和高度。
- '''
- t_size = cv2.getTextSize(label, 0, fontScale=fontScale, thickness=tf)[0]
- # if socre_location=='leftTop':
- p1, p2 = (pts_cls[1][0], pts_cls[0][1]), (pts_cls[1][0] + t_size[0], pts_cls[1][1])
- '''
- 1. img:要绘制矩形的图像
- 2. pt1:矩形框的左上角坐标,可以是一个包含两个整数的元组或列表,例如(x1, y1)或[x1, y1]。
- 3. pt2:矩形框的右下角坐标,可以是一个包含两个整数的元组或列表,例如(x2, y2)或[x2, y2]。
- 4. color:矩形框的颜色,可以是一个包含三个整数的元组或列表,例如(255, 0, 0)表示蓝色,或一个标量值,例如255表示白色。颜色顺序为BGR。
- 5. thickness:线条的粗细,以像素为单位。如果为负值,则表示要绘制填充矩形。默认值为1。
- 6. lineType:线条的类型,可以是cv2.LINE_AA表示抗锯齿线条,或cv2.LINE_4表示4连通线条,或cv2.LINE_8表示8连通线条。默认值为cv2.LINE_8。
- 7. shift:坐标点小数点位数。默认值为0。
- '''
- cv2.rectangle(img, p1, p2, color, -1, cv2.LINE_AA)
- p3 = pts_cls[1][0], pts_cls[1][1] - (lh - t_size[1]) // 2
- '''
- 1. img:要在其上绘制文本的图像
- 2. text:要绘制的文本内容,类型为字符串
- 3. org:文本起始位置的坐标,可以是一个包含两个整数的元组或列表,例如(x, y)或[x, y]。
- 4. fontFace:字体类型,例如cv2.FONT_HERSHEY_SIMPLEX等。
- 5. fontScale:字体大小的缩放因子,例如1.2表示字体大小增加20%。
- 6. color:文本的颜色,可以是一个包含三个整数的元组或列表,例如(255, 0, 0)表示蓝色,或一个标量值,例如255表示白色。颜色顺序为BGR。
- 7. thickness:文本线条的粗细,以像素为单位。默认值为1。
- 8. lineType:线条的类型,可以是cv2.LINE_AA表示抗锯齿线条,或cv2.LINE_4表示4连通线条,或cv2.LINE_8表示8连通线条。默认值为cv2.LINE_8。
- 9. bottomLeftOrigin:文本起始位置是否为左下角。如果为True,则文本起始位置为左下角,否则为左上角。默认值为False。
- '''
- cv2.putText(img, label, p3, 0, fontScale, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
- return img, box
-
-
-
-
- # def draw_painting_joint(box,img,label_array,score=0.5,color=None,font={ 'line_thickness':None,'boxLine_thickness':None, 'fontSize':None},socre_location="leftTop"):
- # #如果box[0]不是list or 元组,则box是[ (x0,y0),(x1,y1),(x2,y2),(x3,y3)]四点格式
- # if isinstance(box[0], (list, tuple,np.ndarray ) ):
- # ###先把中文类别字体赋值到img中
- # lh, lw, lc = label_array.shape
- # imh, imw, imc = img.shape
- # if socre_location=='leftTop':
- # x0 , y1 = box[0][0],box[0][1]
- # elif socre_location=='leftBottom':
- # x0,y1=box[3][0],box[3][1]
- # else:
- # print('plot.py line217 ,label_location:%s not implemented '%( socre_location ))
- # sys.exit(0)
- #
- # x1 , y0 = x0 + lw , y1 - lh
- # if y0<0:y0=0;y1=y0+lh
- # if y1>imh: y1=imh;y0=y1-lh
- # if x0<0:x0=0;x1=x0+lw
- # if x1>imw:x1=imw;x0=x1-lw
- # img[y0:y1,x0:x1,:] = label_array
- # pts_cls=[(x0,y0),(x1,y1) ]
- #
- # #把四边形的框画上
- # box_tl= font['boxLine_thickness'] or round(0.002 * (imh + imw) / 2) + 1
- # cv2.polylines(img, [box], True,color , box_tl)
- #
- # ####把英文字符score画到类别旁边
- # tl = font['line_thickness'] or round(0.002*(imh+imw)/2)+1#line/font thickness
- # label = ' %.2f'%(score)
- # tf = max(tl , 1) # font thickness
- # fontScale = font['fontSize'] or tl * 0.33
- # t_size = cv2.getTextSize(label, 0, fontScale=fontScale , thickness=tf)[0]
- #
- #
- # #if socre_location=='leftTop':
- # p1,p2= (pts_cls[1][0], pts_cls[0][1]),(pts_cls[1][0]+t_size[0],pts_cls[1][1])
- # cv2.rectangle(img, p1 , p2, color, -1, cv2.LINE_AA)
- # p3 = pts_cls[1][0],pts_cls[1][1]-(lh-t_size[1])//2
- #
- # cv2.putText(img, label,p3, 0, fontScale, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
- # return img
- # else:####两点格式[x0,y0,x1,y1]
- # try:
- # box = [int(xx.cpu()) for xx in box]
- # except:
- # box=[ int(x) for x in box]
- # ###先把中文类别字体赋值到img中
- # lh, lw, lc = label_array.shape
- # imh, imw, imc = img.shape
- # if socre_location=='leftTop':
- # x0 , y1 = box[0:2]
- # elif socre_location=='leftBottom':
- # x0,y1=box[0],box[3]
- # else:
- # print('plot.py line217 ,socre_location:%s not implemented '%( socre_location ))
- # sys.exit(0)
- # x1 , y0 = x0 + lw , y1 - lh
- # if y0<0:y0=0;y1=y0+lh
- # if y1>imh: y1=imh;y0=y1-lh
- # if x0<0:x0=0;x1=x0+lw
- # if x1>imw:x1=imw;x0=x1-lw
- # img[y0:y1,x0:x1,:] = label_array
- #
- #
- #
- # ###把矩形框画上,指定颜色和线宽
- # tl = font['line_thickness'] or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1 # line/font thickness
- # box_tl= font['boxLine_thickness'] or round(0.002 * (img.shape[0] + img.shape[1]) / 2) + 1
- # c1, c2 = (int(box[0]), int(box[1])), (int(box[2]), int(box[3]))
- # cv2.rectangle(img, c1, c2, color, thickness=box_tl, lineType=cv2.LINE_AA)
- #
- # ###把英文字符score画到类别旁边
- # label = ' %.2f'%(score)
- # tf = max(tl , 1) # font thickness
- # fontScale = font['fontSize'] or tl * 0.33
- # t_size = cv2.getTextSize(label, 0, fontScale=fontScale , thickness=tf)[0]
- #
- # if socre_location=='leftTop':
- # c2 = c1[0]+ lw + t_size[0], c1[1] - lh
- # cv2.rectangle(img, (int(box[0])+lw,int(box[1])) , c2, color, -1, cv2.LINE_AA) # filled
- # cv2.putText(img, label, (c1[0]+lw, c1[1] - (lh-t_size[1])//2 ), 0, fontScale, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
- # elif socre_location=='leftBottom':
- # c2 = box[0]+ lw + t_size[0], box[3] - lh
- # cv2.rectangle(img, (int(box[0])+lw,int(box[3])) , c2, color, -1, cv2.LINE_AA) # filled
- # cv2.putText(img, label, ( box[0] + lw, box[3] - (lh-t_size[1])//2 ), 0, fontScale, [225, 255, 255], thickness=tf, lineType=cv2.LINE_AA)
- #
- # #print('#####line224 fontScale:',fontScale,' thickness:',tf,' line_thickness:',font['line_thickness'],' boxLine thickness:',box_tl)
- # return img
|