ravinder2024 commited on
Commit
13a8e33
·
verified ·
1 Parent(s): 66ffbd1

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +64 -61
app.py CHANGED
@@ -1,78 +1,81 @@
1
- import face_recognition
2
- import cv2
3
  import numpy as np
 
 
 
 
 
4
 
5
- # Load and encode known faces
 
6
  known_face_encodings = []
7
  known_face_names = []
8
 
9
- image_of_person = face_recognition.load_image_file("Ravinder.jpeg")
10
- encoding_of_person = face_recognition.face_encodings(image_of_person)[0]
11
-
12
- known_face_encodings.append(encoding_of_person)
13
- known_face_names.append("Person's Name")
14
-
15
- video_capture = cv2.VideoCapture(0)
16
-
17
- while True:
18
- ret, frame = video_capture.read()
19
- rgb_frame = frame[:, :, ::-1]
20
- face_locations = face_recognition.face_locations(rgb_frame)
21
- face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
22
-
23
- for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
24
- matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
25
- name = "Unknown"
26
- if True in matches:
27
- match_index = matches.index(True)
28
- name = known_face_names[match_index]
29
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
30
- cv2.putText(frame, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
31
-
32
- from deepface import DeepFace
33
-
34
- def detect_emotion(face_image):
35
- result = DeepFace.analyze(face_image, actions=['emotion'], enforce_detection=False)
36
- return result['dominant_emotion']
37
- import sqlite3
38
 
39
- connection = sqlite3.connect("attendance.db")
 
40
  cursor = connection.cursor()
41
- cursor.execute('''CREATE TABLE IF NOT EXISTS Attendance
42
- (Name TEXT, Timestamp TEXT, Emotion TEXT)''')
43
-
44
- def log_attendance(name, emotion):
45
- from datetime import datetime
 
 
 
 
 
46
  timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
47
- cursor.execute("INSERT INTO Attendance (Name, Timestamp, Emotion) VALUES (?, ?, ?)",
48
- (name, timestamp, emotion))
49
  connection.commit()
50
 
51
- while True:
52
- ret, frame = video_capture.read()
53
- rgb_frame = frame[:, :, ::-1]
54
- face_locations = face_recognition.face_locations(rgb_frame)
55
- face_encodings = face_recognition.face_encodings(rgb_frame, face_locations)
56
-
 
57
  for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
58
  matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
59
  name = "Unknown"
 
60
  if True in matches:
61
  match_index = matches.index(True)
62
  name = known_face_names[match_index]
63
- face_image = frame[top:bottom, left:right]
64
- emotion = detect_emotion(face_image)
65
- log_attendance(name, emotion)
66
-
67
- cv2.rectangle(frame, (left, top), (right, bottom), (0, 255, 0), 2)
68
- cv2.putText(frame, f"{name} ({emotion})", (left, top - 10),
69
- cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 255, 255), 2)
70
-
71
- cv2.imshow('Video', frame)
72
-
73
- if cv2.waitKey(1) & 0xFF == ord('q'):
74
- break
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
 
76
- video_capture.release()
77
- cv2.destroyAllWindows()
78
- connection.close()
 
 
 
1
  import numpy as np
2
+ import cv2
3
+ import face_recognition
4
+ import sqlite3
5
+ import gradio as gr
6
+ from datetime import datetime
7
 
8
+ # Step 1: Initialize Face Recognition
9
+ # Load known faces and names
10
  known_face_encodings = []
11
  known_face_names = []
12
 
13
+ # Example: Add known faces
14
+ image_person1 = face_recognition.load_image_file("person1.jpg")
15
+ encoding_person1 = face_recognition.face_encodings(image_person1)[0]
16
+ known_face_encodings.append(encoding_person1)
17
+ known_face_names.append("Person 1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
+ # Step 2: Set Up Database
20
+ connection = sqlite3.connect("attendance.db", check_same_thread=False)
21
  cursor = connection.cursor()
22
+ cursor.execute('''
23
+ CREATE TABLE IF NOT EXISTS Attendance (
24
+ id INTEGER PRIMARY KEY AUTOINCREMENT,
25
+ name TEXT,
26
+ timestamp TEXT
27
+ )
28
+ ''')
29
+
30
+ # Function to log attendance
31
+ def log_attendance(name):
32
  timestamp = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
33
+ cursor.execute("INSERT INTO Attendance (name, timestamp) VALUES (?, ?)", (name, timestamp))
 
34
  connection.commit()
35
 
36
+ # Step 3: Detect and Recognize Faces
37
+ def detect_and_mark_attendance(image):
38
+ image_np = np.array(image)
39
+ rgb_image = cv2.cvtColor(image_np, cv2.COLOR_BGR2RGB)
40
+ face_locations = face_recognition.face_locations(rgb_image)
41
+ face_encodings = face_recognition.face_encodings(rgb_image, face_locations)
42
+
43
  for (top, right, bottom, left), face_encoding in zip(face_locations, face_encodings):
44
  matches = face_recognition.compare_faces(known_face_encodings, face_encoding)
45
  name = "Unknown"
46
+
47
  if True in matches:
48
  match_index = matches.index(True)
49
  name = known_face_names[match_index]
50
+ log_attendance(name) # Log to database
51
+
52
+ # Draw a rectangle around the face
53
+ cv2.rectangle(image_np, (left, top), (right, bottom), (255, 0, 0), 3)
54
+ cv2.putText(image_np, name, (left, top - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
55
+
56
+ return image_np
57
+
58
+ # Step 4: Fetch Attendance Logs
59
+ def fetch_attendance():
60
+ cursor.execute("SELECT * FROM Attendance")
61
+ rows = cursor.fetchall()
62
+ return rows
63
+
64
+ # Step 5: Gradio Interface
65
+ iface = gr.Interface(
66
+ fn=detect_and_mark_attendance,
67
+ inputs="image",
68
+ outputs="image",
69
+ title="Face Recognition and Attendance",
70
+ description="Upload an image to detect and mark attendance. Attendance will be saved to the database."
71
+ )
72
+
73
+ iface.add_component(
74
+ fn=fetch_attendance,
75
+ inputs=None,
76
+ outputs="dataframe",
77
+ label="Attendance Logs"
78
+ )
79
+
80
+ iface.launch()
81