Spaces:
Runtime error
Runtime error
Update app.py
Browse files
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
|
| 9 |
mp_face_detection = mp.solutions.face_detection
|
| 10 |
mp_drawing = mp.solutions.drawing_utils
|
| 11 |
|
| 12 |
-
# Function to
|
| 13 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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 |
-
|
|
|
|
| 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
|
| 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
|
| 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
|
| 42 |
face_output_path = os.path.join(output_folder, f"face_{idx+1}.jpg")
|
| 43 |
cv2.imwrite(face_output_path, face_image)
|
| 44 |
-
|
| 45 |
-
|
| 46 |
-
|
| 47 |
-
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 56 |
|
| 57 |
if uploaded_file is not None:
|
| 58 |
-
# Convert
|
| 59 |
file_bytes = np.asarray(bytearray(uploaded_file.read()), dtype=np.uint8)
|
| 60 |
image = cv2.imdecode(file_bytes, 1)
|
| 61 |
-
|
| 62 |
-
#
|
| 63 |
output_folder = "output"
|
| 64 |
-
detected_image, face_images =
|
| 65 |
-
|
| 66 |
-
# Display
|
| 67 |
-
st.subheader("
|
| 68 |
-
st.image(cv2.cvtColor(detected_image, cv2.COLOR_BGR2RGB),
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
| 72 |
if face_images:
|
| 73 |
-
|
| 74 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 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.")
|