简体   繁体   English

为什么我无法创建 csv 文件?

[英]Why am I not able to create a csv file?

I've been working on this facial recognition project for a few weeks and while I am able to get data on the scan the program does to recognize the face I'm having trouble taking that data and organizing it into a CSV file.我已经在这个面部识别项目上工作了几个星期,虽然我能够获得扫描数据,但程序确实可以识别面部,但我在获取这些数据并将其组织成 CSV 文件时遇到了麻烦。 I am trying to capture the face in various emotions which would be Happy, Sad, Angry, Surprised, Neutral, and Confused.我试图捕捉各种情绪中的面孔,包括快乐、悲伤、愤怒、惊讶、中立和困惑。 The problem is when I try to download this data the program won't let me.问题是当我尝试下载此数据时,程序不允许我下载。 The way that this is supposed to be set up is when you run the program it asks you for the name of the CSV file and the class the file belongs to.应该设置的方式是当您运行程序时,它会询问您 CSV 文件的名称和文件所属的类。 Each emotion goes to one class.每种情绪都属于一个班级。 So if the file was named Happy the class would be 1 but if the file was Sad the class would be 2. Once it has that it's able to launch the webcam scan the face and once it has 200 sets of data a CSV file is created populated with all the points it could gather from that particular emotion.因此,如果文件名为 Happy,则类将为 1,但如果文件为 Sad,则类将为 2。一旦有了它,它就能够启动网络摄像头扫描面部,一旦它有 200 组数据,就会创建一个 CSV 文件填充了它可以从特定情绪中收集到的所有点。 What I have tried is to separate the code into two parts.我所尝试的是将代码分成两部分。 One part it can run and I can see various markers along my face highlighting my eyes, ears, etc. The other part of the code is what allows the data to be converted into a CSV file.它可以运行一部分,我可以看到脸上的各种标记突出显示我的眼睛、耳朵等。代码的另一部分是允许将数据转换为 CSV 文件的部分。 I've tried looking up the error that I get but to no avail.我试过查找我得到的错误,但无济于事。

import cv2
import face_recognition
import math
import csv
from os import path

# calculate Eye Aspect Ratio (EAR)
def cal_EAR(arr):
    p2_p6 = math.sqrt(pow((arr[1][0] - arr[5][0]), 2) + pow((arr[1][1] - arr[5][1]), 2))
    p3_p5 = math.sqrt(pow((arr[2][0] - arr[4][0]), 2) + pow((arr[2][1] - arr[4][1]), 2))
    p1_p4 = math.sqrt(pow((arr[0][0] - arr[3][0]), 2) + pow((arr[0][1] - arr[3][1]), 2))
    return (p2_p6 + p3_p5) / (2 * p1_p4)


# calculate Mouth Aspect Ratio (MAR)
def cal_MAR(arr_top, arr_bottom):
    top_bottom = math.sqrt(pow(arr_top[-3][0] - arr_bottom[-3][0], 2) + pow(arr_top[-3][1] - arr_bottom[-3][1], 2))
    left_right = math.sqrt(pow(arr_top[0][0] - arr_bottom[0][0], 2) + pow(arr_top[0][1] - arr_bottom[0][1], 2))
    return top_bottom / left_right


# calculate pupil circularity (PUC)
def cal_PUC(arr):
    distance_1_2 = math.sqrt(pow(arr[0][0] - arr[1][0], 2) + pow(arr[0][1] - arr[1][1], 2))
    distance_2_3 = math.sqrt(pow(arr[1][0] - arr[2][0], 2) + pow(arr[1][1] - arr[2][1], 2))
    distance_3_4 = math.sqrt(pow(arr[2][0] - arr[3][0], 2) + pow(arr[2][1] - arr[3][1], 2))
    distance_4_5 = math.sqrt(pow(arr[3][0] - arr[4][0], 2) + pow(arr[3][1] - arr[4][1], 2))
    distance_5_6 = math.sqrt(pow(arr[4][0] - arr[5][0], 2) + pow(arr[4][1] - arr[5][1], 2))
    distance_6_1 = math.sqrt(pow(arr[5][0] - arr[0][0], 2) + pow(arr[5][1] - arr[0][1], 2))
    distance_2_5 = math.sqrt(pow(arr[2][0] - arr[5][0], 2) + pow(arr[2][1] - arr[5][1], 2))
    perimeter = distance_1_2 + distance_2_3 + distance_3_4 + distance_4_5 + distance_5_6 + distance_6_1
    area = pow(distance_2_5 / 2, 2) * math.pi
    circularity = (4 * math.pi * area) / pow(perimeter, 2)
    return circularity


# calculate eyebrow angle (EBA)
def cal_EBA(arr):
    hypotenuse = math.sqrt(pow(arr[2][0] - arr[3][0], 2) + pow(arr[2][1] - arr[3][1], 2))
    opposite = abs(arr[2][1] - arr[3][1])
    angle = opposite / hypotenuse
    return angle

# calculate chin aspect ratio (CAR)
def cal_CAR(arr):
    p0_p16 = math.sqrt(pow(arr[0][0] - arr[16][0], 2) + pow(arr[0][1] - arr[16][1], 2))
    p0_p8 = math.sqrt(pow(arr[0][0] - arr[8][0], 2) + pow(arr[0][1] - arr[8][1], 2))
    CAR = p0_p8 / p0_p16
    return CAR

def modify_csv(file_name):
    # ask user to enter the value of the class
    CLASS = input("Please enter the value of the class: ")

    # open the csv file
    with open(file_name, 'w', newline='') as file:
        writer = csv.writer(file)
        writer.writerow(["L_EAR", "R_EAR", "MAR", "PUC", "MOE", "ABA", "CAR", "CLASS"])

    # turn on the webcam and check the status
    capture = cv2.VideoCapture(0)
    if capture.isOpened() is False:
        print("Camera Error, please check your camera @_@")
        exit()

    rows = 0
    while True:
        # change the BGR frame to gray frame
        ret, frame = capture.read()
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

        # use face_recognition library to locate the landmarks
        face_marks = face_recognition.face_landmarks(gray, None, "large")

        # calculate EAR, MAR, PUC, and MOE
        if face_marks.__len__() != 0:
            L_EAR = cal_EAR(face_marks[0]["left_eye"])
            R_EAR = cal_EAR(face_marks[0]["right_eye"])
            MAR = cal_MAR(face_marks[0]["top_lip"], face_marks[0]["bottom_lip"])
            PUC = cal_PUC(face_marks[0]["left_eye"])
            EBA = cal_EBA(face_marks[0]["right_eyebrow"])
            CAR = cal_CAR(face_marks[0]["chin"])
            MOE =  MAR / L_EAR

            # write these values to csv file
            with open(file_name, 'a', newline='') as file:
                writer = csv.writer(file)
                writer.writerow([L_EAR, R_EAR, MAR, PUC, MOE, EBA, CAR, CLASS])

            rows += 1
            print("Number of records: ", rows)

        # press q to exit the loop
        if rows >= 200:
            print("Done")
            break

    # release the memory
    capture.release()
    cv2.destroyAllWindows()


def main():
    is_existed = True
    while is_existed:
        # input the name of the csv file
        file_name = input("Please enter the name of the csv file you want to create: ")
        file_name = "../csv_files/" + file_name + ".csv"

        # check whether the file is already existed
        is_existed = path.exists(file_name)

        # warn user if the file is already existed
        if is_existed:
            print("The file is already existed")
            # ask user whether he/she wants to delete it or create another file
            choice = input("Do you want to rewrite file? (N/Y): ")
            if choice.upper() == 'N':
                continue
            elif choice.upper() == 'Y':
                is_existed = False
                modify_csv(file_name)
        else:
            is_existed = False
            modify_csv(file_name)
            
if __name__ == "__main__":
    main()

This is the error I get when running the code这是我运行代码时得到的错误

Please enter the name of the csv file you want to create: Neutral
Please enter the value of the class: 1
---------------------------------------------------------------------------
FileNotFoundError                         Traceback (most recent call last)
<ipython-input-13-33aeab86270a> in <module>
    120 
    121 if __name__ == "__main__":
--> 122     main()
    123     modify_csv(file_name)
    124 

<ipython-input-13-33aeab86270a> in main()
    117         else:
    118             is_existed = False
--> 119             modify_csv(file_name)
    120 
    121 if __name__ == "__main__":

<ipython-input-13-33aeab86270a> in modify_csv(file_name)
     48 
     49     # open the csv file
---> 50     with open(file_name, 'w', newline='') as file:
     51         writer = csv.writer(file)
     52         writer.writerow(["L_EAR", "R_EAR", "MAR", "PUC", "MOE", "ABA", "CAR", "CLASS"])

FileNotFoundError: [Errno 2] No such file or directory: '../csv_files/Neutral.csv'

It seems like the issue is the fact that csv_files , the directory, does not exist yet.问题似乎在于目录csv_files尚不存在。 Try creating it manually first or just call `os.mkdir('../csv_files') before creating those csv files.尝试先手动创建它,或者在创建这些 csv 文件之前调用 `os.mkdir('../csv_files') 。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM