Commit d4e26c3a by 杨林龙

优化图片分类模型

1 parent 97b9a367
import tensorflow as tf
from tensorflow import keras
import numpy as np
# 加载预训练的模型
model = keras.applications.MobileNetV2(weights="imagenet")
# 加载图像分类标签
labels_path = tf.keras.utils.get_file(
"ImageNetLabels.txt",
"https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt",
)
with open(labels_path) as f:
labels = f.readlines()
labels = [label.strip() for label in labels]
# 加载并预处理图像
def preprocess_image(image_path):
img = keras.preprocessing.image.load_img(image_path, target_size=(224, 224))
img_array = keras.preprocessing.image.img_to_array(img)
img_array = np.expand_dims(img_array, axis=0)
img_array = tf.keras.applications.mobilenet_v2.preprocess_input(img_array)
return img_array
# 图像分类
def classify_image(image_path):
img_array = preprocess_image(image_path)
predictions = model.predict(img_array)
predicted_label = labels[np.argmax(predictions)]
return predicted_label
# 测试图片分类
image_path = "./55e9354cefebf6b3137a9cfa7e69b87.png" # 替换为你的图片路径
predicted_label = classify_image(image_path)
print("预测结果:", predicted_label)
...@@ -31,9 +31,12 @@ def image_similarity(image1, image2): ...@@ -31,9 +31,12 @@ def image_similarity(image1, image2):
# 读取两张照片 # 读取两张照片
image1 = cv2.imread("./3140454f6ae50c86bb7c99c15620623.jpg") # image1 = cv2.imread("./3140454f6ae50c86bb7c99c15620623.jpg")
# image2 = cv2.imread("./34a66467deca292c93bfed5d13c2706.jpg") # image2 = cv2.imread("./34a66467deca292c93bfed5d13c2706.jpg")
image2 = cv2.imread("./f37fb654ce205faaa796d093c44cc28.jpg") # image2 = cv2.imread("./f37fb654ce205faaa796d093c44cc28.jpg")
image1 = cv2.imread("./892fad7728dc8b8e7e9ec532cf7771e.png")
image2 = cv2.imread("./892fad7728dc8b8e7e9ec532cf7771e.png")
# 比较照片相似度 # 比较照片相似度
is_duplicate = image_similarity(image1, image2) is_duplicate = image_similarity(image1, image2)
......
Markdown is supported
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!