Rasleen commited on
Commit
fd4d6b9
Β·
verified Β·
1 Parent(s): a12e470

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +47 -0
app.py CHANGED
@@ -17,6 +17,7 @@ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
17
  IMAGE_SHAPE = 640
18
  data_path = 'employees'
19
  webcam_path = 'captured_image.jpg'
 
20
 
21
  # Set Streamlit title
22
  st.title("AIML-Student Attendance System")
@@ -28,6 +29,26 @@ image_paths = glob(os.path.join(data_path, '*.jpg'))
28
  app = FaceAnalysis(name="buffalo_l") # ArcFace model
29
  app.prepare(ctx_id=0 if torch.cuda.is_available() else -1, det_size=(IMAGE_SHAPE, IMAGE_SHAPE))
30
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
31
  # Define function to match face embeddings
32
  def prod_function(app, prod_path, webcam_img_pil):
33
  np_webcam = np.array(webcam_img_pil)
@@ -126,3 +147,29 @@ with app_tab:
126
  # Convert back to RGB for display
127
  image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
128
  st.image(image_rgb, caption="Detected Faces", use_container_width=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  IMAGE_SHAPE = 640
18
  data_path = 'employees'
19
  webcam_path = 'captured_image.jpg'
20
+ STUDENT_DB = "students.db"
21
 
22
  # Set Streamlit title
23
  st.title("AIML-Student Attendance System")
 
29
  app = FaceAnalysis(name="buffalo_l") # ArcFace model
30
  app.prepare(ctx_id=0 if torch.cuda.is_available() else -1, det_size=(IMAGE_SHAPE, IMAGE_SHAPE))
31
 
32
+ # Check if roll exists
33
+ def roll_exists(rno):
34
+ with sqlite3.connect(STUDENT_DB) as conn:
35
+ cur = conn.execute("SELECT * FROM students WHERE rno=?", (rno,))
36
+ return cur.fetchone()
37
+
38
+ # Register new student
39
+ def register_student(rno, sname, sclass, image):
40
+ image_path = os.path.join(data_path, f"{rno}.jpg")
41
+ image.save(image_path)
42
+ with sqlite3.connect(STUDENT_DB) as conn:
43
+ conn.execute("INSERT INTO students (rno, sname, sclass, image_path) VALUES (?, ?, ?, ?)",
44
+ (rno, sname, sclass, image_path))
45
+
46
+
47
+ # Load all registered images
48
+ def load_registered_images():
49
+ with sqlite3.connect(STUDENT_DB) as conn:
50
+ return [row[3] for row in conn.execute("SELECT * FROM students")]
51
+
52
  # Define function to match face embeddings
53
  def prod_function(app, prod_path, webcam_img_pil):
54
  np_webcam = np.array(webcam_img_pil)
 
147
  # Convert back to RGB for display
148
  image_rgb = cv2.cvtColor(image_bgr, cv2.COLOR_BGR2RGB)
149
  st.image(image_rgb, caption="Detected Faces", use_container_width=True)
150
+
151
+ with register_tab:
152
+ upload_mode = st.radio("Choose Method", ["πŸ“€ Upload Image", "πŸ“· Take Photo"])
153
+ if upload_mode == "πŸ“€ Upload Image":
154
+ upload = st.file_uploader("Upload Student Image with plain background(.jpg)", type="jpg")
155
+ if upload:
156
+ image_pil = Image.open(upload)
157
+ else:
158
+ camera_capture = st.camera_input("Capture Image")
159
+ if camera_capture:
160
+ image_pil = Image.open(camera_capture)
161
+
162
+ if 'image_pil' in locals():
163
+ st.image(image_pil, caption="Student Image", use_container_width=True)
164
+ rno = st.text_input("Roll Number")
165
+ sname = st.text_input("Student Name")
166
+ sclass = st.text_input("Class")
167
+
168
+ if st.button("Register"):
169
+ if not (rno and sname and sclass):
170
+ st.warning("Fill all details.")
171
+ elif roll_exists(rno):
172
+ st.error("Student with this roll_no registered already.")
173
+ else:
174
+ register_student(rno, sname, sclass, image_pil)
175
+ st.success("Student registered successfully.")