代码拉取完成,页面将自动刷新
同步操作将从 mynameisi/书法体识别APP 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
import os
import time
import yaml
import joblib
import pickle
from keras.applications.vgg16 import VGG16, preprocess_input
from keras.preprocessing import image
from skimage.feature import hog
from skimage import color
import numpy as np
from PIL import Image
# 加载配置文件
with open('0_setting.yaml', 'r', encoding='utf-8') as f:
setting = yaml.load(f, Loader=yaml.Loader)
def save_pickle(data, filename):
with open(filename, 'wb') as f:
pickle.dump(data, f)
# 获取配置文件中的键对应的值
def get(key):
return setting[key]
# 图像预处理函数
def preprocess_image(file_name, new_size):
img = Image.open(file_name)
img = img.resize(new_size, Image.ANTIALIAS)
# 转化为灰度图像
img = color.rgb2gray(np.array(img))
# 提取 HOG 特征
features = hog(img, orientations=9, pixels_per_cell=(8, 8),
cells_per_block=(2, 2), block_norm='L2-Hys')
return features
# 序列化对象并保存到本地
def dump(obj, name, loc):
file_dir = os.path.split(loc)[0]
# 判断文件夹路径是否存在,如果不存在,则创建
if not os.path.isdir(file_dir):
os.makedirs(file_dir)
start = time.time()
print(f"Saving {name} to {loc}...")
joblib.dump(obj, loc)
end = time.time()
print(f"Saved! Location: {loc}, Size: {os.path.getsize(loc) / 1024 / 1024:.3f}M")
print(f"Time taken: {end - start:.3f} seconds")
# 从本地加载对象并反序列化
def load(name, loc):
print(f"Loading {name} from {loc}...")
obj = joblib.load(loc)
return obj
def vgg16_features(img_path):
model = VGG16(weights='imagenet', include_top=False)
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = model.predict(x)
features_flatten = features.reshape(features.shape[0], -1)
return features_flatten
def combined_features(img_path, new_size):
hog_features = preprocess_image(img_path, new_size)
vgg16_features = extract_vgg16_features(img_path)
combined_features = np.concatenate([hog_features, vgg16_features], axis=1)
return combined_features
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。