茶叶网站的建设策划书,做期货看资讯什么网站好,物流单号查询网站建设,图表统计类手机网站开发目录 前言总体设计系统整体结构图系统流程图 运行环境爬虫模型训练实际应用 模块实现1. 数据准备1#xff09;爬虫下载原始图片2#xff09;手动筛选图片 2. 数据处理1#xff09;切割得到人物脸部2#xff09;重新命名处理后的图片3#xff09;添加到数据集 3. 模型训练及… 目录 前言总体设计系统整体结构图系统流程图 运行环境爬虫模型训练实际应用 模块实现1. 数据准备1爬虫下载原始图片2手动筛选图片 2. 数据处理1切割得到人物脸部2重新命名处理后的图片3添加到数据集 3. 模型训练及保存1设置基本参数2模型保存3模块预测 相关其它博客工程源代码下载其它资料下载 前言
本项目通过爬虫技术获取图片利用OpenCV库对图像进行处理识别并切割出人物脸部形成了一个用于训练的数据集。通过ImageAI进行训练最终实现了对动漫人物的识别模型。同时本项目还开发了一个线上Web应用使得用户可以方便地体验和使用该模型。
首先项目使用爬虫技术从网络上获取图片。这些图片包含各种动漫人物其中我们只对人物脸部进行训练所以我们会对图像进行处理并最终将这些图像将作为训练数据的来源。
其次利用OpenCV库对这些图像进行处理包括人脸检测、图像增强等步骤以便准确识别并切割出人物脸部。这一步是为了构建一个清晰而准确的数据集用于模型的训练。
接下来通过ImageAI进行训练。ImageAI是一个简化图像识别任务的库它可以方便地用于训练模型这里用于训练动漫人物的识别模型。
最终通过项目开发的线上Web应用用户可以上传动漫图像系统将使用训练好的模型识别图像中的动漫人物并返回相应的结果。
总的来说本项目结合了爬虫、图像处理、深度学习和Web开发技术旨在提供一个便捷的动漫人物识别服务。这对于动漫爱好者、社交媒体平台等有着广泛的应用前景。
总体设计
本部分包括系统整体结构图和系统流程图。
系统整体结构图
系统整体结构如图所示。 系统流程图
系统流程如图所示。 运行环境
本部分包括爬虫、模型训练及实际应用运行环境。
爬虫
安装Python3.6以上及Selenium3.0.2版本。
详见博客。
模型训练
本部分包括安装依赖、安装ImageAI。
详见博客。
实际应用
实际应用包括前端开发环境和后端环境的搭建。
详见博客。
模块实现
本项目包括4个模块数据准备、数据处理、模型训练及保存、模型测试下面分别介绍各模块的功能及相关代码。
1. 数据准备
本项目的数据来自于百度图片,通过爬虫获取。
1爬虫下载原始图片
详见博客。
2手动筛选图片
部分人物的名称、现实事物或人物有重名现象,加上一些图片质量不佳,需要人为剔除,手动筛选。
详见博客。
2. 数据处理
将图片中的人脸裁剪进行模型训练,切割人脸部分由OpenCV通过训练好的动漫人物脸部识别模型lbpcascade_animeface截取人物脸部。GitHub下载地址为https://github.com/nagadomi/lbpcascade_animeface。
1切割得到人物脸部
相关代码如下:
#基本参数设定
SRC Raw #待处理的文件路径
DST Data #处理后的文件路径
TRAIN_PER 5 #训练的图片比例
TEST_PER 1 #测试的图片比例
#处理原图片得到人物脸部图片并按比例分配训练和测试用于训练模型
for image_file in files: #读取所有图片image_file image_file.replace(\\, /) #解决Windows下的文件路径问题target_path /.join(image_file.strip(/).split(/)[1:-1])target_path os.path.join(dst, target_path) /if not os.path.exists(target_path):os.makedirs(target_path)
count len(os.listdir(target_path)) 1
image cv2.imdecode(np.fromfile(image_file, dtypenp.uint8), -1)
#解决中文路径读入图片问题gray cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #OpenCV的设置gray cv2.equalizeHist(gray) #转化为灰度图片faces cascade.detectMultiScale(gray,scaleFactor1.05, #指定每个图像缩放比例缩小图像大小的参数minNeighbors4, #此参数将影响检测到的面孔值越高检测结果越少质量越好minSize(24, 24) #最小对象大小或者小于此值的对象将被忽略for (x, y, w, h) in faces:crop_img image[y:y h, x:x w]crop_img cv2.resize(crop_img, (96, 96)) #重置为96*96filename os.path.basename(image_file).split(.)[0] cv2.imencode(.jpg,crop_img)[1].tofile(os.path.join(target_path, str(count) .jpg)) #保存切割的脸部处理前和处理后的效果如图所示。 处理前图片 处理后图片 2重新命名处理后的图片
对于处理后的图片需要重新指定文件名称以便于统计和处理。相关代码如下:
def rename_files(dir, prefix, joiner_, startNum0,changeType, ignoreType, typeOnly):重命名一个文件夹中的所有文件Args:dir(string): 重命名文件夹的路径prefix(string): 文件名前缀joiner(string): 连接文件名前缀和数字的连接符默认为下划线startNum(int): 重命名文件的开始数字默认为0changeType(string): 把文件重命名为指定类型默认不指定类型typeOnly(string): 只处理指定类型的文件使用空格分割例如“.jpg .jpeg .png .bmp .webp”ignoreType(string): 忽略处理文件的类型使用空格分割例如“.py .docx”for root, _, files in os.walk(dir):root root.replace(\\, /)if prefix :prefix root.split(/)[-1]count startNumfor file in files:true_type os.path.splitext(file)[-1] #文件真实类型type_list typeOnly.split()ignore_list ignoreType.split()if true_type in type_list or len(type_list) 0:if true_type in ignore_list:continueif changeType : #是否指定改变类型file_type true_typeelse:file_type changeTypenew_name {}{}{}{}.format(prefix, joiner, str(count), file_type)path os.path.join(root, new_name)old_path os.path.join(root, file)if old_path path:continueif not os.path.exists(path):os.rename(old_path, path)count count 1
def main():parser argparse.ArgumentParser(description重命名指定文件夹下的所有文件)parser.add_argument(dir, typestr, help重命名文件的路径)parser.add_argument(--prefix, -p, typestr,default, help前缀默认为文件名)parser.add_argument(--joiner, -j, typestr, default_, help连接符)parser.add_argument(--startNum, -s, typeint, default0, help开始数)parser.add_argument(--changeType, -c, typestr,default, help重命名文件为指定类型)parser.add_argument(--ignoreType, -i, typestr,default, help忽略处理的类型使用空格分割)parser.add_argument(--typeOnly, -t, typestr,default, help指定处理的类型使用空格分割)args parser.parse_args()rename_files(dirargs.dir, joiner_temp_, ignoreTypeargs.ignoreType, typeOnlyargs.typeOnly)rename_files(dirargs.dir, prefixargs.prefix, joinerargs.joiner, startNumargs.startNum,changeTypeargs.changeType, ignoreTypeargs.ignoreType, typeOnlyargs.typeOnly)
print(Rename files finished)3添加到数据集
已经切割得到的脸部经过重新排序命名后按照一定的比例添加到数据集。相关代码如下
def divide_train_test(src, train_percentage5, test_percentage1):if not os.path.exists(src):print(folder %s is not exist % src)returndirs os.listdir(src)test_dir os.path.join(src, test)train_dir os.path.join(src, train) #训练数据路径if not os.path.exists(test_dir):os.mkdir(test_dir)if not os.path.exists(train_dir):os.mkdir(train_dir)for dir_name in dirs:if dir_name ! test and dir_name ! train:current_dir os.path.join(src, dir_name)test_dir os.path.join(src, test, dir_name) #测试集路径train_dir os.path.join(src, train, dir_name) #训练集路径if not os.path.exists(test_dir):os.mkdir(test_dir)if not os.path.exists(train_dir):os.mkdir(train_dir)if os.path.isdir(current_dir):images os.listdir(current_dir)image_num len(images)for image in images:filename os.path.basename(image).split(.)[0]if filename.isdigit():percentage train_percentage test_percentagetest_num (image_num / percentage) * test_percentage 1if int(filename) test_num:if not os.path.exists(os.path.join(test_dir, image)):shutil.move(os.path.join(current_dir, image), os.path.join(test_dir))else:os.remove(os.path.join(current_dir, image))else:if not os.path.exists(os.path.join(train_dir, image)):shutil.move(os.path.join(current_dir, image), os.path.join(train_dir))else:os.remove(os.path.join(current_dir, image))shutil.rmtree(current_dir)for dirs in os.listdir(src):for name in os.listdir(os.path.join(src, dirs)):if os.path.isdir(os.path.join(src, dirs, name)):rename_file(os.path.join(src, dirs, name))print(Set all cropped images to train and test)3. 模型训练及保存
本部分包括设置基本参数、模型保存和模块预测。
1设置基本参数
相关代码如下
DATA_PATH Datas #数据集路径
TRAIN_NUM 30 #训练次数
BATCH 5 #批次
model_trainer ModelTraining()
model_trainer.setModelTypeAsResNet() #训练算法
model_trainer.setDataDirectory(data_path) #训练目录
model_trainer.trainModel(num_objectsnum_obj,
#该参数用于指定图像数据集中对象的数量
num_experimentstrain_num, #该参数用于指定图像训练的次数
enhance_dataTrue, #该参数用于指定是否生成训练图像的副本以获得更好的性能
batch_sizebatch, #该参数用于指定批次数量分批训练直到所有批次训练集都完成为止
show_network_summaryTrue #该参数用于指定是否在控制台中显示训练的过程 2模型保存
模型每次训练完成都会输出一个.h5文件和对应的.json文件,如图1所示。model_class.json文件中包含人物名称molde_ex-xxx_acc_xxxxxx.h5中ex后的数字表示训练次数acc后的数字表示对应的精度。model_class.json文件中的人物名称如图2所示采用Unicode编码。训练好的模型保存后可重复使用也可移植到其他环境中使用。 图1 训练模型后输出结果 图2 model_class.json文件中的人物名称 3模块预测
相关代码如下
#设置基本参数
IMAGE_PATH uploader/ #预测图片路径
MODEL_PATH data/models/model_ex-150_acc-0.883871.h5 #模型路径
JSON_PATH data/json/model_class.json #json文件路径
RESULT_COUNT 3 #显示预测结果的数量
prediction CustomImagePrediction() #初始化ResNet
prediction.setModelTypeAsResNet() #设置ResNet模型
#预测函数
def predict(img_path, model_pathMODEL_PATH, json_pathJSON_PATH, result_countRESULT_COUNT):if not os.path.exists(img_path):print(Can not found img %s % img_path)returnwith open(json_path) as f:num_obj len(json.load(f))print(num_obj)prediction.setModelPath(model_path)prediction.setJsonPath(json_path)prediction.loadModel(num_objectsnum_obj)predictions, probabilities prediction.predictImage(img_path, result_countresult_count)result {}i 1for eachPrediction, eachProbability in zip(predictions, probabilities):result[i]{eachPrediction: str(round(float(eachProbability), 2)) % }i i 1print(result)return result相关其它博客
基于opencvImageAItensorflow的智能动漫人物识别系统——深度学习算法应用(含python、JS、模型源码)数据集一
基于opencvImageAItensorflow的智能动漫人物识别系统——深度学习算法应用(含python、JS、模型源码)数据集二
基于opencvImageAItensorflow的智能动漫人物识别系统——深度学习算法应用(含python、JS、模型源码)数据集四
工程源代码下载
详见本人博客资源下载页 其它资料下载
如果大家想继续了解人工智能相关学习路线和知识体系欢迎大家翻阅我的另外一篇博客《重磅 | 完备的人工智能AI 学习——基础知识学习路线所有资料免关注免套路直接网盘下载》 这篇博客参考了Github知名开源平台AI技术平台以及相关领域专家DatawhaleApacheCNAI有道和黄海广博士等约有近100G相关资料希望能帮助到所有小伙伴们。