datnguyentien204's picture
Upload 2 files
df6e73e
raw
history blame
No virus
1.76 kB
from keras.models import load_model
import keras.utils as image
import numpy as np
import cv2
import tempfile
import streamlit as st
from PIL import Image
# Load the saved model
# Load and preprocess an image for prediction
# img_path = r'D:\PycharmProjects\hocmay\Dog_Cat_CNN2\anh-cho-cuoi.jpg' # Replace with the path to your image
# Normalize the image
# Perform prediction
# Get the index of the predicted class
model_file="model4.h5"
img_file=st.file_uploader("Tải lên ảnh lớp",type=["png","jpg","jpeg"])
temp_file2 = tempfile.NamedTemporaryFile(suffix=".pkl", delete=False)
if img_file is not None:
temp_file2.write(img_file.read())
#Loaded model
loaded_model = load_model(model_file)
button2 = st.button("Xử lí", key="btn2")
if button2:
img = image.load_img(temp_file2.name, target_size=(128, 128))
img_array = image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array /= 255.0
prediction = loaded_model.predict(img_array)
class_index = np.argmax(prediction)
if class_index == 0:
img_cv2 = cv2.imread(temp_file2.name)
img_cv2 = cv2.putText(img_cv2, 'Cat', (00, 70), cv2.FONT_HERSHEY_SIMPLEX,
3, (0, 0, 255), thickness=5)
st.image(img_cv2, caption='Ảnh mèo',channels="BGR")
st.markdown("Đây là ảnh mèo")
else:
img_cv2 = cv2.imread(temp_file2.name)
img_cv2 = cv2.putText(img_cv2, 'Dog', (00, 70), cv2.FONT_HERSHEY_SIMPLEX,
3, (0, 0, 255), thickness=5)
st.image(img_cv2, caption='Ảnh chó',channels="BGR")
st.markdown("Đây là ảnh chó")