File size: 1,463 Bytes
6c5f0dc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
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()