-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adds streamlit face-recognition example
- Loading branch information
1 parent
e9284ed
commit 56cb52e
Showing
4 changed files
with
90 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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." | ||
) |
5 changes: 5 additions & 0 deletions
5
examples/streamlit/streamlit-face-recognition/requirements.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |