以图搜图(demo创建流程)
window10添加向量数据库以及调用
创建docker
1,在windows功能中打开Hyper-V 和 容器
2,进入https://www.docker.com/ ,下载windows版本进行安装
创建milvus及连接
1,创建milvus文件夹,在文件夹下建立conf,db,logs,pic,wal五个文件夹,把docker-compose.yml和server_config.yaml放如conf文件夹中,使用命令提示符进入到conf文件夹中,执行docker-compose up -d,执行结束后,再执行docker-compose up -d来查看运行状态,然后运行docker run -p 3000:3000 -e HOST_URL=http://192.168.10.171:3000 -e MILVUS_URL=192.168.10.171:19530 milvusdb/milvus-insight:latest 。在docker中即可看到
点击3000端口,通过3000接口进入到milvus界面。
2,python程序
1》创建集合
from pymilvus import FieldSchema, DataType, CollectionSchema, connections, Collection
#创建存储字段
id = FieldSchema(
name="id",
dtype=DataType.INT64,
is_primary=True,
)
vec = FieldSchema(
name="vec",
dtype=DataType.FLOAT_VECTOR,
dim=2048
)
schema = CollectionSchema(
fields=[id, vec],
description="Test search"
)
collection_name = "animal"
print("#########连接数据库##############")
connections.connect(
alias="default",
host='192.168.10.171',
port='19530'
)
print("#########根据上面得信息创建集合##############")
collection = Collection(
name=collection_name,
schema=schema,
using='default',
shards_num=2
)
print("#########关闭连接##############")
connections.disconnect("default")
2》添加数据
#print("#########连接数据库##############")
from towhee import pipeline
from pymilvus import connections, Collection
connections.connect(
alias="default",
host='192.168.10.171',
port='19530'
)
#print("#########插入数据##############")
p = pipeline('image-embedding')
output = p('http://localhost:54867/7.jpg')
data = [
[7],
[output],
]
collection = Collection("animal")
mr = collection.insert(data)
print(mr)
3》查询数据
import towhee
from django.http import HttpResponse
from pymilvus import connections,Collection
from towhee import pipeline
#获取图片向量查询的数据
def selectData(request):
connections.connect(host='192.168.10.171', port='19530')
t = (
towhee.glob['path']('E://milvus//coreCode//getImages//getImages//wwwroot//7.jpg')
.image_decode['path', 'img']()
.image_embedding.timm['img', 'vec'](model_name='resnet50')
.milvus_search['vec', 'results'](collection='animal')
.select['results']() # 选择指定列;
.to_list()
)
tr = "";
for i in t:
for j in i.results:
tr = tr + " " + str(j.id)
return HttpResponse(tr, content_type="application/json")
def selectvector(request):
#print("#########连接数据库##############")
connections.connect(
alias="default",
host='192.168.10.171',
port='19530'
)
#print("#########查询数据##############")
p = pipeline('image-embedding')
output = p('http://localhost:54867/7.jpg')
collection = Collection("animal")
search_params = {"metric_type": "L2", "params": {"nprobe": 1}, "search_length": 100}
tt = collection.search(
data=[output],
anns_field="vec",
param=search_params,
offset=0,
limit=3,
expr=None
)
#print("#########查询结果##############")
return HttpResponse(tt, content_type="application/json")
本文来自博客园,作者:zwbsoft,转载请注明原文链接:https://www.cnblogs.com/zwbsoft/p/17079523.html
电话微信:13514280351