LovnishVerma commited on
Commit
d81f3c7
1 Parent(s): d5985cd

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +38 -22
app.py CHANGED
@@ -6,7 +6,7 @@ import face_recognition
6
  import os
7
  from datetime import datetime
8
  import streamlit as st
9
- from streamlit_webrtc import VideoTransformerBase, webrtc_streamer
10
 
11
  # Set page title and description
12
  st.set_page_config(
@@ -16,7 +16,21 @@ st.set_page_config(
16
  initial_sidebar_state="collapsed"
17
  )
18
  st.title("Attendance System Using Face Recognition 📷")
19
- st.markdown("This app recognizes faces in a live camera feed, verifies Aadhaar card details, and updates attendance records with the current timestamp & Location.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
20
 
21
  # Load images for face recognition
22
  Images = []
@@ -48,23 +62,16 @@ def validate_aadhaar(aadhaar):
48
  # For simplicity, let's assume any 4-digit number is a valid Aadhaar card
49
  return len(aadhaar) == 4 and aadhaar.isdigit()
50
 
51
- # Create a Streamlit component to capture images from the camera
52
- webrtc_ctx = webrtc_streamer(
53
- key="example",
54
- video_transformer_factory=None, # No need for video transformation for this example
55
- async_transform=True
56
- )
57
-
58
- # Main Streamlit app logic
59
- if webrtc_ctx.video_transformer:
60
- st.sidebar.markdown("# Capture Image")
61
- aadhaar_number = st.sidebar.text_input("Enter Aadhaar Number:")
62
-
63
- # Use the camera feed to capture images
64
- frame = webrtc_ctx.video_transformer.get_frame()
65
 
66
- if frame is not None:
67
- image = np.array(frame.to_image())
 
 
 
 
68
 
69
  imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
70
  imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
@@ -89,7 +96,8 @@ if webrtc_ctx.video_transformer:
89
  cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
90
 
91
  if name != "Unknown":
92
- url = "https://mrvishal7705.000webhostapp.com"
 
93
  url1 = "/update.php"
94
  data1 = {'name': name, 'aadhaar': aadhaar_number}
95
  response = requests.post(url + url1, data=data1)
@@ -99,11 +107,19 @@ if webrtc_ctx.video_transformer:
99
  else:
100
  st.warning("Data not updated")
101
 
102
- # Display the result
 
 
 
 
 
103
  st.markdown('<style>img { animation: pulse 2s infinite; }</style>', unsafe_allow_html=True)
104
  st.image(image, use_column_width=True, output_format="PNG")
105
 
106
  if name == "Unknown":
107
  st.info("Face not detected. Please try again.")
108
- else:
109
- st.warning("Webcam not available. Please make sure your camera is connected and accessible.")
 
 
 
 
6
  import os
7
  from datetime import datetime
8
  import streamlit as st
9
+ import sqlite3
10
 
11
  # Set page title and description
12
  st.set_page_config(
 
16
  initial_sidebar_state="collapsed"
17
  )
18
  st.title("Attendance System Using Face Recognition 📷")
19
+ st.markdown("This app recognizes faces in an image, verifies Aadhaar card details, and updates attendance records with the current timestamp & Location.")
20
+
21
+ # Create or connect to the SQLite database
22
+ conn = sqlite3.connect("attendance_database.db")
23
+ cursor = conn.cursor()
24
+
25
+ # Check if the 'faces' table exists, create it if not
26
+ cursor.execute('''
27
+ CREATE TABLE IF NOT EXISTS faces (
28
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
29
+ aadhaar TEXT,
30
+ encoding BLOB
31
+ )
32
+ ''')
33
+ conn.commit()
34
 
35
  # Load images for face recognition
36
  Images = []
 
62
  # For simplicity, let's assume any 4-digit number is a valid Aadhaar card
63
  return len(aadhaar) == 4 and aadhaar.isdigit()
64
 
65
+ # Take picture using the camera and input Aadhaar card details
66
+ img_file_buffer = st.file_uploader("Upload an image", type=["jpg", "jpeg"])
67
+ aadhaar_number = st.text_input("Enter Aadhaar Number:")
 
 
 
 
 
 
 
 
 
 
 
68
 
69
+ # Face recognition code...
70
+ if img_file_buffer is not None:
71
+ # Validate Aadhaar card number
72
+ if validate_aadhaar(aadhaar_number):
73
+ test_image = Image.open(img_file_buffer)
74
+ image = np.asarray(test_image)
75
 
76
  imgS = cv2.resize(image, (0, 0), None, 0.25, 0.25)
77
  imgS = cv2.cvtColor(imgS, cv2.COLOR_BGR2RGB)
 
96
  cv2.putText(image, name, (x1 + 6, y2 - 6), cv2.FONT_HERSHEY_COMPLEX, 1, (255, 255, 255), 2)
97
 
98
  if name != "Unknown":
99
+ # Update Aadhaar data
100
+ url = "https://attendanceviaface.000webhostapp.com"
101
  url1 = "/update.php"
102
  data1 = {'name': name, 'aadhaar': aadhaar_number}
103
  response = requests.post(url + url1, data=data1)
 
107
  else:
108
  st.warning("Data not updated")
109
 
110
+ # Store face encoding and Aadhaar number in the database
111
+ face_encoding_bytes = pickle.dumps(encodeFace)
112
+ cursor.execute("INSERT INTO faces (aadhaar, encoding) VALUES (?, ?)", (aadhaar_number, face_encoding_bytes))
113
+ conn.commit()
114
+
115
+ # Apply styling with CSS
116
  st.markdown('<style>img { animation: pulse 2s infinite; }</style>', unsafe_allow_html=True)
117
  st.image(image, use_column_width=True, output_format="PNG")
118
 
119
  if name == "Unknown":
120
  st.info("Face not detected. Please try again.")
121
+ else:
122
+ st.error("Invalid Aadhaar card number. Please enter a valid 4-digit Aadhaar number.")
123
+
124
+ # Close the database connection
125
+ conn.close()