Akshayram1 commited on
Commit
0152ef9
·
verified ·
1 Parent(s): 753a29c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +126 -39
app.py CHANGED
@@ -4,73 +4,160 @@ import mediapipe as mp
4
  import streamlit as st
5
  from PIL import Image
6
  import numpy as np
 
 
 
7
 
8
- # Initialize the MediaPipe Face Detection and Drawing utilities
9
  mp_face_detection = mp.solutions.face_detection
10
  mp_drawing = mp.solutions.drawing_utils
11
 
12
- # Function to detect faces and save each detected face as an image
13
- def detect_and_save_faces(image, output_folder="output"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
14
  if not os.path.exists(output_folder):
15
  os.makedirs(output_folder)
16
-
17
  # Convert image to RGB for MediaPipe
18
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
19
-
20
  # Detect faces
21
  with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
22
  results = face_detection.process(image_rgb)
23
-
24
  face_images = []
25
- # Draw face detections and save each face
 
26
  if results.detections:
 
 
27
  for idx, detection in enumerate(results.detections):
28
  bboxC = detection.location_data.relative_bounding_box
29
  h, w, _ = image.shape
30
-
31
- # Get the bounding box coordinates
32
  x_min = int(bboxC.xmin * w)
33
  y_min = int(bboxC.ymin * h)
34
  box_width = int(bboxC.width * w)
35
  box_height = int(bboxC.height * h)
36
-
37
- # Extract the face from the image
38
  face_image = image[y_min:y_min + box_height, x_min:x_min + box_width]
39
  face_images.append(face_image)
40
-
41
- # Save the detected face
42
  face_output_path = os.path.join(output_folder, f"face_{idx+1}.jpg")
43
  cv2.imwrite(face_output_path, face_image)
44
-
45
- # Draw bounding boxes on the original image
46
- for detection in results.detections:
47
- mp_drawing.draw_detection(image, detection)
48
-
49
- return image, face_images
50
-
51
- # Streamlit UI for face detection
52
- st.title("Face Detection and Extraction")
53
-
54
- # File uploader
55
- uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  if uploaded_file is not None:
58
- # Convert the uploaded file to an OpenCV image
59
  file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
60
  image = cv2.imdecode(file_bytes, 1)
61
-
62
- # Run face detection and save faces
63
  output_folder = "output"
64
- detected_image, face_images = detect_and_save_faces(image, output_folder)
65
-
66
- # Display the original image with detected faces
67
- st.subheader("Original Image with Detected Faces")
68
- st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB), caption="Detected Faces", use_column_width=True)
69
-
70
- # Display the extracted faces
71
- st.subheader("Extracted Faces")
 
72
  if face_images:
73
- for idx, face in enumerate(face_images):
74
- st.image(cv2.cvtColor(face, cv2.COLOR_BGR2RGB), caption=f"Face {idx+1}", use_column_width=False)
 
 
 
 
 
75
  else:
76
- st.write("No faces detected.")
 
 
 
 
 
 
 
 
 
4
  import streamlit as st
5
  from PIL import Image
6
  import numpy as np
7
+ from deepface import DeepFace
8
+ import pandas as pd
9
+ from datetime import datetime
10
 
11
+ # Initialize MediaPipe
12
  mp_face_detection = mp.solutions.face_detection
13
  mp_drawing = mp.solutions.drawing_utils
14
 
15
+ # Function to create user database directory if it doesn't exist
16
+ def initialize_database():
17
+ if not os.path.exists("database"):
18
+ os.makedirs("database")
19
+ if not os.path.exists("database/records.csv"):
20
+ df = pd.DataFrame(columns=['name', 'image_path', 'date_added'])
21
+ df.to_csv("database/records.csv", index=False)
22
+
23
+ # Function to add face to database
24
+ def add_to_database(image, name):
25
+ initialize_database()
26
+
27
+ # Save image to database folder
28
+ timestamp = datetime.now().strftime("%Y%m%d_%H%M%S")
29
+ image_path = f"database/{name}_{timestamp}.jpg"
30
+ cv2.imwrite(image_path, image)
31
+
32
+ # Update records
33
+ df = pd.read_csv("database/records.csv")
34
+ new_record = pd.DataFrame({
35
+ 'name': [name],
36
+ 'image_path': [image_path],
37
+ 'date_added': [datetime.now().strftime("%Y-%m-%d %H:%M:%S")]
38
+ })
39
+ df = pd.concat([df, new_record], ignore_index=True)
40
+ df.to_csv("database/records.csv", index=False)
41
+ return image_path
42
+
43
+ # Function to detect faces and perform recognition
44
+ def detect_and_recognize_faces(image, output_folder="output"):
45
  if not os.path.exists(output_folder):
46
  os.makedirs(output_folder)
47
+
48
  # Convert image to RGB for MediaPipe
49
  image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
50
+
51
  # Detect faces
52
  with mp_face_detection.FaceDetection(min_detection_confidence=0.5) as face_detection:
53
  results = face_detection.process(image_rgb)
 
54
  face_images = []
55
+ face_results = []
56
+
57
  if results.detections:
58
+ df = pd.read_csv("database/records.csv")
59
+
60
  for idx, detection in enumerate(results.detections):
61
  bboxC = detection.location_data.relative_bounding_box
62
  h, w, _ = image.shape
63
+
64
+ # Get coordinates
65
  x_min = int(bboxC.xmin * w)
66
  y_min = int(bboxC.ymin * h)
67
  box_width = int(bboxC.width * w)
68
  box_height = int(bboxC.height * h)
69
+
70
+ # Extract face
71
  face_image = image[y_min:y_min + box_height, x_min:x_min + box_width]
72
  face_images.append(face_image)
73
+
74
+ # Save detected face
75
  face_output_path = os.path.join(output_folder, f"face_{idx+1}.jpg")
76
  cv2.imwrite(face_output_path, face_image)
77
+
78
+ # Perform face recognition if database is not empty
79
+ if not df.empty:
80
+ try:
81
+ matches = []
82
+ for _, row in df.iterrows():
83
+ try:
84
+ result = DeepFace.verify(
85
+ img1_path=face_output_path,
86
+ img2_path=row['image_path'],
87
+ model_name='VGG-Face',
88
+ distance_metric='cosine'
89
+ )
90
+ if result['verified']:
91
+ matches.append((row['name'], result['distance']))
92
+ except Exception as e:
93
+ continue
94
+
95
+ if matches:
96
+ # Get the best match (lowest distance)
97
+ best_match = min(matches, key=lambda x: x[1])
98
+ face_results.append(f"Match found: {best_match[0]}")
99
+ # Draw name on image
100
+ cv2.putText(image, best_match[0], (x_min, y_min - 10),
101
+ cv2.FONT_HERSHEY_SIMPLEX, 0.9, (36,255,12), 2)
102
+ else:
103
+ face_results.append("No match found")
104
+ except Exception as e:
105
+ face_results.append(f"Recognition error: {str(e)}")
106
+
107
+ # Draw detection box
108
+ mp_drawing.draw_detection(image, detection)
109
+
110
+ return image, face_images, face_results
111
+
112
+ # Streamlit UI
113
+ st.title("Face Recognition System")
114
+
115
+ # Sidebar for database management
116
+ st.sidebar.header("Database Management")
117
+ upload_for_db = st.sidebar.file_uploader("Add face to database", type=["jpg", "jpeg", "png"])
118
+ if upload_for_db:
119
+ person_name = st.sidebar.text_input("Enter person's name")
120
+ if st.sidebar.button("Add to Database") and person_name:
121
+ file_bytes = np.asarray(bytearray(upload_for_db.read()), dtype=np.uint8)
122
+ img = cv2.imdecode(file_bytes, 1)
123
+ image_path = add_to_database(img, person_name)
124
+ st.sidebar.success(f"Added {person_name} to database!")
125
+
126
+ # Main interface for face detection and recognition
127
+ st.header("Face Detection and Recognition")
128
+ uploaded_file = st.file_uploader("Choose an image for recognition", type=["jpg", "jpeg", "png"])
129
 
130
  if uploaded_file is not None:
131
+ # Convert uploaded file to image
132
  file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
133
  image = cv2.imdecode(file_bytes, 1)
134
+
135
+ # Process image
136
  output_folder = "output"
137
+ detected_image, face_images, face_results = detect_and_recognize_faces(image, output_folder)
138
+
139
+ # Display results
140
+ st.subheader("Results")
141
+ st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB),
142
+ caption="Detected and Recognized Faces",
143
+ use_column_width=True)
144
+
145
+ # Display extracted faces and recognition results
146
  if face_images:
147
+ cols = st.columns(len(face_images))
148
+ for idx, (face, result, col) in enumerate(zip(face_images, face_results, cols)):
149
+ with col:
150
+ st.image(cv2.cvtColor(face, cv2.COLOR_BGR2RGB),
151
+ caption=f"Face {idx+1}",
152
+ use_column_width=True)
153
+ st.write(result)
154
  else:
155
+ st.write("No faces detected.")
156
+
157
+ # Display database contents
158
+ if st.sidebar.checkbox("Show Database Contents"):
159
+ try:
160
+ df = pd.read_csv("database/records.csv")
161
+ st.sidebar.dataframe(df)
162
+ except:
163
+ st.sidebar.write("Database is empty or not initialized.")