Download as pdf or txt
Download as pdf or txt
You are on page 1of 23

USER

MAiN ADMiN
SCAN QR
MENU

GET iD
ADD REGiSTRY
UPDATE DATA
REGiSTER DURATiON
ATTENDANCE
CREATE QR
DELETE
ViEW ATTENDANCE
ViEW REGiSTRY

ATTENDANCE BiNARY REGiSTRY


FiLE
Code

# Attedance System V-2.0


print ('''
************ Welcome to Attendance System v2 *************

Following modules need to be installed to run this program


1. numpy
2. pyzbar
3. winsound
4. UUID
5. CV-python
6. pyqrcode
7. png

***********************************************************
''')
import pickle

import winsound
dataBuffer_Main={}
userList=[['admin','password','admin']]

### For Addding Employee information to Registry ###


def addRegistry(): #function definition
import uuid #importing uuid module for random id
generation

choice=input('Do you have the file (Y/N): ')


registry=[]
if choice in 'Yy':
fileOpen=open('source.txt','rb') #opening file to read
data=pickle.load(fileOpen)
registry=data.get('Registry')
attendance=data.get('attendance')
fileOpen.close()

name=input('Enter the name of employee: ') # 0


empID=input('Enter the Employee ID: ') #1

#Automatically Generating Random ID


fpAdress=uuid.uuid1() #2 # Simulating a QR Code or
Fingerprint Reading Machine
record=[name,empID,fpAdress]
registry.append(record)

dataBuffer_Main['Registry']=registry #creating data object


dataBuffer_Main['attendance']=attendance
fileOpen=open('source.txt','wb')

pickle.dump(dataBuffer_Main,fileOpen) #dumping file to file


object

fileOpen.close()

print("Data added Succesfully")


winsound.PlaySound('success.wav.wav',winsound.SND_FILENAME)

### Attendance (List) - For Storing Attendance Details ###

def attendance(fpAdress):

from datetime import datetime #impoting datetime module

fileOpen=open('source.txt','rb') #opening file to read


data=pickle.load(fileOpen) # storing the already existing
data(dictionary) from file to a variable

dataBuffer_attendance=data.get('attendance')# Getting
Attendance list from Dictonary
registry=data.get('Registry')# Getting Registry list from
dictionary
for i in registry : # Searhing for the ID in Registry

if str(i[2])==fpAdress:
print ('Record Found')

name=i[0]
empID=i[1]

fileOpen.close()

currentDate=datetime.date(datetime.now()) ###
Getting current date and time
currentTime=datetime.time(datetime.now())

attendanceRecord=[currentDate,empID,name,currentTime]# Creating
Record (List) with employee details

if type(dataBuffer_attendance)!=list: # Checking if
its NONE type or List type

dataBuffer_attendance=[]

dataBuffer_attendance.append(attendanceRecord)

data['attendance']=dataBuffer_attendance # Assigning
the list to the appropriate key in dictionary

## print (data)
fileOpen=open('source.txt','wb')
pickle.dump(data,fileOpen) # After the Appending the
new data to dictionary, the dictionary is completely
# overwritten with new
dictionary

fileOpen.close()

print ("Attendance was Succesfully Registered")

winsound.PlaySound('success.wav.wav',winsound.SND_FILENAME)

break
else:
print ("Record Not Found !!")

winsound.PlaySound('error_effect.wav',winsound.SND_FILENAME)

### For getting the length of Biggest string in a list (Used for
Tabulation) ###

def maxLenStr(list1):
max=0
for i in list1:
if len(str(i))>=max:
max=len(str(i))
return max

### Display attendance sheet ###

def attendanceSheet():
fileOpen=open('source.txt','rb')
data=pickle.load(fileOpen)
attendance=data.get('attendance') # Opening File And storing
the attedance list to variable

n=len(attendance)
name=[]
date=[]
empID=[]
Time=[]

for i in attendance: # Distributing Name,Date,Employee ID


and Time to individual lists
date.append(i[0])
empID.append(i[1])
name.append(i[2])
Time.append(i[3])
### Visual Representation of data (Tabulation) ###

print ('+',end='')
print ('-'*maxLenStr(date),end='')
print ('+',end='')
print ('-'*maxLenStr(empID),end='') # Horizontal Top
Line #
print ('+',end='')
print ('-'*maxLenStr(name),end='')
print ('+',end='')
print ('-'*maxLenStr(Time),end='')
print ('+')

print ('|','Date',"
"*(int(maxLenStr(date))-4),end='',sep='')
print ('|','EmpID',"
"*(int(maxLenStr(empID))-5),end='',sep='') # Column Names #
print ('|','Name',"
"*(int(maxLenStr(name))-4),end='',sep='')
print ('|','Time',"
"*(int(maxLenStr(Time))-4),end='',sep='')
print ('|')

print ('+',end='')
print ('-'*maxLenStr(date),end='')
print ('+',end='')
print ('-'*maxLenStr(empID),end='')
print ('+',end='')
print ('-'*maxLenStr(name),end='') # Horizontal Line Below
Column Names #
print ('+',end='')
print ('-'*maxLenStr(Time),end='')
print ('+')

for i in range (n): # n means number of attendance records


print ('|',date[i]," "*(int(maxLenStr(date))-
(len(str(date[i])))),end='',sep='')
print ('|',empID[i]," "*(int(maxLenStr(empID))-
(len(empID[i]))),end='',sep='') # Printing Data #
print ('|',name[i]," "*(int(maxLenStr(name))-
(len(name[i]))),end='',sep='')
print ('|',Time[i]," "*(int(maxLenStr(Time))-
(len(str(Time[i])))),end='',sep='')
print('|')

print ('+',end='')
print ('-'*maxLenStr(date),end='')
print ('+',end='')
print ('-'*maxLenStr(empID),end='')
print ('+',end='')
print ('-'*maxLenStr(name),end='') # Horizontal Bottom Line
#
print ('+',end='')
print ('-'*maxLenStr(Time),end='')
print ('+')

### Display Registry ( Without fpAdress )

def viewRegistry():
fileOpen=open('source.txt','rb')
data=pickle.load(fileOpen) # Opening and Storing
Registry list to a Variable
registry=data.get('Registry')

n=len(registry)
name=[]
empID=[]
fpAdress=[]

print ('''
Welcome to Admin Portal!
''')

for i in registry:

empID.append(i[1]) ## Distribution of Name,


Employee ID and FP Adresses to individual lists
name.append(i[0])
fpAdress.append(str(i[2]))

print ('+',end='')
print ('-'*maxLenStr(name),end='')
print ('+',end='')
print ('-'*maxLenStr(empID),end='') #
Horizontal Top Line #
print ('+',end='')
print ('-'*maxLenStr(fpAdress),end='')
print ('+')

print ('|','Name',"
"*(int(maxLenStr(name))-4),end='',sep='')
print ('|','EmpID',"
"*(int(maxLenStr(empID))-5),end='',sep='') # Column Names #
print ('|','fpAdress',"
"*(int(maxLenStr(fpAdress))-8),end='',sep='')
print ('|')
print ('+',end='')
print ('-'*maxLenStr(name),end='')
print ('+',end='')
print ('-'*maxLenStr(empID),end='') #
Horizontal line below Column Names #
print ('+',end='')
print ('-'*maxLenStr(fpAdress),end='')
print ('+')

for i in range (n): # n means number of attendance


records
print ('|',name[i]," "*(int(maxLenStr(name))-
(len(name[i]))),end='',sep='')
print ('|',empID[i]," "*(int(maxLenStr(empID))-
(len(empID[i]))),end='',sep='') # Printing Data #
print ('|',fpAdress[i],"
"*(int(maxLenStr(fpAdress))-(len(fpAdress[i]))),end='',sep='')

print ('|')

print ('+',end='')
print ('-'*maxLenStr(name),end='')
print ('+',end='')
print ('-'*maxLenStr(empID),end='') #
Horizontal Bottom Line #
print ('+',end='')
print ('-'*maxLenStr(fpAdress),end='')
print ('+')

### QR Code Creation ###

def createQR():
import pyqrcode
import png
from pyqrcode import QRCode

fileOpen=open('source.txt','rb')
data=pickle.load(fileOpen) # Opening file and lading
the dictionary to a variable
registry=data.get('Registry')
viewRegistry() # Printing
registry
empID=input("Enter the employee ID of the Person: ")

for i in registry:
if i[1]==empID: # Searching for the
record in Registry
fpAdress=i[2]
name=i[0]
break
else:
print ('Record Not found')

winsound.PlaySound('error_effect.wav',winsound.SND_FILENAME)

qrcode=pyqrcode.create(str(fpAdress))
# Creating QR Code
try:
qrcode.png('QRCode '+name+' '+empID+'.png',scale=6)
print ('QR Code Succesfully Created')
# Saving QR Code to the Directory

winsound.PlaySound('success.wav.wav',winsound.SND_FILENAME)
except:
print('File Already Exists!')

winsound.PlaySound('error_effect.wav',winsound.SND_FILENAME)
### Updating Registry ###

def updateRegistry():
import uuid

fileOpen=open('source.txt','rb')
data=pickle.load(fileOpen) ## Opening and Storing
Registry list to a variable
registry=data.get('Registry')

viewRegistry()
empID=input('Enter the employee ID: ')
for i in registry:
if empID==i[1]: ## Searching with Employee ID
print ('''
What do you want to change:
1.Name
2.Fingerprint Adress

''')

### Updating Name of an Employee

choice=int(input(">>>"))
if choice==1:
name=input('Enter New Name: ')
i[0]=name ## Registering new
name given by the user
print('New Name Registered')

winsound.PlaySound('success.wav.wav',winsound.SND_FILENAME)

### Updating fpAdress of an Employee

if choice==2:
fpAdress=uuid.uuid1()
i[2]=fpAdress ## Registering with new
auto-generated adress
print ('New Fingerprint Registered')

winsound.PlaySound('success.wav.wav',winsound.SND_FILENAME)
print ('New Fingerprint Adress',fpAdress)

break
else:
print("***No record found!***")

winsound.PlaySound('error_effect.wav',winsound.SND_FILENAME)

data['Registry']=registry # Updating Dictionary

fileOpen.close()
fileOpen=open('source.txt','wb')
pickle.dump(data,fileOpen) # Overwritting file with new
dictionary
fileOpen.close()

## CLEARING FILE ##

def clearFile():
fileOpen=open('source.txt','wb')
fileOpen.close()
print ('ALL RECORDS CLEARED!')

winsound.PlaySound('success.wav.wav',winsound.SND_FILENAME)

## CONVERING SECONDS TO HOURS, MINUTES , AND SECONDS

def convertSeconds(secs):
hrs=secs//3600
mins=((secs)-(hrs*3600))//60
secs=((secs)-((hrs*3600)+(mins*60)))//1

return (hrs,mins,secs)

## CALCULATE DURATION ##

def duration():
import datetime
fileOpen=open('source.txt','rb')
data=pickle.load(fileOpen) # Opening and
loading the ditioanry to a varibale
attendance=data.get('attendance')
time=[]

empID=input('Employee ID: ')


date=input("Date (DD-MM-YYYY): ")
for i in attendance:
if empID==i[1] and date==str(i[0]):

combine=datetime.datetime.combine(i[0],i[3]) #
Searching for the Record with employee ID and date
name=i[2]
time.append(combine)

if time!=[] and len(time)%2==0:

### TABULATION ##

entryTime=[]
exitTime=[]

for i in range (len(time)):


if i%2==0:
entryTime.append(time[i])
else: #
Seperating Entry and Exit time
exitTime.append(time[i])

print ('+',end='')
print ('-'*maxLenStr(entryTime),end='')
print ('+',end='')
print ('-'*maxLenStr(exitTime),end='') # Horizontal
Top Line #
print ('+')

print ('|','Entry Time'," "*(26-10),end='',sep='')


print ('|','Exit Time'," "*(26-9),end='',sep='')
print ('|')

print ('+',end='')
print ('-'*maxLenStr(entryTime),end='')
print ('+',end='')
print ('-'*maxLenStr(exitTime),end='') # Horizontal
Top Line #
print ('+')

for i in range (len(entryTime)): # n means number of


attendance records
print ('|',str(entryTime[i]),"
"*(int(maxLenStr(entryTime))-
(len(str(entryTime[i])))),end='',sep='')
print ('|',str(exitTime[i]),"
"*(int(maxLenStr(exitTime))-
(len(str(exitTime[i])))),end='',sep='') # Printing Data #
print('|')

print ('+',end='')
print ('-'*maxLenStr(entryTime),end='')
print ('+',end='')
print ('-'*maxLenStr(exitTime),end='') # Horizontal
Top Line #
print ('+')

### TABULATION END ###

n=len(time)

if n%2==0:
deltaTime=time[n-1]-time[0] # Calculating
difference of times

else:
print ('Exit time not found!, Taking Current time as
exit time')
deltaTime=datetime.now()-time[0]

seconds=deltaTime.total_seconds() # Converting
Time to secons
convert=convertSeconds(seconds)
print ('Total time in Office\n')
print('Hours :\t\t',convert[0])
print('Minutes: \t',convert[1]) # Converting
Seconds to Hours, Minutes And Seconds
print('Seconds: \t',convert[2])

### Break Time Calculator ###

breakTime=datetime.datetime(2021,1,1,0,0,0) #
Fixing Random Date
b1=0
b2=0

for i in range (n-1):


if i !=0 and i!=(n-2) and i%2!=0:
b1=time[i]
b2=time[i+1]
breakTime+=(b2-b1)

breakTime=breakTime-datetime.datetime(2021,1,1,0,0,0)
# Taking Delta of time with zero (Since time cannot be directly
-

# converted to seconds)
breakSeconds=breakTime.total_seconds()
convert2=convertSeconds(breakSeconds) #
Conerting to Seconds

print ('\nTotal Break Time\n')

print('Hours :\t\t',convert2[0])
print('Minutes: \t',convert2[1])
print('Seconds: \t',convert2[2])

### Effective Workking Hours ###

effectiveTime=seconds-breakSeconds # Subtracting
Break time from Total time in office to get effective working hours
convert3=convertSeconds(effectiveTime)

print('\nEffective Working Hours\n')

print('Hours :\t\t',convert3[0])
print('Minutes: \t',convert3[1])
print('Seconds: \t',convert3[2])

else:
print('Not Enough Records Found')

winsound.PlaySound('error_effect.wav',winsound.SND_FILENAME)
## SCAN QR CODE ##

def scan():

import cv2
import numpy as np
from pyzbar.pyzbar import decode

cap=cv2.VideoCapture(1) # Starting Video Camera Handle


cap.set(3,640) # Setting Height of the window
cap.set(4,480) # Setting width of the window

while True:
myData=0

true,img=cap.read() # Unpacking Tuple

for i in decode(img): # Travsering through decode array

myData=i.data.decode('utf-8') ## Converting the QR


code data from binary

cv2.imshow('result',img) # Preview Window


if myData!=0:
cap.release() # Releasing Camera Handle
cv2.destroyAllWindows() # Closing All Windows
attendance(myData) # Registering Attendance with
attendance function
break

cv2.waitKey(1) # Holding Camera Window

def delete():
fileOpen=open('source.txt','rb')
data=pickle.load(fileOpen) ## Opening file and loading
dictionary to a varible
registry=data.get('Registry')

viewRegistry()
empID=input("Enter Employee Number: ") # Getting
Employee ID
for i in registry:
if empID==i[1]: # Searching in
Registry

registry.remove(i) # Deleting from


Regsitry

data["Registry"]=registry

fileOpen.close()

You might also like