当前位置: 首页 > wzjs >正文

做网站 写文章怎样加视频wordpress+整合js

做网站 写文章怎样加视频,wordpress+整合js,wordpress4.5注册插件,石家庄建站工具常规练手,图片搜索山寨版。拜读罗云大佬著作,结果只有操作层的东西可以上上手。 书中是自己写的向量数据库,这边直接用python拼个现成的milvus向量数据库。 1. 创建一个向量数据库以及对应的相应数据表: # Milvus Setup Argume…

常规练手,图片搜索山寨版。拜读罗云大佬著作,结果只有操作层的东西可以上上手。

书中是自己写的向量数据库,这边直接用python拼个现成的milvus向量数据库。

1. 创建一个向量数据库以及对应的相应数据表:

# Milvus Setup Arguments
COLLECTION_NAME = 'animal_search'
DIMENSION = 2048
MILVUS_HOST = "localhost"
MILVUS_PORT = "19530"# Inference Arguments
BATCH_SIZE = 128from pymilvus import connections# Connect to the instance
connections.connect(host=MILVUS_HOST,port=MILVUS_PORT)from pymilvus import utility# Remove any previous collection with the same name
if utility.has_collection(COLLECTION_NAME):utility.drop_collection(COLLECTION_NAME)#创建保存ID、图片文件路径及Embeddings的Collection。
from pymilvus import FieldSchema, CollectionSchema, DataType, Collectionfields = [FieldSchema(name='id',dtype=DataType.INT64, is_primary=True, auto_id=True),FieldSchema(name='filepath', dtype=DataType.VARCHAR,max_length=200),FieldSchema(name='image_embedding',dtype=DataType.FLOAT_VECTOR,dim=DIMENSION)]
schema = CollectionSchema(fields=fields)
collection = Collection(name=COLLECTION_NAME, schema=schema)index_params = {'metric_type':'L2','index_type': "IVF_FLAT",'params':{'nlist':16384}
}
collection.create_index(field_name="image_embedding",index_params=index_params)
collection.load()

2. 写一堆图片进去存着,向量其实就是各种像素间的维度特征,

# Milvus Setup Arguments
COLLECTION_NAME = 'animal_search'
DIMENSION = 2048
MILVUS_HOST = "localhost"
MILVUS_PORT = "19530"# Inference Arguments
BATCH_SIZE = 128from pymilvus import connections# Connect to the instance
connections.connect(host=MILVUS_HOST, port=MILVUS_PORT)import globpaths = glob.glob('/mcm/vectorDB_training/animals_db/*',recursive=True)#分批预处理数据
import torch
# Load the embedding model with the last layer removed
model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)
model = torch.nn.Sequential(*(list(model.children())[:-1]))
model.eval()from torchvision import transforms
# Preprocessing for images
preprocess = transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean=[0.485,0.456,0.406],std=[0.229,0.224,0.225])
])#插入数据
from PIL import Image
from tqdm import tqdm# Embed function that embeds the batch and inserts it
def embed(data):from pymilvus import FieldSchema, CollectionSchema, DataType, Collectionfields = [FieldSchema(name='id',dtype=DataType.INT64, is_primary=True, auto_id=True),FieldSchema(name='filepath', dtype=DataType.VARCHAR,max_length=200),FieldSchema(name='image_embedding',dtype=DataType.FLOAT_VECTOR,dim=DIMENSION)]schema = CollectionSchema(fields=fields)collection = Collection(name=COLLECTION_NAME, schema=schema)with torch.no_grad():output = model(torch.stack(data[0])).squeeze()collection.insert([data[1],output.tolist()])collection.flush()data_batch = [[],[]]# Read the images into batches for embedding and insertion
for path in tqdm(paths):im = Image.open(path).convert('RGB')data_batch[0].append(preprocess(im))data_batch[1].append(path)if len(data_batch[0]) % BATCH_SIZE == 0:embed(data_batch)data_batch = [[],[]]# Embed and insert the remainder
if len(data_batch[0]) != 0:embed(data_batch)

3. 向量化图片的函数要单独拎出来,做搜索功能的时候用它。

import torch
import torchvision.transforms as transforms
from torchvision.models import resnet50
from PIL import Imagedef extract_features(image_path):# 加载预训练的 ResNet-50 模型model = resnet50(pretrained=True)model = torch.nn.Sequential(*list(model.children())[:-1])  #移除fc层,不移除,向量最后就是1000层,而不是2048model.eval()# 图像预处理preprocess = transforms.Compose([transforms.Resize(256),transforms.CenterCrop(224),transforms.ToTensor(),transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),])# 读取图像img = Image.open(image_path)img_t = preprocess(img)batch_t = torch.unsqueeze(img_t, 0)# 提取特征with torch.no_grad():out = model(batch_t)# 将特征向量转换为一维数组并返回return out.flatten().numpy()

4. 用flask做的界面

from flask import Flask,request,jsonify
from flask import render_template
from image_eb import extract_features
#from pymilvus import connections
from pymilvus import MilvusClientimport logging
import os
import shutilMILVUS_HOST = "localhost"
MILVUS_PORT = "19530"
COLLECTION_NAME = 'animal_search'
TOP_K = 3app = Flask(__name__)
milvus_client = MilvusClient(uri="http://localhost:19530")@app.route("/")
def index():return render_template("index.html")@app.route("/upload",methods=["POST"])
def upload_image():image_file = request.files["image"]image_id_str = request.form.get("image_id")data = []#检查image_id是否存在。if not image_id_str:return jsonify({"message": "Image ID is required"}),400#image id转化为整型try:image_id = int(image_id_str)data.append(image_id)except ValueError:return jsonify({"message": "Invalid image ID. It must be an integer"}),400filename = image_file.filenameimage_path = os.path.join("static/images",image_id_str)image_file.save(image_path)image_features = extract_features(image_path)data.append(image_features)data_dict = dict(filepath=image_path,image_embedding=data[1])#更新数据库中记录milvus_client.insert(collection_name=COLLECTION_NAME,data=[data_dict])return jsonify({"message": "Image uploaded successfully", "id": image_id})@app.route("/search",methods=["POST"])
def search_image():image_file = request.files["image"]image_path = os.path.join("static/images","temp_image.jpg")image_file.save(image_path)image_features = extract_features(image_path)data_li = [extract_features(image_path).tolist()]search_result = milvus_client.search(collection_name=COLLECTION_NAME,data=data_li,output_fields=["filepath"],limit=TOP_K,search_params={'metric_type': 'L2', 'params': {}},)dict_search_result = search_result[0]arr_search_result = []destination_folder = '/mcm/vectorDB_training/static/images'for index,value in enumerate(dict_search_result):source_file = value["entity"]["filepath"]base_file_name = os.path.basename(source_file)destination_file = os.path.join(destination_folder, base_file_name)shutil.copy(source_file, destination_file)key_file_name = os.path.join("/static/images",base_file_name)arr_search_result.append(key_file_name)        image_urls = [f"{filepath}" for filepath in arr_search_result]return jsonify({"image_urls":image_urls})if __name__=="__main__":app.run(host='0.0.0.0',port=5020,debug=True)

小网站结构,以及其他杂代码,可以查看以及直接下载:https://www.ituring.com.cn/book/3305


文章转载自:

http://Q63Zv2eA.hqhLx.cn
http://3vJWqghC.hqhLx.cn
http://ttFKlDeI.hqhLx.cn
http://oRJtQHht.hqhLx.cn
http://KQe04se0.hqhLx.cn
http://2rordhdl.hqhLx.cn
http://4L6Zo716.hqhLx.cn
http://epLrhUXm.hqhLx.cn
http://QdzUd6I5.hqhLx.cn
http://HxP5OCTk.hqhLx.cn
http://Ck8V74ei.hqhLx.cn
http://FNbIfUnN.hqhLx.cn
http://s2HeOccN.hqhLx.cn
http://xEqH01gs.hqhLx.cn
http://qTGhFBTn.hqhLx.cn
http://D5VNqdr1.hqhLx.cn
http://aPR1Ynfv.hqhLx.cn
http://tfocACcM.hqhLx.cn
http://Y3YpYKRa.hqhLx.cn
http://lOuvFX6Y.hqhLx.cn
http://qXOHSI4i.hqhLx.cn
http://XiEQEk3A.hqhLx.cn
http://Tg6t0tUd.hqhLx.cn
http://7rw1uA0G.hqhLx.cn
http://NbGD3Xrn.hqhLx.cn
http://M4VNUI0T.hqhLx.cn
http://hbo4ZvDk.hqhLx.cn
http://iuFVOavF.hqhLx.cn
http://ZLVlXuAb.hqhLx.cn
http://xyEx70fH.hqhLx.cn
http://www.dtcms.com/wzjs/633093.html

相关文章:

  • 网站开发竞聘报告宁波建设工程检测行业协会网站
  • 资源分享类网站模板南京外贸网站建设公司排名
  • 做外贸网站进行销售 需要纳税吗网站规划步骤有哪些
  • 我想注册网站我怎么做医社保增减员在什么网站做
  • 网页制作与网站建设实验报告wordpress 收录
  • 莆田市秀屿区建设局网站广州网站建设推广公司有哪些
  • 教育网站模板下载注册外贸公司需要多少钱
  • 做网站设计制作的公司企业网站属于下面哪种媒体类型
  • 基于thinkphp网站制作专门做2次元图片的网站
  • 手机怎么上wap网站杭州鼎易科技做网站太坑
  • 微信制作网站开发mvc 门户网站开发框架
  • 阿里巴巴国际网站建设平台网站建设设计
  • 做海淘的网站要哪些证在县城做团购网站
  • 南山网站建设公司wordpress 屏蔽特定国家ip
  • 上海免费网站建设模板推荐seo搜索引擎优化步骤
  • linux网站开发软件网站开发 阿里
  • 广州火车站国家企业信息填报系统登录
  • 可视化的做网站的app简洁好看的网站
  • 网站用词精准性网站被挂黑链排名降权
  • 自己做网站还是开通阿里巴巴诚信通深圳网站建设ln12345
  • 微网站建设晋江小学网站建设
  • 站长之家seo工具包自已怎样网站
  • 网站建设学习资料目前做汽配的网站有哪些
  • php做网站界面代码下载好的网站模板怎么用
  • 花都网站建设网页设计开发一个企业官网多少钱
  • 优质网站的衡量标准如何查询个人名下企业
  • 网站建设的图片鞍山建设信息网站
  • 长治网站制作服务电商推广都有哪些诀窍
  • html电子商务网站模版wordpress图片输出
  • 个人网站开发总结文档网络推广与营销