1. 安装必要的库
首先需要安装必要的库:dlib、face_recognition、opencv-python、numpy、pillow。
```python
!pip install dlib
!pip install face_recognition
!pip install opencv-python
!pip install numpy
!pip install pillow
```
2. 获取表情图像
在创建表情包之前,需要准备好表情图像。可以从网上下载或者自己拍摄表情照片。将表情图像放在一个文件夹中,每个表情对应一个文件夹。
3. 识别人脸和表情
使用face_recognition库可以很方便地实现人脸和表情的识别。首先需要加载表情图像,并将每个表情的特征向量保存下来。
```python
import face_recognition
import os
emotions = ['happy', 'angry', 'sad', 'neutral'] # 表情名称
# 加载表情图像,并将每个表情的特征向量保存下来
face_encodings = []
for emotion in emotions:
folder_path = f'./{emotion}/'
image_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path)]
for image_path in image_paths:
image = face_recognition.load_image_file(image_path)
face_encoding = face_recognition.face_encodings(image)[0]
face_encodings.append(face_encoding)
```
接下来,我们可以使用OpenCV库实现摄像头的实时读取和显示。对于每一帧图像,我们可以使用face_recognition库识别出人脸和表情,并在图像上标出表情的名称。
```python
import cv2
import numpy as np
from PIL import ImageFont, ImageDraw, Image
# 打开摄像头
cap = cv2.VideoCapture(0)
while True:
# 读取摄像头的一帧图像
ret, frame = cap.read()
# 将图像从BGR色彩空间转换为RGB色彩空间
rgb_frame = frame[:, :, ::-1]
# 在图像中识别人脸和表情
face_locations = face_recognition.face_locations(rgb_frame)
face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
# 判断表情类别
distances = face_recognition.face_distance(face_encodings, face_encoding)
min_distance = np.min(distances)
if min_distance <= 0.5:
emotion = emotions[np.argmin(distances)]
else:
emotion = 'unknown'
# 在图像中标出人脸和表情
cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
font = ImageFont.truetype('arial.ttf', 20)
img_pil = Image.fromarray(frame)
draw = ImageDraw.Draw(img_pil)
draw.text((left, top - 25), emotion, font=font, fill=(255, 0, 0))
frame = np.array(img_pil)
# 显示图像
cv2.imshow('Face Emotion Recognition', frame)
# 按下q键退出程序
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# 释放摄像头
cap.release()
cv2.destroyAllWindows()
```
运行程序后,摄像头会实时读取图像,并在图像上标出人脸和表情。如果识别不出表情,会显示unknown。
4. 创建表情包
最后,我们可以使用Pillow库将识别出的表情保存为表情包。对于每个表情,我们可以将所有该表情的图像合成一个表情包。
```python
# 创建表情包
for emotion in emotions:
folder_path = f'./{emotion}/'
image_paths = [os.path.join(folder_path, f) for f in os.listdir(folder_path)]
images = [Image.open(image_path) for image_path in image_paths]
widths, heights = zip(*(i.size for i in images))
max_width = max(widths)
total_height = sum(heights)
new_im = Image.new('RGB', (max_width, total_height))
y_offset = 0
for image in images:
new_im.paste(image, (0, y_offset))
y_offset += image.size[1]
new_im.save(f'{emotion}.png')
```
运行程序后,会在程序所在的文件夹中生成四个表情包文件:happy.png、angry.png、sad.png、neutral.png。每个表情包文件包含了该表情的所有照片。将表情包上传到聊天软件中,就可以使用自己的表情包了。