【问题标题】:How to detect emotions from a single image and not a constant camera feed?如何从单个图像中检测情绪,而不是从恒定的摄像头馈送中检测情绪?
【发布时间】:2021-05-23 05:30:55
【问题描述】:

我正在尝试修改this repository。这个 repo 不断地从相机中获取帧,并不断更新当前的情绪。我现在要做的是从我的一个文件夹中获取已经保存的单个图像,然后在命令行上打印出情绪。

我尝试为 cv2.VideoCapture 提供图像的路径,但它仍然不起作用,我在尝试时遇到的错误之一是

AttributeError: 'cv2.VideoCapture' object has no attribute 'shape'

代码如下:

from tensorflow.keras.preprocessing.image import img_to_array
import imutils
import cv2
from tensorflow.keras.models import load_model
import numpy as np

# from tensorflow.keras import Sequential
# from tensorflow.keras.layers import Conv2D, Flatten, Dense

# parameters for loading data and images
detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'

# hyper-parameters for bounding boxes shape
# loading models
face_detection = cv2.CascadeClassifier(detection_model_path)
emotion_classifier = load_model(emotion_model_path, compile=False)
EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
 "neutral"]


#feelings_faces = []
#for index, emotion in enumerate(EMOTIONS):
   # feelings_faces.append(cv2.imread('emojis/' + emotion + '.png', -1))

# starting video streaming
cv2.namedWindow('your_face')
# camera = cv2.VideoCapture('C:/Users/ajeel/Desktop/Faces/FZKmwbkp.png')
# camera = cv2.VideoCapture('C://Users//ajeel//Desktop//Faces//FZKmwbkp.png')
camera = cv2.VideoCapture('FZKmwbkp.png')
# camera = cv2.VideoCapture()



while True:
    # frame = camera.read('C://Users//ajeel//Desktop//Faces//FZKmwbkp.png')[1]
    # frame = camera.read('C:/Users/ajeel/Desktop/Faces/FZKmwbkp.png')[1]
    ret, frame = camera.read()
    # reading the frame
    # frame = imutils.resize(frame,width=300)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
    
    canvas = np.zeros((250, 300, 3), dtype="uint8")
    frameClone = frame.copy()
    if len(faces) > 0:
        faces = sorted(faces, reverse=True,
        key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
        (fX, fY, fW, fH) = faces
                    # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
            # the ROI for classification via the CNN
        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)
        
        
        preds = emotion_classifier.predict(roi)[0]
        emotion_probability = np.max(preds)
        label = EMOTIONS[preds.argmax()]
    else: continue

 
    for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
                # construct the label text
                text = "{}: {:.2f}%".format(emotion, prob * 100)

                # draw the label + probability bar on the canvas
               # emoji_face = feelings_faces[np.argmax(preds)]

                
                w = int(prob * 300)
                cv2.rectangle(canvas, (7, (i * 35) + 5),
                (w, (i * 35) + 35), (0, 0, 255), -1)
                cv2.putText(canvas, text, (10, (i * 35) + 23),
                cv2.FONT_HERSHEY_SIMPLEX, 0.45,
                (255, 255, 255), 2)
                cv2.putText(frameClone, label, (fX, fY - 10),
                cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
                cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
                              (0, 0, 255), 2)
#    for c in range(0, 3):
#        frame[200:320, 10:130, c] = emoji_face[:, :, c] * \
#        (emoji_face[:, :, 3] / 255.0) + frame[200:320,
#        10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0)


    cv2.imshow('your_face', frameClone)
    cv2.imshow("Probabilities", canvas)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

camera.release()
cv2.destroyAllWindows()

【问题讨论】:

    标签: python tensorflow opencv


    【解决方案1】:

    我找到了解决办法:

    我在循环之外删除了 cv2.VideoCapture,然后将其移交给帧变量,而是直接将 cv2.imread('path-name') 分配给它。

    此外,我禁用了 Windows,所以现在我只能在命令行上获取打印语句。

    代码如下:

    from tensorflow.keras.preprocessing.image import img_to_array
    import imutils
    import cv2
    from tensorflow.keras.models import load_model
    import numpy as np
    
    # from tensorflow.keras import Sequential
    # from tensorflow.keras.layers import Conv2D, Flatten, Dense
    
    # parameters for loading data and images
    detection_model_path = 'haarcascade_files/haarcascade_frontalface_default.xml'
    emotion_model_path = 'models/_mini_XCEPTION.102-0.66.hdf5'
    
    # hyper-parameters for bounding boxes shape
    # loading models
    face_detection = cv2.CascadeClassifier(detection_model_path)
    emotion_classifier = load_model(emotion_model_path, compile=False)
    EMOTIONS = ["angry" ,"disgust","scared", "happy", "sad", "surprised",
     "neutral"]
    
    
    #feelings_faces = []
    #for index, emotion in enumerate(EMOTIONS):
       # feelings_faces.append(cv2.imread('emojis/' + emotion + '.png', -1))
    
    # starting video streaming
    # cv2.namedWindow('your_face')
    # camera = cv2.VideoCapture('C:/Users/ajeel/Desktop/Faces/FZKmwbkp.png')
    # camera = cv2.VideoCapture('C://Users//ajeel//Desktop//Faces//FZKmwbkp.png')
    # camera = cv2.VideoCapture('FZKmwbkp.png')
    # camera = cv2.imread('C://Users//ajeel//Desktop//Faces//FZKmwbkp.png')
    # 
    
    
    while True:
        # frame = camera.read('C://Users//ajeel//Desktop//Faces//FZKmwbkp.png')[1]
        # frame = camera.read('C:/Users/ajeel/Desktop/Faces/FZKmwbkp.png')[1]
        # ret, frame = camera.read()
        # frame = camera.read()[1]
        frame = cv2.imread('C://Users//ajeel//Desktop//Faces//FZKmwbkp.png')
        # reading the frame
        frame = imutils.resize(frame,width=300)
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        faces = face_detection.detectMultiScale(gray,scaleFactor=1.1,minNeighbors=5,minSize=(30,30),flags=cv2.CASCADE_SCALE_IMAGE)
        
        canvas = np.zeros((250, 300, 3), dtype="uint8")
        frameClone = frame.copy()
        if len(faces) > 0:
            faces = sorted(faces, reverse=True,
            key=lambda x: (x[2] - x[0]) * (x[3] - x[1]))[0]
            (fX, fY, fW, fH) = faces
                        # Extract the ROI of the face from the grayscale image, resize it to a fixed 28x28 pixels, and then prepare
                # the ROI for classification via the CNN
            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)
            
            
            preds = emotion_classifier.predict(roi)[0]
            # print("this is preds")
            # print(preds)
            emotion_probability = np.max(preds)
            # print("this is emotional probability")
            # print(emotion_probability)
            label = EMOTIONS[preds.argmax()]
            # print("this is label")
            print(label)
        else: continue
    
     
        for (i, (emotion, prob)) in enumerate(zip(EMOTIONS, preds)):
                    # construct the label text
                    text = "{}: {:.2f}%".format(emotion, prob * 100)
    
                    # draw the label + probability bar on the canvas
                   # emoji_face = feelings_faces[np.argmax(preds)]
    
                    
                    w = int(prob * 300)
                    cv2.rectangle(canvas, (7, (i * 35) + 5),
                    (w, (i * 35) + 35), (0, 0, 255), -1)
                    cv2.putText(canvas, text, (10, (i * 35) + 23),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.45,
                    (255, 255, 255), 2)
                    cv2.putText(frameClone, label, (fX, fY - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.45, (0, 0, 255), 2)
                    cv2.rectangle(frameClone, (fX, fY), (fX + fW, fY + fH),
                                  (0, 0, 255), 2)
    #    for c in range(0, 3):
    #        frame[200:320, 10:130, c] = emoji_face[:, :, c] * \
    #        (emoji_face[:, :, 3] / 255.0) + frame[200:320,
    #        10:130, c] * (1.0 - emoji_face[:, :, 3] / 255.0)
    
    
        # cv2.imshow('your_face', frameClone)
        # cv2.imshow("Probabilities", canvas)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    camera.release()
    cv2.destroyAllWindows()
    
    

    【讨论】:

      猜你喜欢
      • 2021-05-04
      • 1970-01-01
      • 2013-03-23
      • 2015-03-12
      • 1970-01-01
      • 1970-01-01
      • 2022-10-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多