Thanh6624 commited on
Commit
08b08d4
·
verified ·
1 Parent(s): 470c233

Upload 3 files

Browse files
Files changed (3) hide show
  1. app.py +56 -0
  2. human_classifier_model.h5 +3 -0
  3. requirements.txt +4 -0
app.py ADDED
@@ -0,0 +1,56 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+
6
+ # --- CẤU HÌNH ---
7
+ IMG_SIZE = 150 # Kích thước ảnh 150x150
8
+ MODEL_PATH = "model.h5" # Tên file model
9
+
10
+ # --- LOAD MODEL ---
11
+ try:
12
+ model = tf.keras.models.load_model(MODEL_PATH)
13
+ print("Đã load model thành công.")
14
+ except Exception as e:
15
+ print(f"Lỗi không tìm thấy model: {e}")
16
+
17
+ # --- HÀM DỰ ĐOÁN ---
18
+ def phan_loai(image):
19
+ if image is None:
20
+ return None
21
+
22
+ # 1. Resize ảnh về 150x150
23
+ image = image.resize((IMG_SIZE, IMG_SIZE))
24
+
25
+ # 2. Chuyển sang mảng số & Chuẩn hóa (chia 255)
26
+ img_array = np.array(image)
27
+ img_array = img_array / 255.0
28
+
29
+ # 3. Thêm chiều batch (1, 150, 150, 3)
30
+ img_array = np.expand_dims(img_array, axis=0)
31
+
32
+ # 4. Dự đoán
33
+ prediction = model.predict(img_array)[0]
34
+
35
+ # 5. Xử lý kết quả trả về
36
+ # Kiểm tra xem Output là 1 số (Sigmoid) hay 2 số (Softmax)
37
+ if len(prediction) == 1:
38
+ # Dạng Sigmoid: 0-1
39
+ score_cho = float(prediction[0]) # Giả sử 1 là Chó
40
+ return {"Chó": score_cho, "Mèo": 1 - score_cho}
41
+ else:
42
+ # Dạng Softmax: [Mèo, Chó] (Thường ImageDataGenerator xếp theo Alphabet)
43
+ labels = ['Mèo', 'Chó']
44
+ return {labels[i]: float(prediction[i]) for i in range(len(labels))}
45
+
46
+ # --- GIAO DIỆN ---
47
+ demo = gr.Interface(
48
+ fn=phan_loai,
49
+ inputs=gr.Image(type="pil", label="Tải ảnh lên"),
50
+ outputs=gr.Label(num_top_classes=2, label="Kết quả"),
51
+ title="🐶 Web Phân Loại Chó Mèo 🐱",
52
+ description="Model AI nhận diện Chó và Mèo (Input size: 150x150)."
53
+ )
54
+
55
+ if __name__ == "__main__":
56
+ demo.launch()
human_classifier_model.h5 ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ version https://git-lfs.github.com/spec/v1
2
+ oid sha256:abfa3a4988cc5124b8209c4b6dffe8a58ba817fddf57f2aa5384c87276fbcc8b
3
+ size 41494600
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ tensorflow
2
+ numpy
3
+ pillow
4
+ gradio