gpt4 book ai didi

python - 如何从图像中分离出人脸?

转载 作者:行者123 更新时间:2023-12-01 00:19:52 25 4
gpt4 key购买 nike

我正在研究情感识别。目前的设计可以识别一张脸。然而,一张图像中的多个面孔只能在一种情感中被识别。

我尝试使用 for 循环来获取单独的面孔,但代码抛出错误。帮助我获得单独的面孔以进行情感识别。

face_detection = cv2.CascadeClassifier('haarcascade_files/haarcascade_frontalface_default.xml')
emotion_classifier = load_model('_mini_XCEPTION.102-0.66.hdf5', compile=False)
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised","neutral"]


image = cv2.imread('test.jpg')

gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)


faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)

如果一张脸工作正常

if len(faces) == 1:
print(len(faces), "face Length 1")
faces = sorted(faces, reverse=True,
key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]

(fX, fY, fW, fH) = faces

roi = gray[fY:fY + fH, fX:fX + fW]
roi = cv2.resize(roi, (64, 64))
roi = roi.astype("float") / 255.0
roi = img_to_array(roi)
roi = np.expand_dims(roi, axis=0)
#print(type(roi), "roi", len(roi))

preds = emotion_classifier.predict(roi)[0]
emotion_probability = np.max(preds)
label = EMOTIONS[preds.argmax()]
print(label)

我尝试使用循环

for xx in (faces):

xx = sorted(0, reverse=True,
key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
(fX, fY, fW, fH) = xx

错误

key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
TypeError: 'int' object is not iterable

最佳答案

像这样循环面孔:

for face in faces:
(fX, fY, fW, fH) = face
# rest of your code

关于python - 如何从图像中分离出人脸?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59026026/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com