python+milvus实现一个以图搜图系统

引言

当您听到“以图搜图”时,是否首先想到了百度、Google 等搜索引擎的以图搜图功能呢?事实上,您完全可以搭建一个属于自己的以图搜图系统:自己建立图片库;自己选择一张图片到库中进行搜索,并得到与其相似的若干图片。

说明

大概需要三个步骤

  1. 准备数据
  2. 训练数据
  3. 查询数据

准备数据

在这里插入图片描述
在这里插入图片描述

训练数据

在这里插入图片描述

训练数据的步骤这里是用了python方式集成towhee使用resnet50模型训练提出特征向量,存储到milvus,技术背景自行去了解,本文只整理操作步骤

安装minlvus(docker-compose方式)

1)、下载docker-compose.yml

wget https://github.com/milvus-io/milvus/releases/download/v2.1.4/milvus-standalone-docker-compose.yml -O docker-compose.yml

2)、docker启动docker-compose

docker-compose up -d

启动成功会显示

Creating milvus-etcd  ... done
Creating milvus-minio ... done
Creating milvus-standalone ... done

查询容器状态

docker-compose ps
      Name                     Command                  State                            Ports
--------------------------------------------------------------------------------------------------------------------
milvus-etcd         etcd -advertise-client-url ...   Up             2379/tcp, 2380/tcp
milvus-minio        /usr/bin/docker-entrypoint ...   Up (healthy)   9000/tcp
milvus-standalone   /tini -- milvus run standalone   Up             0.0.0.0:19530->19530/tcp, 0.0.0.0:9091->9091/tcp

觉得不清楚的可以参考官方文档
https://milvus.io/docs/v2.1.x/install_standalone-docker.md

python集成milvus+towhee

官方项目Demo包含前后端,其中后端python实现,前端演示react实现
https://github.com/milvus-io/bootcamp/tree/master/solutions/image/reverse_image_search/one_step

提取特征向量的核心代码

import towhee
from towhee.functional.option import _Reason

class ResNet50:
    def __init__(self):
        self.pipe = (towhee.dummy_input()
                    .image_decode()
                    .image_embedding.timm(model_name='resnet50')
                    .tensor_normalize()
                    .as_function()
        )
      
    def resnet50_extract_feat(self, img_path):
        feat = self.pipe(img_path)
        if isinstance(feat, _Reason):
            raise feat.exception
        return feat


if __name__ == "__main__":
    ResNet50().resnet50_extract_feat('https://github.com/towhee-io/towhee/raw/main/towhee_logo.png')

python后端启动方式

启动之前,首先修改config.py,将host修改成自己部署milvus服务器的host

MILVUS_HOST = os.getenv("MILVUS_HOST", "127.0.0.1")
MILVUS_PORT = int(os.getenv("MILVUS_PORT", "19530"))

然后安装依赖,启动

$ cd bootcamp/solutions/reverse_image_search/one_step/server
$ pip install -r requirements.txt
$ python main.py

在这里插入图片描述

启动前端

注意:

  • 前端demo只是一个功能演示,可以自己使用postman调用后端接口测试
  • 确保自己有nodejs环境

前端目录:cd bootcamp/solutions/reverse_image_search/one_step/client
1、首先修改/src/utils/Endpoints.ts中后端调用服务器端口,就是刚刚启动python项目端口
在这里插入图片描述

2、启动,我这边用的yarn

yarn install
yarn  start

在这里插入图片描述
浏览器输入http://localhost:3000/
在这里插入图片描述
填入需要训练入库的图片文件夹,点击+
在这里插入图片描述
我们其实可以f12查看其接口的调用,刚刚的训练数据已经入库,现在显示有14个特征
在这里插入图片描述

查询数据

在这里插入图片描述

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
Python中,你可以使用requests库发送POST请求来实现以图搜图功能。首先,你需要导入requests库并定义一个函数,比如test0()。在函数中,你需要指定搜图的URL和相关参数,比如图片的URL和页码。然后,使用requests.post()方法发送POST请求,并打印返回的结果。\[1\] 另外,Python也提供了许多其他库来获取图片的长宽等信息。比如,你可以使用Python图像库(PIL)来打开图片,并使用其size属性来获取图片的长宽信息。除了PIL,还有其他高质量的库,如wxPython和Twisted等,也可以用来处理图片和获取相关信息。\[2\] 需要注意的是,以图搜图功能通常是由搜索引擎提供的,比如百度和谷歌。如果你想搭建自己的以图搜图系统,你需要建立自己的图片库,并选择一张图片进行搜索,然后得到与其相似的若干图片。这需要一些额外的工作和算法来实现。\[3\] #### 引用[.reference_title] - *1* [python 谷歌以图搜图](https://blog.csdn.net/lwdfzr/article/details/124805045)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [以图搜图算法pytorch,Python以图搜图爬虫](https://blog.csdn.net/mr_yu_an/article/details/126965770)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [python+milvus实现一个以图搜图系统](https://blog.csdn.net/weixin_43316101/article/details/130076870)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值