File size: 1,665 Bytes
b7114b7
3220d0b
 
b7114b7
 
 
 
 
 
 
3220d0b
e01b157
 
 
b7114b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a3b83e7
b7114b7
 
 
 
 
 
049e8fd
b7114b7
 
 
 
 
 
 
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
49
50
import gradio as gr
import keras
from keras.models import load_model
from tensorflow.keras.preprocessing import image
import numpy as np
from PIL import Image

# Load the trained model (ensure this path is correct in your Space)
model = load_model('human_identifier_model.h5')

model.compile(optimizer=keras.optimizers.Adam(),  # Choose the same optimizer used during training
              loss='binary_crossentropy',  # Use the same loss function as during training
              metrics=['accuracy'])

# Define the image size for input (based on your model's input size)
IMG_SIZE = (299, 299)

# Define the image preparation function
def prepare_image(img):
    img = img.resize(IMG_SIZE)
    img_array = np.array(img)
    img_array = np.expand_dims(img_array, axis=0)  # Add batch dimension
    img_array = img_array / 255.0  # Normalize if necessary
    return img_array

# Define the prediction function
def predict(image):
    # Prepare the image for prediction
    img_array = prepare_image(image)

    # Make a prediction
    prediction = model.predict(img_array)

    # Return the result
    result = 'Human' if prediction[0] < 0.5 else 'Non-Human'
    return result, prediction[0]

# Create the Gradio interface
iface = gr.Interface(
    fn=predict, 
    inputs=gr.Image(type="pil"),  # Accepts image input
    outputs=[gr.Label(num_top_classes=1), gr.Textbox()],  # Displays result and confidence
    live=True,  # Optional: updates the result as soon as the user uploads an image
    title="Human vs Non-Human Classifier",
    description="Upload an image to classify whether it's a human or non-human."
)

# Launch the Gradio interface
iface.launch()