不管从哪个角度来说,科技领域的科技树都在从人工智能的方向在发展,从最初的数据挖掘,到前几年的大数据,然后是机器学习,到机器学习的细分领域深度学习,都在逐渐让人工智能的想象成为现实,在自然语言、图像和声音领域,深度学习已经展现了令人叹为观止的能力。
今天,这里展示一个人工智能的经典用例——图像识别。
代码参考自 http://www.sohu.com/a/151663692_697750
先上结果,代码附在最后,免得没看完就关掉。
其实这是一个挺不错的思路,对于人工智能的应用来说,并非一切都必须从头来过,对于已有模型的二次开发也许存在着非常大的机会。
这是一个人工智能逐渐下沉的时代。
以下是代码。
```
# coding=utf-8
from keras.applications import ResNet50
from keras.applications import InceptionV3
from keras.applications import Xception # tensforlow only
from keras.applications import VGG16
from keras.applications import VGG19
from keras.applications import imagenet_utils
from keras.applications.inception_v3 import preprocess_input
from keras.preprocessing.image import img_to_array
from keras.preprocessing.image import load_img
import numpy as np
import argparse
import cv2
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required=True,
help="path to the input image")
ap.add_argument("-model", "--model", type=str, default="vgg16",
help="name of pre-trained network to use")
args = vars(ap.parse_args())
MODELS = {
"vgg16": VGG16,
"vgg19": VGG19,
"inception": InceptionV3,
"xception": Xception,
"resnet": ResNet50
}
if args["model"] not in MODELS.keys():
raise AssertionError("The --model command line argument should be a key in the `MODELS` dictionary")
if args["model"] in ("inception", "xception"):
inputShape = (299, 299)
preprocess = preprocess_input
else:
inputShape = (224, 224)
preprocess = imagenet_utils.preprocess_input
print("[INFO] loading {}...".format(args["model"]))
Network = MODELS[args["model"]]
model = Network(weights="imagenet")
print("[INFO] loading and pre-processing image..." )
image = load_img(args["image"], target_size=inputShape)
image = img_to_array(image)
image = np.expand_dims(image, axis=0)
image = preprocess(image)
print("[INFO] classifying image with '{}'...".format(args["model"]))
preds = model.predict(image)
P = imagenet_utils.decode_predictions(preds)
for (i, (imagenetID, label, prob)) in enumerate(P[0]):
print("{}. {}: {:.2f}%".format(i + 1, label, prob * 100))
orig = cv2.imread(args["image"])
(imagenetID, label, prob) = P[0][0]
cv2.putText(orig, "Label: {}, {:.2f}%".format(label, prob * 100),
(10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 2)
cv2.imshow("Classification", orig)
cv2.waitKey(0)
```