open cv提取图片特征值_基于VGG16网络提取Flicker8K数据集图像特征
# !/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time : ${20200326} ${18:00}
# @Author : ZicoZhou
# @Version :1.0
# @Function : VGG16網絡提取圖像特征
from keras.models import model_from_json
from keras.models import Model
from PIL import Image as pil_image
from keras import backend as K
import numpy as np
import pickle
import os
import keras
import time
os.chdir(os.path.split(os.path.realpath(__file__))[0])
print(os.getcwd())
def load_vgg16_model():"""從當前目錄下的vgg16_exported.json和vgg16_exported.h5文件導入vgg16網絡并返回創建的網絡模型 # Returns 創建的網絡模型Models """
with open("G:/KeepOnStudying/ML/Documents/CV/PycharmProjects/Image_captioning/task1/vgg16_exported.json", "r") as json_file:
loaded_model_json= json_file.read()
loaded_model= model_from_json(loaded_model_json)
loaded_model.load_weights("G:/KeepOnStudying/ML/Documents/CV/PycharmProjects/Image_captioning/task1/vgg16_exported.h5")
return loaded_model
def preprocess_input(x):"""預處理圖像用于網絡輸入,將圖像由RGB格式轉換為BGR格式 將圖像的每一個圖像通道減去其均值 #:argument numpy數組,4維 data_format: Data format of the Image array # Returns preprocessed numpy array """
x_red = x[:, :, :, 0].copy()
x[:, :, :, 0] = x[:, :, :, 2]
x[:, :, :, 2] = x_red
x[:, :, :, 0] -= np.mean(x[:, :, :, 0])
x[:, :, :, 1] -= np.mean(x[:, :, :, 1])
x[:, :, :, 2] -= np.mean(x[:, :, :, 2])
return x
def load_img_as_np_array(path,target_size):"""從指定文件加載圖像,轉換圖像為target_size,返回2位浮點數numpy數組, :param path: 圖像文件路徑 :param target_size:元組(圖像高度,圖像寬度) :returns A PIL Image instance """
img=pil_image.open(path)
img=img.resize(target_size,pil_image.NEAREST)
return np.asarray(img,dtype=K.floatx())
def extract_features(directory):"""提取給定文件夾中所有圖像的特征,將提取的特征保存在features.pkl中 提取的文件保存在一個dict中,key為文件名,value為特征值【np.array】 :param directory:包含.JPG文件的文件夾, :returns:None """
model = load_vgg16_model()
model.layers.pop()
model = Model(inputs=model.inputs, outputs=model.layers[-1].output)
print("the model used is summarized as follow:")
model.summary()
features = {}
count = 0
for fn in os.listdir(directory):
img_id = os.path.splitext(fn)[0]
if (img_id[-4] == "."): continue
count += 1
print("[" + str(count) + "] " + img_id)
fn = os.path.join(directory, fn)
arr = load_img_as_np_array(fn, (224, 224))
arr = np.reshape(arr, (1, arr.shape[0], arr.shape[1], arr.shape[2]))
arr = preprocess_input(arr)
feature = model.predict(arr, verbose=0)
features[str(img_id)] = feature
return features
if __name__ == '__main__':
# 提取所有圖像的特征,保存在一個文件中, 大約一小時的時間,最后的文件大小為127M
directory='G:/KeepOnStudying/ML/Documents/CV/PycharmProjects/Image_captioning/task1/Flicker8K'
time_start=time.time()
features = extract_features(directory)
time_end=time.time()
print("time cost : ",format(time_end-time_start))
print("提取特征文件個數: " ,format(len(features)))
print(keras.backend.image_data_format())
#保存特征到文件
pickle.dump(features,open('features.pkl','wb'))
print("I am lucky and finish this summary for sharing")
print('Your evalution and correction is waited')
print('Thanks a lot for your good teaching, especially Jerry')
總結
以上是生活随笔為你收集整理的open cv提取图片特征值_基于VGG16网络提取Flicker8K数据集图像特征的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: mysql between and 包含
- 下一篇: linux c 编程 pdf_C/C++