Skip to content

Commit

Permalink
adds streamlit face-recognition example
Browse files Browse the repository at this point in the history
  • Loading branch information
edublancas committed Sep 14, 2024
1 parent e9284ed commit 56cb52e
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 0 deletions.
21 changes: 21 additions & 0 deletions examples/streamlit/streamlit-face-recognition/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
FROM python:3.11-slim

RUN apt-get update && apt install cmake g++ git -y \
&& rm -rf /var/lib/apt/lists/*

WORKDIR /app

COPY requirements.txt /app/requirements.txt
RUN pip install -r requirements.txt

COPY . /app

ENTRYPOINT ["streamlit", "run", "app.py", \
"--server.port=80", \
"--server.headless=true", \
"--server.address=0.0.0.0", \
"--browser.gatherUsageStats=false", \
"--server.enableStaticServing=true", \
"--server.fileWatcherType=none", \
# hide the Streamlit menu
"--client.toolbarMode=viewer"]
3 changes: 3 additions & 0 deletions examples/streamlit/streamlit-face-recognition/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Streamlit face recognition

An app that shows how to use `face_recognition`
61 changes: 61 additions & 0 deletions examples/streamlit/streamlit-face-recognition/app.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import streamlit as st
import face_recognition
from PIL import Image, ImageDraw
import numpy as np


def detect_faces(image):
# Convert the image to RGB (face_recognition requires RGB)
rgb_image = image.convert("RGB")

# Convert PIL Image to numpy array
np_image = np.array(rgb_image)

# Find all face locations and face encodings in the image
face_locations = face_recognition.face_locations(np_image)

# Create a copy of the image to draw on
image_with_faces = image.copy()
draw = ImageDraw.Draw(image_with_faces)

# Draw rectangles around detected faces
for face_location in face_locations:
top, right, bottom, left = face_location
draw.rectangle(((left, top), (right, bottom)), outline="red", width=2)

return image_with_faces, len(face_locations)


st.title("Face Detection App")

uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])

if uploaded_file is not None:
# Read the image file
image = Image.open(uploaded_file)

# Display the original image
st.image(image, caption="Original Image", use_column_width=True)

try:
# Detect faces
image_with_faces, face_count = detect_faces(image)

# Display the result
st.image(
image_with_faces,
caption=f"Detected Faces: {face_count}",
use_column_width=True,
)

if face_count == 0:
st.write("No faces detected in the image.")
elif face_count == 1:
st.write("1 face detected in the image.")
else:
st.write(f"{face_count} faces detected in the image.")
except Exception as e:
st.error(f"An error occurred during face detection: {str(e)}")
st.write(
"Please try uploading a different image or check if the face_recognition models are properly installed."
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
face-recognition
git+https://github.com/ageitgey/face_recognition_models
streamlit
numpy
Pillow

0 comments on commit 56cb52e

Please sign in to comment.