Spaces:
Runtime error
Runtime error
Commit
•
18f06de
1
Parent(s):
cb7c59a
Upload 2 files
Browse files- .gitattributes +1 -0
- App.py +92 -0
- Model.keras +3 -0
.gitattributes
CHANGED
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
36 |
+
Model.keras filter=lfs diff=lfs merge=lfs -text
|
App.py
ADDED
@@ -0,0 +1,92 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
import time
|
3 |
+
import streamlit as st
|
4 |
+
from PIL import Image
|
5 |
+
import numpy as np
|
6 |
+
from tensorflow.keras.preprocessing.image import ImageDataGenerator # type: ignore
|
7 |
+
from tensorflow.keras.models import load_model # type: ignore
|
8 |
+
from tensorflow.keras.preprocessing import image # type: ignore
|
9 |
+
|
10 |
+
def Get_Information(predicted_name):
|
11 |
+
# Đường dẫn đến thư mục 'Dataset/Train'
|
12 |
+
train_dir = 'Dataset/Train'
|
13 |
+
|
14 |
+
# Duyệt qua mỗi lớp trong thư mục 'Dataset/Train'
|
15 |
+
for class_name in class_names:
|
16 |
+
# Nếu class_name không phải là predicted_name, tiếp tục vòng lặp
|
17 |
+
if class_name != predicted_name:
|
18 |
+
continue
|
19 |
+
|
20 |
+
# Đường dẫn đến tệp 'information.text' trong thư mục của lớp
|
21 |
+
info_file_path = os.path.join(train_dir, class_name, 'information.text')
|
22 |
+
|
23 |
+
# Kiểm tra xem tệp 'information.text' có tồn tại không
|
24 |
+
if os.path.exists(info_file_path):
|
25 |
+
# Đọc nội dung của tệp
|
26 |
+
with open(info_file_path, 'r', encoding='utf-8') as f:
|
27 |
+
info_text = f.read()
|
28 |
+
|
29 |
+
return info_text
|
30 |
+
else:
|
31 |
+
return f"Không tìm thấy tệp 'information.text' trong thư mục của lớp '{class_name}'."
|
32 |
+
|
33 |
+
# Tải mô hình
|
34 |
+
model = load_model('Model.keras')
|
35 |
+
|
36 |
+
# Khởi tạo ImageDataGenerator
|
37 |
+
datagen = ImageDataGenerator(rescale=1./255)
|
38 |
+
|
39 |
+
# Tải dữ liệu từ thư mục 'Dataset/Train'
|
40 |
+
train_data = datagen.flow_from_directory(
|
41 |
+
'Dataset/Train',
|
42 |
+
target_size=(224, 224),
|
43 |
+
batch_size=32,
|
44 |
+
class_mode='categorical'
|
45 |
+
)
|
46 |
+
|
47 |
+
# Get the dictionary of class indices
|
48 |
+
class_indices = train_data.class_indices
|
49 |
+
|
50 |
+
# Get the list of class names
|
51 |
+
class_names = list(class_indices.keys())
|
52 |
+
|
53 |
+
st.title("Nhận diện và tra cứu thông tin động vật quý hiếm thông qua học sâu")
|
54 |
+
|
55 |
+
uploaded_file = st.sidebar.file_uploader("Hoặc chọn một hình ảnh từ máy tính của bạn", type=['jpg', 'png'])
|
56 |
+
photo = st.camera_input("Chụp một bức ảnh")
|
57 |
+
|
58 |
+
if uploaded_file is not None or photo is not None:
|
59 |
+
if photo is not None:
|
60 |
+
img = Image.open(photo)
|
61 |
+
else:
|
62 |
+
img = Image.open(uploaded_file)
|
63 |
+
st.image(img, caption='Hình ảnh đã tải lên.', use_column_width=True)
|
64 |
+
|
65 |
+
img = img.resize((224, 224))
|
66 |
+
|
67 |
+
# Convert the image to a numpy array
|
68 |
+
img_array = image.img_to_array(img)
|
69 |
+
|
70 |
+
# Add a third dimension (for batch size), and rescale pixel values to [0, 1]
|
71 |
+
img_array = np.expand_dims(img_array, axis=0)
|
72 |
+
img_array /= 255.
|
73 |
+
|
74 |
+
with st.spinner('Đang xử lý...'):
|
75 |
+
# Use the model to predict the image's label
|
76 |
+
predictions = model.predict(img_array)
|
77 |
+
time.sleep(3)
|
78 |
+
|
79 |
+
# The output of the model is a 2D array, with shape (1, num_classes).
|
80 |
+
# To get the predicted label, we get the index of the maximum value in the array.
|
81 |
+
predicted_label = np.argmax(predictions)
|
82 |
+
|
83 |
+
# Use the predicted label to get the class name
|
84 |
+
predicted_class_name = class_names[predicted_label]
|
85 |
+
|
86 |
+
st.write("Độ Chính Xác")
|
87 |
+
st.bar_chart(predictions)
|
88 |
+
|
89 |
+
st.write("`ID: " + str(predicted_label) + "`")
|
90 |
+
st.write("## Tên: " + predicted_class_name)
|
91 |
+
with st.expander("Xem thông tin chi tiết"):
|
92 |
+
st.markdown(Get_Information(predicted_class_name))
|
Model.keras
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:fb0c326db37a85c59cd68126d4ea654bf7ed1ef231c431c825f280d135c021e7
|
3 |
+
size 367381449
|