
    8Zh                     \    d dl Z d dlZd dlZd dlZd dlZd dlmZ dZd Zd Z	d Z
d Zd ZdS )	    N)datetimezapp/facial_attendance_db.dbc                     t           j                            t                    st	          d           t          j        t                    5 } |                                 }|                    d           |                    d           |                    d           | 	                                 ddd           dS # 1 swxY w Y   dS )z2Sets up the database and creates necessary tables.z Creating facial_attendance_db...z
            CREATE TABLE IF NOT EXISTS USERS (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT UNIQUE NOT NULL
            )
        z
            CREATE TABLE IF NOT EXISTS attendance (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                name TEXT NOT NULL,
                timestamp DATETIME DEFAULT CURRENT_TIMESTAMP
            )
        a/  
            CREATE TABLE IF NOT EXISTS USER_IMAGES (
                id INTEGER PRIMARY KEY AUTOINCREMENT,
                user_id INTEGER NOT NULL,
                image_path TEXT,
                encoding TEXT NOT NULL,
                FOREIGN KEY(user_id) REFERENCES USERS(id)
            )
        N)
ospathexistsAPP_DB_FILEprintsqlite3connectcursorexecutecommit)connr   s     7D:\DownLoads\facial_attendance_system-main\app\utils.pydb_setupr   
   s	   7>>+&& 20111		%	%   	 	 	 	  	 	 	 	  	 	 	 	1                 s   A(CC	Cc                     t           j                            |           rFt          j        |           }t          j        |          }|r|d         S t          d|  d           nt          d|  d           dS )z"Encodes the face in an image file.r   zWarning: No faces found in 'z'.zError: Image file 'z' not found.N)r   r   r   face_recognitionload_image_fileface_encodingsr	   )img_pathimg_dataimg_encodingss      r   
encode_imgr   (   s    	w~~h <#3H==(7AA 	$ ##9X999:::::H:::;;;4    c                     g } 	 t          j        t                    5 }|                                }|                    d                                          }|D ]R}t          j        t          j	        |d                             }| 
                    |d         |d         |f           S	 ddd           n# 1 swxY w Y   n)# t          $ r}t          d|            Y d}~nd}~ww xY w| S )z&Loads all encodings from the database.z$SELECT id, name, encoding FROM USERS   r      NzError loading encodings: )r
   r   r   r   r   fetchallnparrayjsonloadsappend	Exceptionr	   )encoding_listsr   r   resultsrowencodinges          r   load_encodingsr*   4   sQ   N/_[)) 	BT[[]]Fnn%KLLUUWWG B B8DJs1v$6$677%%s1vs1vx&@AAAAB	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B 	B  / / /-!--......../s;   C BB;/C ;B??C B?C 
C-C((C-c                    	 t          j        t                    5 }|                                }t	          j                                        d          }|                    d| |f           |                                 t          d|  d           ddd           dS # 1 swxY w Y   dS # t           j
        $ r}t          d|            Y d}~dS d}~ww xY w)z"Marks attendance for a given user.z%Y-%m-%d %H:%M:%Sz6INSERT INTO attendance (name, timestamp) VALUES (?, ?)zAttendance marked for .NzError marking attendance: )r
   r   r   r   r   nowstrftimer   r   r	   Error)	user_namer   r   
local_timer)   s        r   mark_attendancer2   B   s.   0_[)) 	9T[[]]F!001DEEJNNSV_akUlmmmKKMMM79777888	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 	9 = 0 0 0.1../////////0s;   B/ A:B"B/ "B&&B/ )B&*B/ /C>CCc                  8   	 t          j        t                    5 } |                                 }|                    d                                          }|cddd           S # 1 swxY w Y   n)# t          $ r}t          d|            Y d}~nd}~ww xY wg S )zFetches all attendance records.z&SELECT name, timestamp FROM attendanceNz Error fetching attendance logs: )r
   r   r   r   r   r   r$   r	   )r   r   r&   r)   s       r   fetch_attendance_logsr4   N   s    6_[)) 	T[[]]Fnn%MNNWWYYG	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	 	  6 6 6444555555556Is:   A1 =A%A1 %A))A1 ,A)-A1 1
B;BB)r   r
   r!   r   numpyr   r   r   r   r   r*   r2   r4    r   r   <module>r7      s    				                +  <
 
 
  
0 
0 
0	 	 	 	 	r   