山西门户网站建设,玄武建设局网站,做游戏都需要什么网站,dw做网站一般需要多大尺寸目录 前言总体设计系统整体结构图系统流程图 运行环境爬虫模型训练实际应用 模块实现1. 数据准备1#xff09;爬虫下载原始图片2#xff09;手动筛选图片 相关其它博客工程源代码下载其它资料下载 前言
本项目通过爬虫技术获取图片#xff0c;利用OpenCV库对图像进行处理爬虫下载原始图片2手动筛选图片 相关其它博客工程源代码下载其它资料下载 前言
本项目通过爬虫技术获取图片利用OpenCV库对图像进行处理识别并切割出人物脸部形成了一个用于训练的数据集。通过ImageAI进行训练最终实现了对动漫人物的识别模型。同时本项目还开发了一个线上Web应用使得用户可以方便地体验和使用该模型。
首先项目使用爬虫技术从网络上获取图片。这些图片包含各种动漫人物其中我们只对人物脸部进行训练所以我们会对图像进行处理并最终将这些图像将作为训练数据的来源。
其次利用OpenCV库对这些图像进行处理包括人脸检测、图像增强等步骤以便准确识别并切割出人物脸部。这一步是为了构建一个清晰而准确的数据集用于模型的训练。
接下来通过ImageAI进行训练。ImageAI是一个简化图像识别任务的库它可以方便地用于训练模型这里用于训练动漫人物的识别模型。
最终通过项目开发的线上Web应用用户可以上传动漫图像系统将使用训练好的模型识别图像中的动漫人物并返回相应的结果。
总的来说本项目结合了爬虫、图像处理、深度学习和Web开发技术旨在提供一个便捷的动漫人物识别服务。这对于动漫爱好者、社交媒体平台等有着广泛的应用前景。
总体设计
本部分包括系统整体结构图和系统流程图。
系统整体结构图
系统整体结构如图所示。 系统流程图
系统流程如图所示。 运行环境
本部分包括爬虫、模型训练及实际应用运行环境。
爬虫
安装Python3.6以上及Selenium3.0.2版本。
详见博客。
模型训练
本部分包括安装依赖、安装ImageAI。
详见博客。
实际应用
实际应用包括前端开发环境和后端环境的搭建。
详见博客。
模块实现
本项目包括4个模块数据准备、数据处理、模型训练及保存、模型测试下面分别介绍各模块的功能及相关代码。
1. 数据准备
本项目的数据来自于百度图片,通过爬虫获取。
1爬虫下载原始图片
下图为下载人物的部分列表。 爬虫可根据列表自动下载指定数量的人物图片存放于指定文件夹相关代码如下:
#phantomjs设置
dcap dict(DesiredCapabilities.PHANTOMJS)
dcap[phantomjs.page.settings.userAgent] (Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/54.0.2840.100
)
#根据关键词爬取并下载图片
#下载基本参数设置更多参数设置在main()函数处
NAME_LIST characters_name_list.txt #导入需要获取关键字文件每个关键字一行
MAX_NUM 60 #每个关键字下载数量
OUTPUT_PATH ./Raw #下载图片书保存目录
TIME_OUT 20 #设置超时
DELAY 1 #随机下载延迟0~1秒同样防止被服务器识别出爬虫
#产生随机的header防止被服务器识别出爬虫
def get_random_headers():ua UserAgent().random #产生随机的User-Agentheaders {Accept:text/html,application/xhtmlxml,application/xml;q0.9,image/webp,*/*;q0.8,Proxy-Connection: keep-alive,User-Agent: ua,Accept-Encoding: gzip, deflate, sdch,}return headersdef get_image_url(keywords, max_number10000, face_onlyFalse):#获取图像urldef decode_url(url): #解码urlin_table 0123456789abcdefghijklmnopqrstuvwout_table 7dgjmoru140852vsnkheb963wtqplifcatranslate_table str.maketrans(in_table, out_table)mapping {_z2C$q: :, _ze3B: ., AzdH3F: /}for k, v in mapping.items():url url.replace(k, v)return url.translate(translate_table)base_url https://image.baidu.com/search/acjson?tnresultjson_comipnrjct201326592\lm7fpresultieutf-8oeutf-8st-1keywords_str word{}queryWord{}.format(quote(keywords), quote(keywords))query_url base_url keywords_strquery_url face{}.format(1 if face_only else 0)init_url query_url pn0rn30res requests.get(init_url)init_json json.loads(res.text.replace(r\, ), encodingutf-8, strictFalse)total_num init_json[listNum]target_num min(max_number, total_num)crawl_num min(target_num * 2, total_num)crawled_urls list()batch_size 30with futures.ThreadPoolExecutor(max_workers5) as executor:future_list list()def process_batch(batch_no, batch_size): #批处理image_urls list()url query_url \pn{}rn{}.format(batch_no * batch_size, batch_size)try_time 0while True:try:response requests.get(url)breakexcept Exception as e:try_time 1if try_time 3:print(e)return image_urlsresponse.encoding utf-8res_json json.loads(response.text.replace(r\, ), encodingutf-8, strictFalse)for data in res_json[data]:if objURL in data.keys():image_urls.append(decode_url(data[objURL]))elifreplaceUrlin data.keys() and len(data[replaceUrl]) 2:image_urls.append(data[replaceUrl][1][ObjURL])return image_urlsfor i in range(0, int((crawl_num batch_size - 1) / batch_size)):future_list.append(executor.submit(process_batch, i, batch_size))for future in futures.as_completed(future_list):if future.exception() is None:crawled_urls future.result()else:print(future.exception())return crawled_urls[:min(len(crawled_urls), target_num)]def get_image_url(keywords, max_number10000, face_onlyFalse):def decode_url(url): #解码in_table 0123456789abcdefghijklmnopqrstuvwout_table 7dgjmoru140852vsnkheb963wtqplifcatranslate_table str.maketrans(in_table, out_table)mapping {_z2C$q: :, _ze3B: ., AzdH3F: /}for k, v in mapping.items():url url.replace(k, v)return url.translate(translate_table)base_url https://image.baidu.com/search/acjson?tnresultjson_comipnrjct201326592\lm7fpresultieutf-8oeutf-8st-1keywords_str word{}queryWord{}.format(quote(keywords), quote(keywords))query_url base_url keywords_strquery_url face{}.format(1 if face_only else 0)init_url query_url pn0rn30res requests.get(init_url)init_json json.loads(res.text.replace(r\, ), encodingutf-8, strictFalse)total_num init_json[listNum]target_num min(max_number, total_num)crawl_num min(target_num * 2, total_num)crawled_urls list()batch_size 30with futures.ThreadPoolExecutor(max_workers5) as executor:future_list list()def process_batch(batch_no, batch_size):image_urls list()url query_url \pn{}rn{}.format(batch_no * batch_size, batch_size)try_time 0while True:try:response requests.get(url)breakexcept Exception as e:try_time 1if try_time 3:print(e)return image_urlsresponse.encoding utf-8res_json json.loads(response.text.replace(r\, ), encodingutf-8, strictFalse)for data in res_json[data]:if objURL in data.keys():image_urls.append(decode_url(data[objURL]))elifreplaceUrl in data.keys() and len(data[replaceUrl]) 2:image_urls.append(data[replaceUrl][1][ObjURL])return image_urlsfor i in range(0, int((crawl_num batch_size - 1) / batch_size)):future_list.append(executor.submit(process_batch, i, batch_size))for future in futures.as_completed(future_list):if future.exception() is None:crawled_urls future.result()else:print(future.exception())return crawled_urls[:min(len(crawled_urls), target_num)]#main函数
def main(list_file, output./Raw, max_number100, threads50, timeout20, time_delay3, face_onlyTrue,browserphantomjs, quietFalse, file_prefiximg):with open(list_file, encodingutf-8) as keywords_list:for keywords in keywords_list:keywords keywords.rstrip() #去除换行符if keywords : #跳过空行continueif os.path.exists(os.path.join(output, keywords)): #查看是否已经下载print([warn: ] [{}] is already downloaded, downloader will skip [{}].format(keywords, keywords))continuecrawled_urls crawler.crawl_image_urls(keywords, max_numbermax_number, face_onlyface_only,browserbrowser, quietquiet)download_images(image_urlscrawled_urls, dst_diroutput, keywordskeywords,concurrencythreads, timeouttimeout,time_delaytime_delay, file_prefixfile_prefix)img_count len(os.listdir(os.path.join(output, keywords)))print([{}]: get {} image(s).format(keywords, img_count))def download_image(image_url, dst_dir, file_name, timeout20, time_delay1):time.sleep(random.randint(0, time_delay)) #暂停0~time_delay的整数秒response Nonefile_path os.path.join(dst_dir, file_name)try_times 0while True:try:try_times 1response requests.get(image_url, headersget_random_headers(), timeouttimeout)with open(file_path, wb) as f:f.write(response.content)response.close()file_type imghdr.what(file_path)if file_type in [jpg, jpeg, png, bmp]:new_file_name {}.{}.format(file_name, file_type)new_file_path os.path.join(dst_dir, new_file_name)shutil.move(file_path, new_file_path)print([OK:] {} {}.format(new_file_name, image_url))else:os.remove(file_path)print([Err:] file type err or not exists {}.format(image_url))breakexcept Exception as e:if try_times 3:continueif response:response.close()print([Fail:] {} {}.format(image_url, e.args))breakdef download_images(image_urls, dst_dir, keywords, file_prefiximg, concurrency50, timeout20, time_delay1):with concurrent.futures.ThreadPoolExecutor(max_workersconcurrency) as executor:future_list list()count 0dst_dir os.path.join(dst_dir, keywords)if not os.path.exists(dst_dir):os.makedirs(dst_dir)for image_url in image_urls:file_name file_prefix _ %04d % countfuture_list.append(executor.submit(download_image, image_url, dst_dir, file_name, timeout, time_delay))count 1concurrent.futures.wait(future_list, timeout180)结果如图所示。
2手动筛选图片
部分人物的名称、现实事物或人物有重名现象,加上一些图片质量不佳,需要人为剔除,手动筛选,如图所示。 相关其它博客
基于opencvImageAItensorflow的智能动漫人物识别系统——深度学习算法应用(含python、JS、模型源码)数据集一
基于opencvImageAItensorflow的智能动漫人物识别系统——深度学习算法应用(含python、JS、模型源码)数据集三
基于opencvImageAItensorflow的智能动漫人物识别系统——深度学习算法应用(含python、JS、模型源码)数据集四
工程源代码下载
详见本人博客资源下载页 其它资料下载
如果大家想继续了解人工智能相关学习路线和知识体系欢迎大家翻阅我的另外一篇博客《重磅 | 完备的人工智能AI 学习——基础知识学习路线所有资料免关注免套路直接网盘下载》 这篇博客参考了Github知名开源平台AI技术平台以及相关领域专家DatawhaleApacheCNAI有道和黄海广博士等约有近100G相关资料希望能帮助到所有小伙伴们。