Spaces:
Sleeping
Sleeping
File size: 2,086 Bytes
c7801ef |
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 |
import gradio as gr
import numpy as np
from PIL import Image
import tensorflow as tf
import joblib
# 1. Load Model CNN (Keras)
try:
model = tf.keras.models.load_model('cnn_model.h5')
print("Load model thành công")
except Exception as e:
print(f"Lỗi load model: {e}")
model = None
# 2. Load Label Encoder
try:
label_encoder = joblib.load('label_encoder.joblib')
print("Load encoder thành công")
except:
label_encoder = None
def preprocess_image(image):
# Chuyển sang RGB
image = image.convert("RGB")
# Resize đúng kích thước lúc train (64x64)
image = image.resize((64, 64))
# Chuyển thành mảng numpy và chuẩn hóa / 255.0
image_array = np.array(image) / 255.0
# QUAN TRỌNG: CNN cần input 4 chiều (Batch, Height, Width, Channel)
# Nên ta phải thêm 1 chiều ở đầu: (64,64,3) -> (1, 64, 64, 3)
image_array = np.expand_dims(image_array, axis=0)
return image_array
def predict(image):
if model is None or label_encoder is None:
return "Lỗi: Chưa load được model hoặc encoder."
try:
# Xử lý ảnh
processed_img = preprocess_image(image)
# Dự đoán
prediction = model.predict(processed_img)
# Lấy vị trí có xác suất cao nhất (argmax)
class_index = np.argmax(prediction)
# Chuyển từ số về tên nhãn
class_name = label_encoder.inverse_transform([class_index])[0]
# Lấy độ tin cậy (xác suất)
confidence = float(np.max(prediction))
return f"Kết quả: {class_name} ({confidence*100:.2f}%)"
except Exception as e:
return f"Lỗi dự đoán: {str(e)}"
# Giao diện Gradio
iface = gr.Interface(
fn=predict,
inputs=gr.Image(type="pil"),
outputs="text",
title="Nhận diện Biển báo Giao thông (CNN)",
description="Upload ảnh biển báo để model CNN dự đoán."
)
if __name__ == "__main__":
iface.launch()
|