Spaces:
Sleeping
Sleeping
Commit ·
c5aeb39
1
Parent(s): c1ac3cd
Update app.py
Browse files
app.py
CHANGED
|
@@ -34,28 +34,32 @@ cursor.execute('''
|
|
| 34 |
conn.commit()
|
| 35 |
|
| 36 |
# Load images for face recognition
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
myList = os.listdir(directory)
|
| 42 |
-
|
| 43 |
-
for cls in myList:
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 53 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 54 |
encode = face_recognition.face_encodings(img)[0]
|
| 55 |
-
|
| 56 |
-
return
|
| 57 |
|
| 58 |
-
|
| 59 |
|
| 60 |
# Function to validate Aadhaar card number
|
| 61 |
def validate_aadhaar(aadhaar):
|
|
@@ -63,70 +67,62 @@ def validate_aadhaar(aadhaar):
|
|
| 63 |
# For simplicity, let's assume any 4-digit number is a valid Aadhaar card
|
| 64 |
return len(aadhaar) == 4 and aadhaar.isdigit()
|
| 65 |
|
| 66 |
-
#
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
st.success("Data updated on: " + url)
|
| 108 |
-
else:
|
| 109 |
-
st.warning("Data not updated")
|
| 110 |
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
|
| 116 |
-
|
| 117 |
-
|
| 118 |
-
|
| 119 |
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
else:
|
| 123 |
-
st.error("Invalid Aadhaar card number. Please enter a valid 4-digit Aadhaar number.")
|
| 124 |
-
|
| 125 |
-
# Section to take attendance by clicking a photo
|
| 126 |
-
st.header("Take Attendance by Clicking Photo")
|
| 127 |
-
capture_button = st.button("Capture Photo for Attendance")
|
| 128 |
|
| 129 |
-
|
|
|
|
| 130 |
# Capture photo using the camera
|
| 131 |
captured_image = st.camera_input("Capture a photo for attendance")
|
| 132 |
|
|
@@ -143,30 +139,44 @@ if capture_button:
|
|
| 143 |
if len(faces_captured) > 0:
|
| 144 |
# Check if any of the captured faces match known faces
|
| 145 |
for encode_captured, face_loc in zip(encodes_captured, faces_captured):
|
| 146 |
-
matches_captured = face_recognition.compare_faces(
|
| 147 |
-
face_dis_captured = face_recognition.face_distance(
|
| 148 |
match_index_captured = np.argmin(face_dis_captured)
|
| 149 |
|
| 150 |
if matches_captured[match_index_captured]:
|
| 151 |
name_captured = classnames[match_index_captured].upper()
|
| 152 |
|
| 153 |
-
#
|
| 154 |
-
|
| 155 |
-
user_data = cursor.fetchone()
|
| 156 |
-
|
| 157 |
-
if user_data:
|
| 158 |
-
st.warning(f"{name_captured} is already present in the database.")
|
| 159 |
-
else:
|
| 160 |
-
# Store face encoding and Aadhaar number in the database
|
| 161 |
-
face_encoding_bytes_captured = pickle.dumps(encode_captured)
|
| 162 |
-
cursor.execute("INSERT INTO faces (aadhaar, encoding) VALUES (?, ?)", (aadhaar_number, face_encoding_bytes_captured))
|
| 163 |
-
conn.commit()
|
| 164 |
st.success(f"{name_captured}'s attendance recorded.")
|
|
|
|
|
|
|
| 165 |
else:
|
| 166 |
st.warning("No matching face found in the database.")
|
| 167 |
|
| 168 |
else:
|
| 169 |
st.warning("No face detected in the captured photo. Please try again.")
|
| 170 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 171 |
# Close the database connection
|
| 172 |
conn.close()
|
|
|
|
| 34 |
conn.commit()
|
| 35 |
|
| 36 |
# Load images for face recognition
|
| 37 |
+
def load_images(directory):
|
| 38 |
+
Images = []
|
| 39 |
+
classnames = []
|
| 40 |
+
|
| 41 |
+
myList = os.listdir(directory)
|
| 42 |
+
|
| 43 |
+
for cls in myList:
|
| 44 |
+
if os.path.splitext(cls)[1] in [".jpg", ".jpeg"]:
|
| 45 |
+
img_path = os.path.join(directory, cls)
|
| 46 |
+
curImg = cv2.imread(img_path)
|
| 47 |
+
Images.append(curImg)
|
| 48 |
+
classnames.append(os.path.splitext(cls)[0])
|
| 49 |
+
|
| 50 |
+
return Images, classnames
|
| 51 |
+
|
| 52 |
+
Images, classnames = load_images("photos")
|
| 53 |
+
|
| 54 |
+
def find_encodings(images):
|
| 55 |
+
encode_list = []
|
| 56 |
+
for img in images:
|
| 57 |
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
|
| 58 |
encode = face_recognition.face_encodings(img)[0]
|
| 59 |
+
encode_list.append(encode)
|
| 60 |
+
return encode_list
|
| 61 |
|
| 62 |
+
encodeListKnown = find_encodings(Images)
|
| 63 |
|
| 64 |
# Function to validate Aadhaar card number
|
| 65 |
def validate_aadhaar(aadhaar):
|
|
|
|
| 67 |
# For simplicity, let's assume any 4-digit number is a valid Aadhaar card
|
| 68 |
return len(aadhaar) == 4 and aadhaar.isdigit()
|
| 69 |
|
| 70 |
+
# Function to verify Aadhaar from the database
|
| 71 |
+
def verify_aadhaar_from_database(aadhaar_number):
|
| 72 |
+
cursor.execute("SELECT * FROM faces WHERE aadhaar = ?", (aadhaar_number,))
|
| 73 |
+
user_data = cursor.fetchone()
|
| 74 |
+
return user_data is not None
|
| 75 |
+
|
| 76 |
+
# Function to perform face recognition and Aadhaar verification
|
| 77 |
+
def recognize_and_verify(image, encodeListKnown, aadhaar_number):
|
| 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 |
+
|
| 85 |
+
if len(encodesCurFrame) > 0:
|
| 86 |
+
for encodeFace, faceLoc in zip(encodesCurFrame, facesCurFrame):
|
| 87 |
+
matches = face_recognition.compare_faces(encodeListKnown, encodeFace)
|
| 88 |
+
faceDis = face_recognition.face_distance(encodeListKnown, encodeFace)
|
| 89 |
+
matchIndex = np.argmin(faceDis)
|
| 90 |
+
|
| 91 |
+
if matches[matchIndex]:
|
| 92 |
+
name = classnames[matchIndex].upper()
|
| 93 |
+
|
| 94 |
+
y1, x2, y2, x1 = faceLoc
|
| 95 |
+
y1, x2, y2, x1 = y1 * 4, x2 * 4, y2 * 4, x1 * 4
|
| 96 |
+
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 255, 0), 2)
|
| 97 |
+
cv2.rectangle(image, (x1, y2 - 35), (x2, y2), (0, 255, 0), cv2.FILLED)
|
| 98 |
+
cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
|
| 99 |
+
|
| 100 |
+
if name != "Unknown":
|
| 101 |
+
# Update Aadhaar data
|
| 102 |
+
url = "https://attendanceviaface.000webhostapp.com"
|
| 103 |
+
url1 = "/update.php"
|
| 104 |
+
data1 = {'name': name, 'aadhaar': aadhaar_number}
|
| 105 |
+
response = requests.post(url + url1, data=data1)
|
| 106 |
+
|
| 107 |
+
if response.status_code == 200:
|
| 108 |
+
st.success("Data updated on: " + url)
|
| 109 |
+
else:
|
| 110 |
+
st.warning("Data not updated")
|
|
|
|
|
|
|
|
|
|
| 111 |
|
| 112 |
+
# Store face encoding and Aadhaar number in the database
|
| 113 |
+
face_encoding_bytes = pickle.dumps(encodeFace)
|
| 114 |
+
cursor.execute("INSERT INTO faces (aadhaar, encoding) VALUES (?, ?)", (aadhaar_number, face_encoding_bytes))
|
| 115 |
+
conn.commit()
|
| 116 |
|
| 117 |
+
# Apply styling with CSS
|
| 118 |
+
st.markdown('<style>img { animation: pulse 2s infinite; }</style>', unsafe_allow_html=True)
|
| 119 |
+
st.image(image, use_column_width=True, output_format="PNG")
|
| 120 |
|
| 121 |
+
if name == "Unknown":
|
| 122 |
+
st.info("Face not detected. Please try again.")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 123 |
|
| 124 |
+
# Function to take attendance by clicking a photo
|
| 125 |
+
def take_attendance_by_photo(encodeListKnown, aadhaar_number):
|
| 126 |
# Capture photo using the camera
|
| 127 |
captured_image = st.camera_input("Capture a photo for attendance")
|
| 128 |
|
|
|
|
| 139 |
if len(faces_captured) > 0:
|
| 140 |
# Check if any of the captured faces match known faces
|
| 141 |
for encode_captured, face_loc in zip(encodes_captured, faces_captured):
|
| 142 |
+
matches_captured = face_recognition.compare_faces(encodeListKnown, encode_captured)
|
| 143 |
+
face_dis_captured = face_recognition.face_distance(encodeListKnown, encode_captured)
|
| 144 |
match_index_captured = np.argmin(face_dis_captured)
|
| 145 |
|
| 146 |
if matches_captured[match_index_captured]:
|
| 147 |
name_captured = classnames[match_index_captured].upper()
|
| 148 |
|
| 149 |
+
# Verify Aadhaar from the database
|
| 150 |
+
if verify_aadhaar_from_database(aadhaar_number):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 151 |
st.success(f"{name_captured}'s attendance recorded.")
|
| 152 |
+
else:
|
| 153 |
+
st.warning(f"{name_captured} is not registered with Aadhaar {aadhaar_number}.")
|
| 154 |
else:
|
| 155 |
st.warning("No matching face found in the database.")
|
| 156 |
|
| 157 |
else:
|
| 158 |
st.warning("No face detected in the captured photo. Please try again.")
|
| 159 |
|
| 160 |
+
# Take picture using the camera and input Aadhaar card details
|
| 161 |
+
img_file_buffer = st.file_uploader("Upload an image", type=["jpg", "jpeg"])
|
| 162 |
+
aadhaar_number = st.text_input("Enter Aadhaar Number:")
|
| 163 |
+
|
| 164 |
+
# Face recognition and Aadhaar verification code...
|
| 165 |
+
if img_file_buffer is not None:
|
| 166 |
+
# Validate Aadhaar card number
|
| 167 |
+
if validate_aadhaar(aadhaar_number):
|
| 168 |
+
test_image = Image.open(img_file_buffer)
|
| 169 |
+
image = np.asarray(test_image)
|
| 170 |
+
recognize_and_verify(image, encodeListKnown, aadhaar_number)
|
| 171 |
+
else:
|
| 172 |
+
st.error("Invalid Aadhaar card number. Please enter a valid 4-digit Aadhaar number.")
|
| 173 |
+
|
| 174 |
+
# Section to take attendance by clicking a photo
|
| 175 |
+
st.header("Take Attendance by Clicking Photo")
|
| 176 |
+
capture_button = st.button("Capture Photo for Attendance")
|
| 177 |
+
|
| 178 |
+
if capture_button:
|
| 179 |
+
take_attendance_by_photo(encodeListKnown, aadhaar_number)
|
| 180 |
+
|
| 181 |
# Close the database connection
|
| 182 |
conn.close()
|