Spaces:
Sleeping
Sleeping
File size: 1,032 Bytes
9394729 3e818e4 d0299ad 9394729 347551a 9394729 3e818e4 9394729 3e818e4 9394729 3e818e4 9394729 3e818e4 9394729 3e818e4 9394729 3e818e4 9394729 |
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 |
import tensorflow as tf
import gradio as gr
import pathlib
# Kiểm tra hệ điều hành
plt = platform.system()
if plt == 'Linux':
pathlib.WindowsPath = pathlib.PosixPath
# Tải mô hình từ file .h5
model = tf.keras.models.load_model('model.h5')
# Các danh mục cần phân loại
categories = ('dog', 'wolf')
# Hàm dự đoán ảnh
def classify_image(img):
img = img.resize((128, 128)) # Đảm bảo kích thước ảnh đúng như mô hình yêu cầu
img_array = tf.keras.utils.img_to_array(img) / 255.0 # Chuyển ảnh thành mảng và chuẩn hóa
img_array = tf.expand_dims(img_array, axis=0) # Thêm batch dimension
probs = model.predict(img_array)[0] # Lấy dự đoán
return dict(zip(categories, map(float, probs)))
# Tạo giao diện Gradio
image = gr.inputs.Image(shape=(192, 192))
label = gr.outputs.Label()
examples = ['dog.jpg', 'wolf.jpg', 'bird.jpg']
intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
intf.launch(inline=False)
|