728x90
반응형
cv2.putText가 간단한데 한글이 깨짐
PIL로 한글 깨짐 방지하는 코드
상단 txt파일에 있는 글자를 이미지에 같이 표현하게끔 코드 작성
겉에 있는 bbox는 cv2의 polylines 활용
import cv2
import numpy as np
import os
from PIL import ImageFont, ImageDraw, Image
# Define Document Type
input_doc = 'directory_name'
file_list = os.listdir('./gts/%s'%input_doc)
file_name = [file.split('.')[0][3:] for file in file_list]
image_list = ['./images/%s/'%input_doc+file+'.jpg' for file in file_name]
for i, f_name in enumerate(file_name):
f = open('./gts/%s/'%input_doc+file_list[i],'r')
img = cv2.imread(image_list[i])
lines = f.readlines()
for line in lines:
polygon = line.split('\t')[:-2]
label = line.split('\t')[-2]
key_value = line.split('\t')[-1]
polygon = [int(point) for point in polygon]
points = []
for idx in range(0, len(polygon), 2):
points.append([polygon[idx], polygon[idx+1]])
points = np.array(points)
img = cv2.polylines(img, [points], True, (255,0,0),2)
fontpath = "../resources/gulim.ttf"
font = ImageFont.truetype(fontpath, 10)
img = Image.fromarray(img)
draw = ImageDraw.Draw(img)
#str(label+":"+key_value)
draw.text((points[0][0],points[0][1]-15), label+":"+key_value, font=font, fill=(0,0,0))
img = np.array(img)
cv2.imwrite( './images/%s/poly_%s.jpg'%(input_doc, f_name), img)
728x90
반응형