woff 發表於 2023-7-19 23:08:22

上傳圖片(0~9)辨識ImportError: Filepath looks like a hdf5 file but h5py ...

import streamlit as st
from skimage import io
from skimage.transform import resize

import numpy as np

import tensorflow as tf

model = tf.keras.models.load_model('./mnist_model.h5')
uploaded_file = st.file_uploader("上傳圖片(.png)",type="png")

if uploaded_file is not None:
    image1 = io.imread(uploaded_file, as_gray=True)
    image_resized = resize(image1,(28,28), anti_aliasing=True)
    X1 = image_resized.reshape(1,28,28,1)
    X1 = np.abs(1-X1)

    predictions = model.predict_classes(X1)
    st.write(f'預測結果:{predictions}')
    st.image(image1)錯誤代碼

ImportError: Filepath looks like a hdf5 file but h5pyis not available上面敘述加入

import h5py在Termainal
輸入
streamlit run D:\scrapytest\img-read.py

這時又出現錯誤
'Sequential' object has no attribute 'predict_classes'

代碼
predictions = model.predict_classes(X1)
    st.write(f'預測結果:{predictions}')
    st.image(image1)


改成
# 預測
    predictions = model.predict(X1)
    classes_x = np.argmax(predictions, axis=1)
    # 顯示預測結果
    st.write(f'預測結果:{classes_x}')

就正常了

參考文章
https://www.cjavapy.com/article/2239/

頁: [1]
查看完整版本: 上傳圖片(0~9)辨識ImportError: Filepath looks like a hdf5 file but h5py ...