def draw_text(img, text, box, color='bw'):
"""
FONT_HERSHEY_COMPLEX
FONT_HERSHEY_COMPLEX_SMALL
FONT_HERSHEY_DUPLEX
FONT_HERSHEY_PLAIN
FONT_HERSHEY_SCRIPT_COMPLEX
FONT_HERSHEY_SCRIPT_SIMPLEX
FONT_HERSHEY_SIMPLEX
FONT_HERSHEY_TRIPLEX
FONT_ITALIC
"""
x, y, w, h = box
font = cv2.FONT_HERSHEY_DUPLEX
region = crop(img, box)
if color == 'bw':
brightness = np.mean(cv2.cvtColor(region, cv2.COLOR_BGR2GRAY))
if brightness > 127:
font_color = (0,0,0)
else:
font_color = (255,255,255)
elif color == 'color':
mean_bg = np.round(np.mean(region, axis=(0, 1)))
font_color = tuple(map(int, np.array((255,255,255)) - mean_bg))
else:
font_color = (255, 0, 0) # blue
cv2.putText(img, text, (x, y+h), font, 1, font_color, 2)
评论列表
文章目录