Diezu commited on
Commit
9394729
·
verified ·
1 Parent(s): 347551a

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +20 -10
app.py CHANGED
@@ -1,20 +1,30 @@
1
- from fastai.vision.all import *
2
  import gradio as gr
3
  import pathlib
 
 
4
  plt = platform.system()
5
- if plt == 'Linux': pathlib.WindowsPath = pathlib.PosixPath
 
6
 
7
- learn = load_learner(pathlib.Path('model.pkl'))
8
- categories = ('dog','wolf')
9
 
10
- def classify_image(img):
11
- pred,idx,probs = learn.predict(img)
12
- return dict(zip(categories, map(float,probs)))
13
 
 
 
 
 
 
 
 
14
 
15
- image = gr.inputs.Image(shape=(192,192))
 
16
  label = gr.outputs.Label()
17
- examples = ['dog.jpg','wolf.jpg','bird.jpg']
18
 
19
  intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
20
- intf.launch(inline=False)
 
1
+ import tensorflow as tf
2
  import gradio as gr
3
  import pathlib
4
+
5
+ # Kiểm tra hệ điều hành
6
  plt = platform.system()
7
+ if plt == 'Linux':
8
+ pathlib.WindowsPath = pathlib.PosixPath
9
 
10
+ # Tải mô hình từ file .h5
11
+ model = tf.keras.models.load_model('model.h5')
12
 
13
+ # Các danh mục cần phân loại
14
+ categories = ('dog', 'wolf')
 
15
 
16
+ # Hàm dự đoán ảnh
17
+ def classify_image(img):
18
+ img = img.resize((128, 128)) # Đảm bảo kích thước ảnh đúng như mô hình yêu cầu
19
+ img_array = tf.keras.utils.img_to_array(img) / 255.0 # Chuyển ảnh thành mảng và chuẩn hóa
20
+ img_array = tf.expand_dims(img_array, axis=0) # Thêm batch dimension
21
+ probs = model.predict(img_array)[0] # Lấy dự đoán
22
+ return dict(zip(categories, map(float, probs)))
23
 
24
+ # Tạo giao diện Gradio
25
+ image = gr.inputs.Image(shape=(192, 192))
26
  label = gr.outputs.Label()
27
+ examples = ['dog.jpg', 'wolf.jpg', 'bird.jpg']
28
 
29
  intf = gr.Interface(fn=classify_image, inputs=image, outputs=label, examples=examples)
30
+ intf.launch(inline=False)