调用 TensorFlow 已训练好的模型做图像识别

2021-03-0510:39:37数据结构与算法Comments4,008 views字数 1416阅读模式
# !pip install tensorflow
%matplotlib inline 

import matplotlib.pyplot as plt
import tensorflow as tf
from tensorflow.keras.applications.xception import Xception
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.xception import preprocess_input, decode_predictions
import numpy as np

from PIL import Image
import requests
from io import BytesIO

加载模型文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html

model = Xception(weights='imagenet', include_top=True)

加载图片文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html

# img = image.load_img(image_file_path, target_size=(299, 299))  # 从硬盘加载图片

url = "https://images.unsplash.com/photo-1611090480455-fc0ea8ef5792?ixid=MXwxMjA3fDB8MHx0b3BpYy1mZWVkfDM2MXxSX0Z5bi1Hd3Rsd3x8ZW58MHx8fA%3D%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=500&q=60"
rsp = requests.get(url)
img = Image.open(BytesIO(rsp.content))

# 调整大小为299*299 以适应Xception模型的输入格式
img = img.resize((299,299))
plt.imshow(img)
<matplotlib.image.AxesImage at 0x7f9ba45a8450>

调用 TensorFlow 已训练好的模型做图像识别文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html

把图片转换成NumPy数组,并做预测文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html

x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

features = model.predict(x)

# 打印前 10 识别的对象
decode_predictions(features, top=10)
[[('n02948072', 'candle', 0.5540947),
  ('n04201297', 'shoji', 0.03528823),
  ('n04590129', 'window_shade', 0.022406286),
  ('n04330267', 'stove', 0.01924976),
  ('n03388549', 'four-poster', 0.01494646),
  ('n04239074', 'sliding_door', 0.010124662),
  ('n03201208', 'dining_table', 0.009404816),
  ('n03992509', "potter's_wheel", 0.009054),
  ('n02699494', 'altar', 0.0068786764),
  ('n03179701', 'desk', 0.0058375616)]]

识别出来是蜡烛文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html

HowTos 项目 Github 地址: https://github.com/toutiaoio/HowTos文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/suanfa/21006.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/suanfa/21006.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定