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

图像识别快速实现

文本的跑通了,接下来玩玩图片场景

1. 引入模型

再另起类test_qdrant_img.py,转化图片用到的模型和文本不太一样,我们这里使用ResNet-50模型

import unittest
from qdrant_client.http.models import Distance, VectorParams
from qdrant_client import QdrantClient
import torch
import torchvision.transforms as transforms
from PIL import Imageclass TestQDrantImg(unittest.TestCase):def setUp(self):self.collection_name = "img_collection"self.client = QdrantClient("localhost", port=6333)# 加载ResNet-50模型self.model = torch.hub.load('pytorch/vision:v0.10.0', 'resnet50', pretrained=True)self.model.eval()# 图像预处理self.preprocess = transforms.Compose([# 图像调整为256*256transforms.Resize(256), # 中心裁剪为224*224transforms.CenterCrop(224), # 转换为张量,像素值从范围[0,255]缩放到范围[0,1],RGB(红绿蓝)转换为通道顺序(即 RGB 顺序)transforms.ToTensor(), # 应用归一化,减去均值(mean)并除以标准差(std)transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])])

2. 添加图片向量

我们先创建一个新集合

def test_create_collection(self):self.client.create_collection(collection_name=self.collection_name,vectors_config=VectorParams(size=1000, distance=Distance.EUCLID),)

往集合里分别添加1个猫的图片和1个狗的图片

    def test_img_vector(self):# 加载并预处理图像id = 1image_path = './img/cat1.png'# id = 2# image_path = './img/dog1.png'image = Image.open(image_path)image_tensor = self.preprocess(image)# 在第0维度上添加一个维度,将图像张量转换为形状为 (1, C, H, W) 的张量,其中 C 是通道数,H 是高度,W 是宽度image_tensor = torch.unsqueeze(image_tensor, 0)with torch.no_grad():# 去除维度为1的维度,将特征向量的形状从 (1, D) 转换为 (D,)feature_vector = self.model(image_tensor).squeeze().tolist()operation_info = self.client.upsert(collection_name=self.collection_name,points=[{'id': id, 'vector': feature_vector, 'payload': {"image_path": image_path}}])print(operation_info)

3. 匹配图片向量

然后用其他猫狗的图片来做搜索匹配

    def test_search(self):# 加载并预处理图像image_path = './img/cat2.png'# image_path = './img/dog2.png'# image_path = './img/cat3.png'image = Image.open(image_path)image_tensor = self.preprocess(image)image_tensor = torch.unsqueeze(image_tensor, 0)with torch.no_grad():feature_vector = self.model(image_tensor).squeeze().tolist()search_result = self.client.search(collection_name=self.collection_name, query_vector=feature_vector, limit=3, with_vectors=True, with_payload=True)print(search_result)

结果:

[ScoredPoint(id = 1, version = 0, score = 68.21013, payload = {

'image_path': './img/cat1.png'

}, vector = [...]),

ScoredPoint(id = 2, version = 1, score = 85.10757, payload = {

'image_path': './img/dog1.png'

}, vector = [...])]

当使用猫2猫3作为查询条件时,跟猫1记录的score(向量距离)较小;

同理,使用狗2作为查询条件时,跟狗1记录的score(向量距离)较小

http://www.lryc.cn/news/274319.html

相关文章:

  • 一文详解动态 Schema
  • Web网页开发-总结笔记2
  • C#的StringBuilder方法
  • 美格智能5G RedCap模组SRM813Q通过广东联通5G创新实验室测试认证
  • MVCC 并发控制原理-源码解析(非常详细)
  • 通过国家网络风险管理方法提供安全的网络环境
  • input中typedate的属性都有那些
  • 将PPT4页并排成1页
  • iPhone 恢复出厂设置后如何恢复数据
  • 欧洲最好的AI大模型:Mistral 7B!(开源、全面超越Llama 2)
  • Python | 诞生、解析器的分类版本及安装
  • vim学习记录
  • bat脚本:将ini文件两行值转json格式
  • scratch绘制小正方形 2023年12月中国电子学会图形化编程 少儿编程 scratch编程等级考试四级真题和答案解析
  • 【产品应用】一体化伺服电机在管道检测机器人中的应用
  • Django在urls.py利用函数path()配置路由时传递参数给调用的视图函数的方法
  • Ubuntu20 编译 Android 12源码
  • RFID传感器|识读器CNS-RFID-01/1S在AGV小车|搬运机器人领域的安装与配置方法
  • 用友U8 Cloud smartweb2.RPC.d XML外部实体注入漏洞
  • 220.【2023年华为OD机试真题(C卷)】考勤信息(滑动窗口算法-JavaPythonC++JS实现)
  • 2024最新SLAM实习、秋招面经(百度、华为、小米、蔚来、理想、美团、阿里菜鸟……)
  • Html5实用个人博客留言板模板源码
  • 解码 Elasticsearch 查询 DSL:利用 Elasticsearch 中的 has_child 和 has_parent 查询进行父子文档搜索
  • 架构(1)
  • 第8课 将推流端与播放端合并为一对一音视频聊天功能
  • 如何保障集团下达的政策要求有效落地
  • 霍尔传感器测速测距实验——STM32驱动(课程设计)
  • 数据库——SQL注入攻击
  • 【已解决】js定义对象属性是.如何访问
  • Linux入门攻坚——11、Linux网络属性配置相关知识1