File size: 1,090 Bytes
7812f47
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9bee20d
7812f47
 
 
 
 
 
 
 
 
 
 
 
 
 
4661cbe
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
import gradio as gr
import os
from svnm.models import GenderDetection
from datetime import datetime

# Create a directory for saving uploaded images
UPLOAD_DIR = "uploadimages"
os.makedirs(UPLOAD_DIR, exist_ok=True)

# Load the model once
model = GenderDetection()

# Function to save image and make predictions
def classify_image(img):
    # Generate a unique filename
    timestamp = datetime.now().strftime("%Y%m%d%H%M%S")
    img_path = os.path.join(UPLOAD_DIR, f"{timestamp}.jpg")
    
    # Save the uploaded image
    img.save(img_path)
    
    # Make predictions using the model
    label, conf = model.predict(img_path)
    os.unlink(img_path)
    
    return f"Label: {label}, Confidence: {conf*100:.2f}%"

# Define the Gradio interface
interface = gr.Interface(
    fn=classify_image,
    inputs=gr.Image(type="pil"),  # Updated Gradio input
    outputs="text",              # Output as text
    title="Image Classification",
    description="Upload an image to classify it into categories."
)

# Launch the interface
if __name__ == "__main__":
    interface.launch(share=True)