python查找相似图片

pip install nanopq

Ks 是8的倍数,需要比数据维度小

向量长度需要是M的倍数
默认按照距离排序,非常不错


import datetime
import heapq

import os

import cv2
import nanopq
import numpy as np


import nanopq
import numpy as np
import shutil


def dHash(gray,height):
    #缩放8*8
    # gray=cv2.resize(img,(64,63),interpolation=cv2.INTER_CUBIC)
    #转换灰度图
    # gray=cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
    hash_str=''
    #每行前一个像素大于后一个像素为1,相反为0,生成哈希
    for i in range(height):
        for j in range(height):
            if gray[i,j]>gray[i,j+1]:
                hash_str=hash_str+'1'
            else:
                hash_str=hash_str+'0'
    return hash_str

def pq_dis():
    N, D = 10000, 128
    X = np.random.random((N, D)).astype(np.float32)  # 10,000 128-dim vectors
    query = np.random.random((D,)).astype(np.float32)  # a 128-dim vector

    # Instantiate with M=8 sub-spaces
    pq = nanopq.PQ(M=8,Ks=256)

    # Train with the top 1000 vectors
    pq.fit(X[:1000])

    # Encode to PQ-codes
    X_code = pq.encode(X)  # (10000, 8) with dtype=np.uint8

    time1=datetime.datetime.now()
    # Results: create a distance table online, and compute Asymmetric Distance to each PQ-code
    dists = pq.dtable(query).adist(X_code)

    nsmallestList = heapq.nsmallest(5, dists)
    print(nsmallestList)
    indexs=[dists.tolist().index(i) for i in nsmallestList]
    print(indexs)
    print(dists[indexs])
    print("time",(datetime.datetime.now()-time1).microseconds)

if __name__ == '__main__':
    path=r"C:\Users\Administrator\Documents\Tencent Files\441648051\FileRecv\pic_error"
    files=os.listdir(path)

    datas=[]
    for file in files:
        img_1=cv2.imread(path+"/"+file,0)
        img1 = cv2.resize(img_1, (65, 64), interpolation=cv2.INTER_LINEAR)
        dhash=dHash(img1,64)
        data= list(map(int,dhash))
        datas.append(data)
    datas=np.asarray(datas,dtype=np.float32)
    N=len(datas)
    D=64*64

    query =datas[0]# np.random.random((D,)).astype(np.float32)  # a 128-dim vector

    # Instantiate with M=8 sub-spaces
    pq = nanopq.PQ(M=8,Ks=48)

    # Train with the top 1000 vectors
    pq.fit(datas)

    # Encode to PQ-codes
    X_code = pq.encode(datas)  # (10000, 8) with dtype=np.uint8

    time1=datetime.datetime.now()
    # Results: create a distance table online, and compute Asymmetric Distance to each PQ-code
    dists = pq.dtable(query).adist(X_code)

    nsmallestList = heapq.nsmallest(54, dists)
    print(nsmallestList)
    indexs=[dists.tolist().index(i) for i in nsmallestList]
    print(indexs)
    for i in indexs:
        print(files[i])
        shutil.copy(path+"/"+files[i], "out/"+str(dists[i])+"_"+files[i])
    print("time",(datetime.datetime.now()-time1).microseconds)
  • 0
    点赞
  • 11
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

AI视觉网奇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值