Spaces:
Build error
Build error
import streamlit as st | |
from PIL import Image | |
import torch | |
from torchvision import transforms | |
from transformers import pipeline | |
# Load the face detection model | |
face_detector = pipeline('face-detection', model='facebook/facemask-plugin-fasterrcnn') | |
# Function to detect faces in an image | |
def detect_faces(image): | |
# Convert image to PyTorch tensor | |
image_tensor = transforms.ToTensor()(image).unsqueeze(0) | |
# Detect faces | |
faces = face_detector(image_tensor) | |
return faces | |
# Streamlit app | |
def main(): | |
st.title("Multiple Face Detection using Hugging Face") | |
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "jpeg", "png"]) | |
if uploaded_file is not None: | |
# Read the image | |
image = Image.open(uploaded_file) | |
st.image(image, caption="Uploaded Image", use_column_width=True) | |
# Detect faces | |
faces = detect_faces(image) | |
# Display the number of faces detected | |
num_faces = len(faces) | |
st.write(f"Number of faces detected: {num_faces}") | |
# Display bounding boxes around detected faces | |
for face in faces: | |
xmin, ymin, xmax, ymax = face['box'] | |
image_with_box = image.copy() | |
draw = ImageDraw.Draw(image_with_box) | |
draw.rectangle([xmin, ymin, xmax, ymax], outline="red", width=3) | |
st.image(image_with_box, caption="Face Detection", use_column_width=True) | |
if __name__ == "__main__": | |
main() | |