datnguyentien204
commited on
Commit
•
ca8e13f
1
Parent(s):
df6e73e
Upload app.py
Browse files
app.py
ADDED
@@ -0,0 +1,57 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from keras.models import load_model
|
2 |
+
import keras.utils as image
|
3 |
+
import numpy as np
|
4 |
+
import cv2
|
5 |
+
import tempfile
|
6 |
+
import streamlit as st
|
7 |
+
from PIL import Image
|
8 |
+
|
9 |
+
# Load the saved model
|
10 |
+
|
11 |
+
|
12 |
+
# Load and preprocess an image for prediction
|
13 |
+
# img_path = r'D:\PycharmProjects\hocmay\Dog_Cat_CNN2\anh-cho-cuoi.jpg' # Replace with the path to your image
|
14 |
+
# Normalize the image
|
15 |
+
|
16 |
+
|
17 |
+
# Perform prediction
|
18 |
+
# Get the index of the predicted class
|
19 |
+
|
20 |
+
|
21 |
+
model_file="model4.h5"
|
22 |
+
img_file=st.file_uploader("Tải lên ảnh lớp",type=["png","jpg","jpeg"])
|
23 |
+
temp_file2 = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False)
|
24 |
+
if img_file is not None:
|
25 |
+
temp_file2.write(img_file.read())
|
26 |
+
|
27 |
+
#Loaded model
|
28 |
+
loaded_model = load_model(model_file)
|
29 |
+
|
30 |
+
button2 = st.button("Xử lí", key="btn2")
|
31 |
+
if button2:
|
32 |
+
img = image.load_img(temp_file2.name, target_size=(128, 128))
|
33 |
+
img_array = image.img_to_array(img)
|
34 |
+
img_array = np.expand_dims(img_array, axis=0)
|
35 |
+
img_array /= 255.0
|
36 |
+
|
37 |
+
prediction = loaded_model.predict(img_array)
|
38 |
+
class_index = np.argmax(prediction)
|
39 |
+
|
40 |
+
if class_index == 0:
|
41 |
+
img_cv2 = cv2.imread(temp_file2.name)
|
42 |
+
|
43 |
+
img_cv2 = cv2.putText(img_cv2, 'Cat', (00, 70), cv2.FONT_HERSHEY_SIMPLEX,
|
44 |
+
3, (0, 0, 255), thickness=5)
|
45 |
+
|
46 |
+
|
47 |
+
st.image(img_cv2, caption='Ảnh mèo',channels="BGR")
|
48 |
+
st.markdown("Đây là ảnh mèo")
|
49 |
+
|
50 |
+
else:
|
51 |
+
img_cv2 = cv2.imread(temp_file2.name)
|
52 |
+
|
53 |
+
img_cv2 = cv2.putText(img_cv2, 'Dog', (00, 70), cv2.FONT_HERSHEY_SIMPLEX,
|
54 |
+
3, (0, 0, 255), thickness=5)
|
55 |
+
st.image(img_cv2, caption='Ảnh chó',channels="BGR")
|
56 |
+
st.markdown("Đây là ảnh chó")
|
57 |
+
|