File size: 3,315 Bytes
6867877 9474ecb 6867877 9474ecb 6867877 |
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 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '-1'
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'
os.environ["PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION"] = "python"
import tensorflow as tf
tf.config.set_visible_devices([], 'GPU')
import gradio as gr
import numpy as np
from tensorflow.keras.preprocessing import image
from tensorflow.keras.models import model_from_json
from PIL import Image
from huggingface_hub import hf_hub_download
from description import description
from location import location
def load_model_from_file(json_path, h5_path):
with open(json_path, "r") as f:
json_config = f.read()
model = model_from_json(json_config)
model.load_weights(h5_path)
return model
# Load model langsung dari Hugging Face Hub
model_json_path = hf_hub_download(repo_id="CapstoneML/Model", filename="model.json")
model_weights_path = hf_hub_download(repo_id="CapstoneML/Model", filename="my_model.h5")
model = load_model_from_file(model_json_path, model_weights_path)
labels = [
"Benteng Vredeburg", "Candi Borobudur", "Candi Prambanan", "Gedung Agung Istana Kepresidenan",
"Masjid Gedhe Kauman", "Monumen Serangan 1 Maret", "Museum Gunungapi Merapi",
"Situs Ratu Boko", "Taman Sari", "Tugu Yogyakarta"
]
def classify_image(img):
try:
img = img.resize((224, 224))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = img_array / 255.0
pred = model.predict(img_array)[0]
confidence = np.max(pred)
predicted_label = labels[np.argmax(pred)]
akurasi = float(confidence)
if confidence < 0.8:
label_output = "Tidak dapat dikenali (Confidence: {:.2f}%)".format(confidence * 100)
deskripsi = (
"Tolong arahkan ke objek yang jelas agar bisa diidentifikasikan. "
"Pastikan anda berada di salah satu tempat seperti:\n"
"- Benteng Vredeburg\n- Candi Borobudur\n- Candi Prambanan\n"
"- Gedung Agung Istana Kepresidenan Yogyakarta\n- Masjid Gedhe Kauman\n"
"- Monumen Serangan 1 Maret\n- Museum Gunungapi Merapi\n- Situs Ratu Boko\n"
"- Taman Sari\n- Tugu Yogyakarta"
)
lokasi = "-"
else:
label_output = f"{predicted_label} (Confidence: {confidence * 100:.2f}%)"
deskripsi = description.get(predicted_label, "Deskripsi belum tersedia.")
lokasi = location.get(predicted_label, None)
if lokasi:
lokasi = f'<a href="{lokasi}" target="_blank">Lihat Lokasi di Google Maps</a>'
else:
lokasi = "Lokasi tidak ditemukan"
return label_output, deskripsi, lokasi, akurasi
except Exception as e:
return "Error", str(e), "-"
interface = gr.Interface(
fn=classify_image,
inputs=gr.Image(type="pil", label="Upload Gambar"),
outputs=[
gr.Textbox(label="Output Klasifikasi"),
gr.Textbox(label="Deskripsi Lengkap", lines=20, max_lines=50),
gr.HTML(label="Link Lokasi"),
],
title="Klasifikasi Gambar",
description="Upload gambar, sistem akan mengklasifikasikan dan memberikan deskripsi mengenai gambar tersebut."
)
interface.launch()
|