sarwansingh commited on
Commit
996139e
1 Parent(s): 2a7b7bf

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +115 -75
app.py CHANGED
@@ -1,92 +1,132 @@
1
- from keras.models import load_model
2
  from PIL import Image
3
  import numpy as np
4
- import cv2
5
  import requests
6
-
7
  import face_recognition
8
  import os
9
- from datetime import datetime
10
 
11
- #the following are to do with this interactive notebook code
12
- from matplotlib import pyplot as plt # this lets you draw inline pictures in the notebooks
13
- import pylab # this allows you to control figure size
14
- pylab.rcParams['figure.figsize'] = (10.0, 8.0) # this controls figure size in the notebook
 
 
 
 
 
15
 
16
- import io
17
- import streamlit as st
18
- bytes_data=None
 
 
 
 
19
 
20
- Images = []
21
- classnames = []
22
- myList = os.listdir()
23
- #st.write(myList)
24
  for cls in myList:
25
- if os.path.splitext(cls)[1] == ".jpg" :
26
- curImg = cv2.imread(f'{cls}')
 
27
  Images.append(curImg)
28
  classnames.append(os.path.splitext(cls)[0])
29
- st.write(classnames)
 
30
 
 
 
 
 
 
31
 
32
- def findEncodings(Images):
33
- encodeList = []
34
- for img in Images:
35
- img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
36
- encode = face_recognition.face_encodings(img)[0]
37
- encodeList.append(encode)
38
- return encodeList
 
 
 
 
39
 
 
 
 
 
40
 
41
- encodeListknown = findEncodings(Images)
42
- st.write('Encoding Complete')
 
43
 
44
- img_file_buffer=st.camera_input("Take a picture")
45
- if img_file_buffer is not None:
46
-
47
- test_image = Image.open(img_file_buffer)
48
- st.image(test_image, use_column_width=True)
49
-
50
- image = np.asarray(test_image)
51
 
52
- #########################
53
- imgS = cv2.resize(image,(0,0),None,0.25,0.25)
54
- imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
55
- facesCurFrame = face_recognition.face_locations(imgS)
56
- encodesCurFrame = face_recognition.face_encodings(imgS,facesCurFrame)
57
-
58
- for encodeFace,faceLoc in zip(encodesCurFrame,facesCurFrame):
59
- matches = face_recognition.compare_faces(encodeListknown,encodeFace)
60
- faceDis = face_recognition.face_distance(encodeListknown,encodeFace)
61
- #print(faceDis)
62
- matchIndex = np.argmin(faceDis)
63
-
64
- if matches[matchIndex]:
65
- name = classnames[matchIndex].upper()
66
- st.write(name)
67
- y1, x2, y2, x1 = faceLoc
68
- y1, x2, y2, x1 = y1*4,x2*4,y2*4,x1*4
69
- cv2.rectangle(image,(x1,y1),(x2,y2),(0,255,0),2)
70
- cv2.rectangle(image,(x1,y2-35),(x2,y2),(0,255,0),cv2.FILLED)
71
- cv2.putText(image,name,(x1+6,y2-6),cv2.FONT_HERSHEY_COMPLEX,1,(255, 255, 255),2)
72
-
73
- ##############
74
- url = "https://rgiattendance.000webhostapp.com"
75
- url1 = "/update.php"
76
- data1 = {'name':name }
77
- response = requests.post(url+url1, data=data1)
78
-
79
- # # https://aimljul23f.glitch.me/fsave?nm=testname
80
- # url = "https://aimljul23f.glitch.me/"
81
- # url1 = "fsave?nm=" + name
82
- # # data1 = {'name':name }
83
- # response = requests.post(url+url1)
84
-
85
- if response.status_code == 200 :
86
- st.write(" data updated on : " + url)
87
- else : st.write("data NOT updated " + url+url1)
88
-
89
- ##############################
90
- st.image(image)
91
- if bytes_data is None:
92
- st.stop()
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
  from PIL import Image
2
  import numpy as np
3
+ import cv2
4
  import requests
 
5
  import face_recognition
6
  import os
7
+ import streamlit as st
8
 
9
+ # Set page title and description
10
+ st.set_page_config(
11
+ page_title="Aadhaar Based Face Recognition Attendance System",
12
+ page_icon="📷",
13
+ layout="centered",
14
+ initial_sidebar_state="collapsed"
15
+ )
16
+ st.title("Attendance System Using Face Recognition 📷")
17
+ st.markdown("This app recognizes faces in an image, verifies Aadhaar card details, and updates attendance records with the current timestamp.")
18
 
19
+ # Load images for face recognition
20
+ Images = [] # List to store Images
21
+ classnames = [] # List to store classnames
22
+ aadhar_numbers = [] # List to store Aadhaar numbers
23
+
24
+ directory = "photos"
25
+ myList = os.listdir(directory)
26
 
 
 
 
 
27
  for cls in myList:
28
+ if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
29
+ img_path = os.path.join(directory, cls)
30
+ curImg = cv2.imread(img_path)
31
  Images.append(curImg)
32
  classnames.append(os.path.splitext(cls)[0])
33
+ # Assume Aadhaar number is part of the image filename (e.g., "123456_john.jpg")
34
+ aadhar_numbers.append(cls.split('_')[0])
35
 
36
+ # Function to validate Aadhaar card number
37
+ def validate_aadhaar(aadhaar):
38
+ # Implement your Aadhaar card validation logic here
39
+ # For simplicity, let's assume any 6-digit number is a valid Aadhaar card
40
+ return len(aadhaar) == 6 and aadhaar.isdigit()
41
 
42
+ # Function to update Aadhaar data
43
+ def update_data(name, aadhaar_number):
44
+ url = "https://attendanceviaface.000webhostapp.com"
45
+ url1 = "/update.php"
46
+ data = {'name': name, 'aadhaar': aadhaar_number}
47
+ response = requests.post(url + url1, data=data)
48
+
49
+ if response.status_code == 200:
50
+ st.success("Data updated on: " + url)
51
+ else:
52
+ st.warning("Data not updated")
53
 
54
+ # Function to display image with overlay
55
+ def display_image_with_overlay(image, name):
56
+ # Add overlay to the image (e.g., bounding box and name)
57
+ # ...
58
 
59
+ # Apply styling with CSS
60
+ st.markdown('<style>img { animation: pulse 2s infinite; }</style>', unsafe_allow_html=True)
61
+ st.image(image, use_column_width=True, output_format="PNG")
62
 
63
+ # Take input Aadhaar card details
64
+ aadhaar_number = st.text_input("Enter your Last 6-digits Aadhaar Number:")
 
 
 
 
 
65
 
66
+ # Take picture using the camera
67
+ img_file_buffer = st.camera_input("Take a picture")
68
+
69
+ # Load images for face recognition
70
+ encodeListknown = [face_recognition.face_encodings(img)[0] for img in Images]
71
+
72
+ if img_file_buffer is not None:
73
+ # Validate Aadhaar card number
74
+ if validate_aadhaar(aadhaar_number):
75
+ test_image = Image.open(img_file_buffer)
76
+ image = np.asarray(test_image)
77
+
78
+ imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
79
+ imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
80
+ facesCurFrame = face_recognition.face_locations(imgS)
81
+ encodesCurFrame = face_recognition.face_encodings(imgS, facesCurFrame)
82
+
83
+ name = "Unknown" # Default name for unknown faces
84
+ match_found = False # Flag to track if a match is found
85
+
86
+ # Checking if faces are detected
87
+ if len(encodesCurFrame) > 0:
88
+ for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
89
+ # Assuming that encodeListknown is defined and populated in your code
90
+ matches = face_recognition.compare_faces(encodeListknown, encodeFace)
91
+ faceDis = face_recognition.face_distance(encodeListknown, encodeFace)
92
+ matchIndex = np.argmin(faceDis)
93
+
94
+ if matches[matchIndex]:
95
+ name = classnames[matchIndex].upper()
96
+
97
+ # Check if Aadhaar number is found in the database
98
+ if aadhaar_number not in aadhar_numbers:
99
+ st.error("Face recognized, but Aadhaar number not found in the database.")
100
+ else:
101
+ # Update data only if a known face is detected and Aadhaar number is valid
102
+ update_data(name, aadhaar_number)
103
+ match_found = True # Set the flag to True
104
+
105
+ else:
106
+ # Face recognized, but not matched with Aadhaar number
107
+ st.error("Face recognized, but Aadhaar number does not match.")
108
+
109
+ y1, x2, y2, x1 = faceLoc
110
+ y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
111
+ cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
112
+ cv2.rectangle(image, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
113
+ cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
114
+
115
+ display_image_with_overlay(image, name)
116
+
117
+ # Display the name corresponding to the entered Aadhaar number
118
+ if not match_found:
119
+ # Match Aadhaar number with the list
120
+ aadhar_index = aadhar_numbers.index(aadhaar_number) if aadhaar_number in aadhar_numbers else None
121
+ if aadhar_index is not None:
122
+ st.success(f"Match found: {classnames[aadhar_index]}")
123
+ else:
124
+ st.warning("Face not detected, and Aadhaar number not found in the database.")
125
+ else:
126
+ st.success(f"Face recognized: {name}")
127
+
128
+ else:
129
+ st.warning("No faces detected in the image. Face recognition failed.")
130
+
131
+ else:
132
+ st.error("Invalid Aadhaar card number. Please enter a valid 6-digit Aadhaar number.")