import streamlit as st import numpy as np import json from PIL import Image import tensorflow as tf from tensorflow.keras.models import load_model from tensorflow.keras.applications.mobilenet_v2 import preprocess_input from tensorflow.keras.preprocessing import image as keras_image # === Fungsi untuk preprocessing gambar === def preprocess_image_inference(image_file, target_size=(224, 224)): img = Image.open(image_file).convert('RGB') img = img.resize(target_size) img_array = keras_image.img_to_array(img) img_array = preprocess_input(img_array) img_array = np.expand_dims(img_array, axis=0) return img_array # === Load model dan class names === model = load_model('model_inf.h5') with open("class_names.json", "r") as f: class_names = json.load(f) def run(): st.title("Computer Vision-Based Vehicle Recognition") st.write("### *Upload gambar yang ingin diprediksi:") # Upload file form uploaded_file = st.file_uploader("Upload gambar", type=["jpg", "jpeg", "png"]) if uploaded_file is not None: # Preprocess dan prediksi image_array = preprocess_image_inference(uploaded_file) pred = model.predict(image_array) predicted_index = np.argmax(pred, axis=1)[0] predicted_label = class_names[predicted_index] # Tampilkan hasil st.image(uploaded_file, caption=f"Predicted: {predicted_label}", use_column_width=True) st.success(f"✅ Prediksi: **{predicted_label}** (class index: {predicted_index})") if __name__ == '__main__': run()