31 lines
892 B
Python
31 lines
892 B
Python
from PIL import Image, ImageDraw, ImageFont
|
|
|
|
from deepface import DeepFace
|
|
|
|
img_path = r"E:\yuxin\test-images\girl-16.jpg"
|
|
|
|
objs = DeepFace.analyze(
|
|
img_path=img_path,
|
|
actions=tuple([
|
|
# 'age',
|
|
# 'gender',
|
|
# 'race',
|
|
'emotion'
|
|
])
|
|
)
|
|
img = Image.open(img_path)
|
|
draw = ImageDraw.Draw(img)
|
|
for obj in objs:
|
|
dominant_emotion = obj['dominant_emotion']
|
|
print()
|
|
x1 = obj['region']['x']
|
|
y1 = obj['region']['y']
|
|
x2 = obj['region']['x'] + obj['region']['w']
|
|
y2 = obj['region']['y'] + obj['region']['h']
|
|
draw.rectangle((x1, y1, x2, y2), outline=(255, 0, 0), width=2)
|
|
text_label = dominant_emotion + ': %.2f' % obj['emotion'][dominant_emotion]
|
|
text_size = int(obj['region']['h'] / 6)
|
|
ft = ImageFont.truetype("/home/arial.ttf", text_size)
|
|
draw.text((x1, y1 - text_size - 2), text_label, (255, 0, 0), font=ft)
|
|
img.show()
|