簡體   English   中英

如何從圖像中分離出人臉?

[英]How to get separate face from image?

我正在研究情感識別。 當前的設計可以識別一張臉。 然而,一張圖像中的多張臉只能在一種情緒中被識別。

我嘗試使用 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM