Time not being inserted in MongoDB (Python)

Viewed 13
        import face_recognition
        import cv2
        import numpy as np
        from datetime import datetime
        from datetime import date
        from pymongo import MongoClient
        import time 
        
        
        video_capture = cv2.VideoCapture(0)
        
        
        Siddharth_image = face_recognition.load_image_file("Siddharth.png")
        Siddharth_face_encoding = face_recognition.face_encodings(Siddharth_image)[0]
       
        
        
        
        known_face_encodings = [

        # 1. List item

            Gaurav_face_encoding,
            Mahima_face_encoding,
            Pushpraj_face_encoding,
            Sardeep_face_encoding,
            Sandeep_face_encoding,
            Aakash_face_encoding,
            Abhishek_face_encoding,
            Aishwarya_face_encoding,
            Akhil_face_encoding,
            Akhilesh_face_encoding,
            Anil_face_encoding,
            Ankit_face_encoding,
            Ankita_face_encoding,
            AnkitE_face_encoding,
            AnkitP_face_encoding,
            aprajita_face_encoding,
            Ayush_face_encoding,
            Bhavesh_face_encoding,
            Chitresh_face_encoding,
            Deepak_face_encoding,
            Divyanshi_face_encoding,
            Harsh_face_encoding,
            Jighyasa_face_encoding,
            Krishna_face_encoding,
            Mitesh_face_encoding,
            Neha_face_encoding,
            Nisha_face_encoding,
            Nishi_face_encoding,
            Nitin_face_encoding,
            Rahul_face_encoding,
            Sahil_face_encoding,
            Shivam_face_encoding,
            Shivasha_face_encoding,
            Shriya_face_encoding,
             ShriyaA_face_encoding,
             Shruti_face_encoding,
             ShubhamJ_face_encoding,
             Shubhankit_face_encoding,
             Sonika_face_encoding,
             Swati_face_encoding,
             Vikalp_face_encoding,
             Vinitha_face_encoding,
             Soumya_face_encoding,
             Siddharth_face_encoding
        
        ]
        known_face_names = [
            "Gaurav Gaur",
            "Mahima",
            "Pushpraj",
            "Sardeep",
            "Sandeep",
            "Aakash",
            "Abhishek",
            "Aishwarya",
            "Akhil Verma",
            "Akhilesh",
            "Anil",
            "Ankit",
            "Ankita",
            "Ankit Engle",
            "Ankit Pawar",
            "Aparajita",
            "Ayush",
            "Bhavesh",
            "Chitresh",
            "Deepak",
            "Divyanshi",
            "Harsh",
            "Jighaysa",
            "Krishna",
            "Mitesh",
            "Neha",
            "Nisha",
            "Nishi",
            "Nitin",
            "Rahul",
            "Sahil",
            "Shivam",
            "Shivasha",
            "Shriya",
            "Shriya Agnihotri",
            "Shruti",
            "Shubham Joshi",
            "Shubhankit",
            "Sonika",
            "Swati",
            "Vikalp",
            "Vinitha",
            "Soumya",
            "Siddharth"
        ]
        List_Name={}
        face_locations = []
        face_encodings = []
        face_names = []
        login={}
        timeset=set()
        process_this_frame = True
        
        while True:
        
            ret, frame = video_capture.read()
            
        
            if process_this_frame:
               
                small_frame = cv2.resize(frame, (0, 0), fx=0.25, fy=0.25)
        
                rgb_small_frame = small_frame[:, :, ::-1]
                
                face_locations = face_recognition.face_locations(rgb_small_frame)
                face_encodings = face_recognition.face_encodings(rgb_small_frame, face_locations)
                face_names = []
                unknownCount=0
                
                for face_encoding in face_encodings:
                    
                    matches = face_recognition.compare_faces(known_face_encodings, face_encoding,tolerance=0.5)
                    name = "Unknown"
                        
                    face_distances = face_recognition.face_distance(known_face_encodings, face_encoding)
                    best_match_index = np.argmin(face_distances)
                    
                           
                    if matches[best_match_index]:
                        now = datetime.now()
                        current_time = now.strftime("%H:%M:%S")
                        name = known_face_names[best_match_index]
                        if(name not in  List_Name.keys()) :
                                listt = set()
                                listt.add(now.strftime("%H:%M:%S"))
                                List_Name[name]= listt
                        else:  
                                now = datetime.now()
                                current_time = now.strftime("%H:%M:%S")
                                # time.sleep(10)
                                timeset.add(current_time)
                                List_Name[name]= timeset
                    else :
                        unknownCount=unknownCount+1
                    face_names.append(name)
                    
            process_this_frame = not process_this_frame
        
        
            for (top, right, bottom, left), name in zip(face_locations, face_names):
                top *= 4
                right *= 4
                bottom *= 4
                left *= 4
        
                cv2.rectangle(frame, (left, top), (right, bottom), (0, 0, 255), 2)
        
                cv2.rectangle(frame, (left, bottom - 35), (right, bottom), (0, 0, 255), cv2.FILLED)
                font = cv2.FONT_HERSHEY_DUPLEX
                cv2.putText(frame, name, (left + 6, bottom - 6), font, 1.0, (255, 255, 255), 1)
        
            cv2.imshow('Video', frame)
        
            if cv2.waitKey(1) & 0xFF == ord('q'):
                break
        
        video_capture.release()
        cv2.destroyAllWindows()
        myclient =MongoClient("mongodb://localhost:27017/")
        mydb = myclient["OpenCV"]
        mycol = mydb["OpenCVData"]
        # document={
        #     "name":"Pushpraj",
        # "time":{10:30}
        # }
        # mycol.insert_one(document)
        
        for i in range (0,len(List_Name)-1):
            name=list(List_Name.keys())[i];
            time=str(List_Name.values())[i];
        x = mycol.insert_one({
            "name" :str(name),
            "time" :str(time)
        })
        print(name)
        print(time) 
        print(List_Name.values())
        length=len(List_Name)  
        today = date.today()
        print("login details for date : ",today,"Login is ",List_Name)
        print("Known count is : ",length)
        print("Unknown count is : ",unknownCount)
        print("Total count is : ",unknownCount+length)
    
    
  1. Is my code - working but when i insert it into mongo time inserted is <module 'time' (built-in)> full result is - Pushpraj <module 'time' (built-in)> dict_values([{'13:11:34'}]) login details for date : 2022-09-22 Login is {'Pushpraj': {'13:10:34'}} Known count is : 1 Unknown count is : 0 Total count is : 1

How can I get to insert the correct time that I can see in output

1 Answers

You have to convert the str(time) to a list format then extract the first element through it, this is how you can do this:

"time" : str(list(list(time)[0][0])[0])

Just convert the dict_values to a list, then get the inner list, after that convert the inner dict to again a list and slice out the string.

Related