Rifatsyq/Computer_Vision
0
1import streamlit as st
2import numpy as np
3import json
4from PIL import Image
5import tensorflow as tf
6
7from tensorflow.keras.models import load_model
8from tensorflow.keras.applications.mobilenet_v2 import preprocess_input
9from tensorflow.keras.preprocessing import image as keras_image
10
11# === Fungsi untuk preprocessing gambar ===
12def preprocess_image_inference(image_file, target_size=(224, 224)):
13 img = Image.open(image_file).convert('RGB')
14 img = img.resize(target_size)
15 img_array = keras_image.img_to_array(img)
16 img_array = preprocess_input(img_array)
17 img_array = np.expand_dims(img_array, axis=0)
18 return img_array
19
20# === Load model dan class names ===
21model = load_model('model_inf.h5')
22
23with open("class_names.json", "r") as f:
24 class_names = json.load(f)
25
26def run():
27 st.title("Computer Vision-Based Vehicle Recognition")
28 st.write("### *Upload gambar yang ingin diprediksi:")
29
30 # Upload file form
31 uploaded_file = st.file_uploader("Upload gambar", type=["jpg", "jpeg", "png"])
32
33 if uploaded_file is not None:
34 # Preprocess dan prediksi
35 image_array = preprocess_image_inference(uploaded_file)
36 pred = model.predict(image_array)
37 predicted_index = np.argmax(pred, axis=1)[0]
38 predicted_label = class_names[predicted_index]
39
40 # Tampilkan hasil
41 st.image(uploaded_file, caption=f"Predicted: {predicted_label}", use_column_width=True)
42 st.success(f"✅ Prediksi: **{predicted_label}** (class index: {predicted_index})")
43
44if __name__ == '__main__':
45 run()
46 