Spaces:
Sleeping
Sleeping
File size: 1,758 Bytes
df6e73e |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
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ó")
|