Skip to content

Instantly share code, notes, and snippets.

@adash333
Last active August 4, 2017 15:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save adash333/fcf40b9f9b68b2da5701d6bfc4cfac9f to your computer and use it in GitHub Desktop.
Save adash333/fcf40b9f9b68b2da5701d6bfc4cfac9f to your computer and use it in GitHub Desktop.
fruit102.py
# このコードはfruit101.pyの続きです
# original code from http://qiita.com/secang0/items/1229212a37d8c9922901
import numpy as np
import matplotlib.pyplot as plt
filepath = "./tomato.jpg"
#画像の読み込み
#imgに三次元のnp.arrayの配列が格納される。
img = plt.imread(filepath)
print(filepath)
type(img) #=> numpy.ndarray
print(type(img))
img.size
print(img.shape)
#画像の表示
plt.imshow(img)
plt.show() #使った画像が小さいときはボケて見えるけど今は気にしないで
# 画像を25x25pixelに変換し、1要素が[R,G,B]3要素を含む配列の5x5の2次元配列として読み込む。
# [R,G,B]はそれぞれが0-255の配列。
image = np.array(Image.open(filepath).resize((5,5)))
print(filepath)
print(image)
plt.imshow(image)
plt.show()
# 配列を変換し、[[Redの配列],[Greenの配列],[Blueの配列]] のような形にする。
image = image.transpose(2, 0, 1)
print()
print("transpose(2, 0, 1)")
print(image)
# さらにフラットな1次元配列に変換。最初の1/3はRed、次がGreenの、最後がBlueの要素がフラットに並ぶ。
image = image.reshape(1, image.shape[0] * image.shape[1] * image.shape[2]).astype("float32")[0]
print()
print("reshape")
print(image)
# 出来上がった配列を正規化?
image = image / 255.
print()
print("normalization")
print(image)
'''
# kerasに渡すためにnumpy配列に変換。
image = np.array(image)
print()
print("numpy array")
print(image)
'''
result = model.predict_classes(np.array([image]))
print()
print("result:", result[0], "(0:りんご, 1:オレンジ)")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment