简体   繁体   中英

How to create a separate CSV file for each day in face based attendance rather than overwriting the the existing one

In this code attendance get marked in "attendance.csv" file. What I want is to create separate "attendance.csv" day wise, rather than overwriting the existing file, so that I can trace back past attendance if any conflicts from employee arise in future. I am unable to think and produce how it can be achieved.

Attendance CSV file contains the following entries:

ID,NAME,ATTENDANCE,EMAIL
12,sk,1,sk12@gmail.com
15,Aratrika Kosta,0,ak15@gmail.com

Programming language used Python.

def marking():
    df = pd.read_csv("CSV_files//attendance.csv")
   # df.loc[:,"ATTENDANCE"] = 0 # default attendance value (absent)
   # df.to_csv("CSV_files//attendance.csv",mode = 'w', index=False)

    def assure_path_exists(path): #if path not exists create path
        dir = os.path.dirname(path)
        if not os.path.exists(dir):
            os.makedirs(dir)

    
    recognizer = cv2.face.LBPHFaceRecognizer_create()
    assure_path_exists("trainer/")
    recognizer.read('trainer/trainer.yml')
    cascadePath = "haarcascade_frontalface_default.xml"
    faceCascade = cv2.CascadeClassifier(cascadePath);
    font = cv2.FONT_HERSHEY_SIMPLEX
    cam = cv2.VideoCapture(0)

    while True:
        # Read the video frame
        ret, im =cam.read()

        # Convert the captured frame into grayscale
        gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)

        # Get all face from the video frame
        faces = faceCascade.detectMultiScale(gray, 1.2,5)

        # For each face in faces
        for(x,y,w,h) in faces:

            # Create rectangle around the face
            cv2.rectangle(im, (x-20,y-20), (x+w+20,y+h+20), (0,255,0), 4)

            # Recognize the face belongs to which ID
            Id, confidence = recognizer.predict(gray[y:y+h,x:x+w])
            
            
            print(confidence)
            #confidence-->0 (best) , more the value of confidence lesser will be accuracy
            if confidence>80: 
                continue
            
            else:
                imgdict = dict()
                df = pd.read_csv("CSV_files//attendance.csv")
                #Names and Ids as imgdict
                diction = dict(df.loc[:,'ID']) #making dict from ID and NAME columns
                imgdict = {}
                for key,vals in diction.items():
                    imgdict[key+1] = vals

                # Check the ID if exist 
                for k,v in imgdict.items(): #looping through all keys
                    #print(k, type(k), v)
                    if (v==Id):
                        Id = v #if key is matched with recognizer Id , then assign Id=valueofimgdict
                        df = pd.read_csv("CSV_files//attendance.csv")
                        df.loc[k-1,"ATTENDANCE"]= 1  #student is present
                        # df.loc[:,"ATTENDANCE"]= 1  #student is present
                        df.to_csv("CSV_files//attendance.csv",mode = 'w', index=False)
                         
                # Put text describe who is in the picture
                cv2.rectangle(im, (x-22,y-90), (x+w+22, y-22), (0,255,0), -1) #-1-->filled rectangle
                cv2.putText(im, str(Id), (x,y-40), font, 1, (255,255,255), 3) #1 is size , 3 is thickness

        # Display the video frame with the bounded rectangle
        cv2.imshow('im',im) 

        # If 'q' is pressed, close program
        if cv2.waitKey(10) & 0xFF == ord('q'):
            break

    # Stop the camera
    cam.release()

    # Close all windows

    cv2.destroyAllWindows()

Import

You need to import datetime module

import datetime

Just add today Date

then incorporate day with attendance name like following:

x = datetime.date.today()
path = 'CSV_files//attendance_{}.csv'.format(x.day)

OR

path = f'CSV_files//attendance_{x.day}.csv'

Now if you print this path, this will give you the following:

print(path)

Output

CSV_files//attendance_4.csv

For complete date

path = f'CSV_files//attendance_{x.day}-{x.month}-{x.year}.csv'

Output

print(path)    
CSV_files//attendance_4-4-2021.csv

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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