TShopping

標題: 上傳圖片(0~9)辨識ImportError: Filepath looks like a hdf5 file but h5py ... [打印本頁]

作者: woff    時間: 2023-7-19 23:08
標題: 上傳圖片(0~9)辨識ImportError: Filepath looks like a hdf5 file but h5py ...
  1. import streamlit as st
  2. from skimage import io
  3. from skimage.transform import resize

  4. import numpy as np

  5. import tensorflow as tf

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

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

  13.     predictions = model.predict_classes(X1)[0]
  14.     st.write(f'預測結果:{predictions}')
  15.     st.image(image1)
複製代碼
錯誤代碼

  1. ImportError: Filepath looks like a hdf5 file but h5pyis not available
複製代碼
上面敘述加入

  1. import h5py
複製代碼
在Termainal
輸入
  1. streamlit run D:\scrapytest\img-read.py
複製代碼


這時又出現錯誤
  1. 'Sequential' object has no attribute 'predict_classes'
複製代碼


代碼
  1. predictions = model.predict_classes(X1)[0]
  2.     st.write(f'預測結果:{predictions}')
  3.     st.image(image1)
複製代碼



改成
  1. # 預測
  2.     predictions = model.predict(X1)
  3.     classes_x = np.argmax(predictions, axis=1)
  4.     # 顯示預測結果
  5.     st.write(f'預測結果:{classes_x}')
複製代碼


就正常了

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






歡迎光臨 TShopping (http://www.tshopping.com.tw/) Powered by Discuz! X3.2