File size: 985 Bytes
9b7d4d8
 
 
 
 
 
 
72a8cc6
9b7d4d8
 
 
 
 
 
 
 
 
 
 
 
 
e870457
 
 
9b7d4d8
 
 
 
 
 
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
import cv2
import dlib
import gradio as gr
from PIL import Image
from transformers import pipeline

# Load pre-trained image classification model from transformers library
model = pipeline("image-classification", model="0x70DA/down-syndrome-classifier-v2")

# Load face detector from dlib library
detector = dlib.get_frontal_face_detector()


def predict(img):
    faces = detector(img)
    if len(faces) > 0:
        face = faces[0]  # Assuming there's only one face in the image
        x, y, w, h = face.left(), face.top(), face.width(), face.height()
        cropped_face = img[y : y + h, x : x + w]
        # Convert the cropped image to a PIL image
        pil_image = Image.fromarray(cv2.cvtColor(cropped_face, cv2.COLOR_BGR2RGB))
        pred = model(pil_image)
        return {o["label"]: o["score"] for o in pred}
    return RuntimeError("No faces detected.")


demo = gr.Interface(
    fn=predict, inputs=gr.components.Image(), outputs=gr.components.Label()
)
demo.launch()