|
import gradio as gr |
|
import numpy as np |
|
from tensorflow.keras.preprocessing import image |
|
from tensorflow.keras.models import load_model |
|
from huggingface_hub import hf_hub_download |
|
|
|
|
|
repo_id = "johansetiawan17/intel-image-classification" |
|
filename = "model-intel-image-classification.h5" |
|
|
|
|
|
model_path = hf_hub_download(repo_id=repo_id, filename=filename) |
|
|
|
|
|
model = load_model(model_path) |
|
|
|
|
|
class_labels = ['buildings', 'forest', 'glacier', 'mountain', 'sea', 'street'] |
|
|
|
|
|
def predict_image(img_input): |
|
image.save_img('./image.jpeg', img_input) |
|
img = image.load_img('./image.jpeg', target_size=(150, 150)) |
|
img_array = image.img_to_array(img) |
|
img_array = np.expand_dims(img_array, axis=0) |
|
img_array /= 255. |
|
|
|
prediction = model.predict(img_array) |
|
predicted_class = np.argmax(prediction) |
|
confidence = prediction[0][predicted_class] * 100 |
|
|
|
result = f"Prediction: {class_labels[predicted_class]} ({confidence:.2f}%)" |
|
return result |
|
|
|
|
|
iface = gr.Interface( |
|
fn=predict_image, |
|
inputs=gr.Image(type="pil"), |
|
outputs=gr.Label(num_top_classes=6), |
|
title="Natural Scenes Classification", |
|
description="Upload an natural scenes or landscape image to classify it into one of the six categories: buildings, forest, glacier, mountain, sea, or street.", |
|
) |
|
|
|
|
|
iface.launch(share=True) |