Spaces:
Sleeping
Sleeping
Krishs21 commited on
Commit ·
b7114b7
1
Parent(s): d561637
Add application file
Browse files- app.py +44 -0
- human_identifier_model.h5 +3 -0
- requirements.txt +4 -0
app.py
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from tensorflow.keras.models import load_model
|
| 3 |
+
from tensorflow.keras.preprocessing import image
|
| 4 |
+
import numpy as np
|
| 5 |
+
from PIL import Image
|
| 6 |
+
|
| 7 |
+
# Load the trained model (ensure this path is correct in your Space)
|
| 8 |
+
model = load_model('human_identifier_model.h5')
|
| 9 |
+
|
| 10 |
+
# Define the image size for input (based on your model's input size)
|
| 11 |
+
IMG_SIZE = (299, 299)
|
| 12 |
+
|
| 13 |
+
# Define the image preparation function
|
| 14 |
+
def prepare_image(img):
|
| 15 |
+
img = img.resize(IMG_SIZE)
|
| 16 |
+
img_array = np.array(img)
|
| 17 |
+
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
| 18 |
+
img_array = img_array / 255.0 # Normalize if necessary
|
| 19 |
+
return img_array
|
| 20 |
+
|
| 21 |
+
# Define the prediction function
|
| 22 |
+
def predict(image):
|
| 23 |
+
# Prepare the image for prediction
|
| 24 |
+
img_array = prepare_image(image)
|
| 25 |
+
|
| 26 |
+
# Make a prediction
|
| 27 |
+
prediction = model.predict(img_array)
|
| 28 |
+
|
| 29 |
+
# Return the result
|
| 30 |
+
result = 'Human' if prediction[0] > 0.5 else 'Non-Human'
|
| 31 |
+
return result, prediction[0]
|
| 32 |
+
|
| 33 |
+
# Create the Gradio interface
|
| 34 |
+
iface = gr.Interface(
|
| 35 |
+
fn=predict,
|
| 36 |
+
inputs=gr.Image(type="pil"), # Accepts image input
|
| 37 |
+
outputs=[gr.Label(num_top_classes=1), gr.outputs.Textbox()], # Displays result and confidence
|
| 38 |
+
live=True, # Optional: updates the result as soon as the user uploads an image
|
| 39 |
+
title="Human vs Non-Human Classifier",
|
| 40 |
+
description="Upload an image to classify whether it's a human or non-human."
|
| 41 |
+
)
|
| 42 |
+
|
| 43 |
+
# Launch the Gradio interface
|
| 44 |
+
iface.launch()
|
human_identifier_model.h5
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:e638411ef9bdbaf659ee8e1844e0fdeaaf5865a504b93e7708410f0521c91075
|
| 3 |
+
size 275697416
|
requirements.txt
ADDED
|
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
tensorflow
|
| 2 |
+
keras
|
| 3 |
+
gradio
|
| 4 |
+
numpy
|