使用elasticSearch实现以图搜图
逛github的时候发现了这个有趣的项目
项目地址:https://github.com/sethuiyer/Image-to-Image-Search
实现原理
使用CaptionGenerator对图片生成说明
CaptionGenerator可以识别某一个图片并生成描述该图片的语言
比方说下面这张图片,CaptionGenerator会生成:一个男人在冲浪
将生成的图片描述和图片地址保存到elasticsearch
查询图片时生成查询图片的描述,将此描述放到elasticsearch去进行匹配,以此显示出匹配度相近的图片
快速安装
elasticsearch只能在linux系统上安装,所以需要一台虚拟机或者是云服务器,Python程序可以跑在云服务器或者是本地
我的配置:
-
elasticsearch6.0
-
java1.8
-
centos7.6
-
python3.6
实现步骤:
-
安装好以上环境
-
下载项目到本地https://github.com/sethuiyer/Image-to-Image-Search
-
将此模型https://cs.stanford.edu/people/karpathy/neuraltalk/flickr8k_cnn_lstm_v1.zip下载解压后放入models 文件夹下
-
根据requirements.txt文件安装Python包,下面的是我自己改的requirements.txt,按照项目里的版本跑不了
absl-py==0.7.1 astor==0.7.1 bleach==3.3.0 Click==7.0 elasticsearch==6.3.1 enum34==1.1.6 Flask==1.0.3 gast==0.2.2 grpcio==1.19.0 h5py==2.9.0 html5lib==0.9999999 itsdangerous==1.1.0 Jinja2==2.11.3 Keras==2.2.4 Keras-Applications==1.0.7 Keras-Preprocessing==1.0.9 Markdown==3.1 MarkupSafe==1.1.1 mock==2.0.0 numpy==1.16.2 pbr==5.1.3 Pillow==8.1.1 protobuf==3.7.1 PyYAML==5.4 scipy==1.2.1 six==1.12.0 tensorboard==1.13.1 tensorflow==2.4.0 tensorflow-estimator==1.13.0 tensorflow-tensorboard==0.1.8 termcolor==1.1.0 urllib3==1.24.2 Werkzeug==0.15.3
-
如果你的项目和elasticsearch在一台机子上就跳过,否则需要将文件server.py和index_database.py里的
es = Elasticsearch()
改为es = Elasticsearch([{'host': 'xxx', 'port': 9200}], timeout=3600)
,xxx改为安装了elasticsearch机子的ip -
运行index_database.py,添加信息到elasticsearch,可以访问http://xxx:9200/desearch/_search查看是否添加成功
-
接着运行server.py,启动web服务,打开http://127.0.0.1:5000/search即可搜图
![]() | ![]() |
---|---|
![]() | ![]() |