File size: 1,953 Bytes
0f186b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import cv2
import gradio as gr

from details import get_celebrity_details
from model import predict_faces


# Upload mode logic with dynamic radio button functionality
def upload_mode(image):
    # Predict the faces in the uploaded image
    predictions, _ = predict_faces(image, cv2.data.haarcascades + 'haarcascade_frontalface_default.xml')

    # If no faces are detected, return default messages
    if predictions == "No faces detected.":
        return "No faces detected", "No details available", "path/to/default/img.jpg"

    # Get the first predicted celebrity and their details
    celebrity_name = predictions[0] if predictions else None
    details = get_celebrity_details(celebrity_name) if celebrity_name else {"details": "No details available",
                                                                            "img": "path/to/default/img.jpg"}

    # Return dynamic radio button choices, details, and  image path
    return gr.update(choices=predictions), details["details"], details["img"]


# Define a callback function for when a radio button choice is selected
def update_details(selected_celebrity):
    details = get_celebrity_details(selected_celebrity)
    return details["details"], details["img"]


with gr.Blocks() as upload_interface:
    with gr.Row():
        image_input = gr.Image(type="numpy", label="Upload Image with Celebrities")
        radio_btn = gr.Radio(choices=[], label="Select Detected Celebrity", interactive=True)

    with gr.Row():
        details_textbox = gr.Markdown(label="Celebrity Details")
        image = gr.Image(type="filepath", label="Celebrity")

    # Callbacks for updating the interface
    image_input.change(upload_mode, inputs=image_input, outputs=[radio_btn, details_textbox, image])
    radio_btn.change(update_details, inputs=radio_btn, outputs=[details_textbox, image])

if __name__ == "__main__":
    upload_interface.launch()