python csv文件不包含用户的所有情感

问题描述 投票:-3回答:1
from keras.preprocessing.image import img_to_array
import imutils
import cv2
from keras.models import load_model
import numpy as np
import csv

# parameters for loading data and images
detection_model_path, emotion_model_path = "haarcascade_frontalface_default.xml", "traieddata.hdf5"

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


# starting video streaming
cv2.namedWindow("your_face")
camera = cv2.VideoCapture(0)
while True:
    frame = camera.read()[1]
    # reading the frame
    frame = imutils.resize(frame, width=1000)
    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

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

        preds = emotion_load.predict(RE)[0]

        label = human_emotion[preds.argmax()]
    else:
        continue

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

        """w = int(prob * 300)                                #for saving memory remove 1st 3 lines these are of no use 
                cv2.rectangle(canvas, (7, (i * 35) + 5),              #untill you need graph (percentage canva)
                (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)
        l = [label]
        with open("coors_new.csv", mode="w") as outfile:  # here is the problem
            writer = csv.writer(outfile)
            writer.writerow(l)

    cv2.imshow("your_face", frameClone)
    # cv2.imshow('percentage',canvas) uncomment to see percentage
    if cv2.waitKey(1) & 0xFF == ord("q"):
        break

camera.release()
cv2.destroyAllWindows()

无法生成完美的csv文件,csv文件仅包含用户的第一情感,但我希望它应保存用户的所有情感。我已经在True时尝试过,但是给出了权限错误。我需要解决此错误,我认为缩进错误或我不知道。如何解决。帮助我解决这个问题,并为所有用户情感生成csv文件。如果您想要hdf5文件,我可以给您]

python python-3.x csv python-requests cv2
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.