|
import gradio as gr |
|
import tensorflow as tf |
|
from tensorflow.keras.preprocessing import image |
|
import numpy as np |
|
from PIL import Image |
|
from keras import layers |
|
|
|
|
|
|
|
model = tf.keras.models.load_model("inception_acc_0.989001-_val_acc_0.98252.h5") |
|
|
|
|
|
class_labels = ['arm', 'hand', 'foot', 'legs','fullbody','head','backside', 'torso', 'stake', 'plastic'] |
|
|
|
def classify_image(img): |
|
|
|
img = img.resize((299, 299)) |
|
img = np.array(img) / 255.0 |
|
img = np.expand_dims(img, axis=0) |
|
|
|
|
|
predictions = model.predict(img) |
|
predicted_class = np.argmax(predictions, axis=1)[0] |
|
confidence = np.max(predictions) |
|
return {class_labels[i]: float(predictions[0][i]) for i in range(len(class_labels))}, confidence |
|
|
|
|
|
example_images = [ |
|
'head.jpg', |
|
'torso.jpg' |
|
] |
|
|
|
|
|
demo = gr.Interface( |
|
fn=classify_image, |
|
title="Human Bodypart Image Classification", |
|
description = "Predict the bodypart of human bodypart images. This is a demo of our human bodypart image <a href=\"https://huggingface.co/icputrd/Inception-V3-Human-Bodypart-Classifier\">classifier</a>.", |
|
inputs=gr.Image(type="pil"), |
|
outputs=[gr.Label(num_top_classes=len(class_labels)), gr.Number()], |
|
examples=example_images, |
|
cache_examples=False, |
|
live=True, |
|
) |
|
|
|
if __name__ == "__main__": |
|
demo.launch() |
|
|