不使用深度学习的人脸表情识别python代码实现,要详细中文注释
时间: 2024-06-08 09:11:11 浏览: 24
以下是一个基于传统机器学习方法的人脸表情识别 Python 代码,使用的是 OpenCV 和 scikit-learn 库。
首先,需要导入所需的库:
```python
import cv2
import numpy as np
from sklearn.externals import joblib
```
接下来,加载训练好的分类器模型和特征选取器模型:
```python
clf = joblib.load('classifier.pkl')
feat_extract = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
```
然后,定义分类器模型和特征选取器模型的参数:
```python
IMG_SIZE = 48
NUM_FEATURES = 576
```
接着,定义一个函数用于从图像中提取人脸特征:
```python
def extract_features(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = feat_extract.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
if len(faces) == 0:
return None
(x, y, w, h) = faces[0]
face_crop = gray[y:y+h, x:x+w]
face_resized = cv2.resize(face_crop, (IMG_SIZE, IMG_SIZE))
return face_resized.flatten()
```
该函数首先将输入图像转换为灰度图像,然后使用特征选取器模型检测人脸,并将其裁剪出来。接着,将裁剪后的人脸图像缩放到指定的大小,并将其展平为一个一维向量,作为人脸的特征值。
最后,定义一个函数用于对输入的图像进行表情识别:
```python
def predict_emotion(img):
features = extract_features(img)
if features is None:
return None
else:
emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
emotion_id = clf.predict([features])[0]
return emotion_labels[emotion_id]
```
该函数首先使用 extract_features 函数从输入图像中提取人脸特征。如果提取不到特征,则返回 None。否则,使用分类器模型对提取出的特征进行分类,并返回预测的表情标签。
完整代码如下:
```python
import cv2
import numpy as np
from sklearn.externals import joblib
clf = joblib.load('classifier.pkl')
feat_extract = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
IMG_SIZE = 48
NUM_FEATURES = 576
def extract_features(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = feat_extract.detectMultiScale(gray, scaleFactor=1.3, minNeighbors=5)
if len(faces) == 0:
return None
(x, y, w, h) = faces[0]
face_crop = gray[y:y+h, x:x+w]
face_resized = cv2.resize(face_crop, (IMG_SIZE, IMG_SIZE))
return face_resized.flatten()
def predict_emotion(img):
features = extract_features(img)
if features is None:
return None
else:
emotion_labels = ['Angry', 'Disgust', 'Fear', 'Happy', 'Sad', 'Surprise', 'Neutral']
emotion_id = clf.predict([features])[0]
return emotion_labels[emotion_id]
```
需要注意的是,该方法的表情识别准确率可能不如深度学习方法。
相关推荐
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![tar](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![-](https://csdnimg.cn/download_wenku/file_type_lunwen.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)