首页 > 解决方案 > 如何在 MySQL 8 中保存 phash 并计算汉明距离?

问题描述

与给定的 phash 相比,我试图在 MySQL 8.0 表中找到类似的图像。

阶段是通过 python 生成的,当前存储在 varchar(255) 字段中。通常它们看起来像这样:ae95916ec1354a9d

我的查询应该以字节为单位返回差异:

SELECT m.*,
       BIT_COUNT( 0xae95916ec1354a9d ^ m.HASH) as hd,
       BIT_COUNT( 0xae95916ec1354a9d ^ concat('0x', m.HASH)) as hd,
       concat('0x', m.HASH)
from
     media m
where m.HASH is not null
ORDER BY hd ASC;

不幸的是,这不起作用,我怀疑这是因为哈希前面缺少 0x。

哈希在 Python 中是这样生成的:

    response2 = requests.get(url, stream=True)
    if response2:
        response2.raw.decode_content = True
        image2 = Image.open(response2.raw)
        hash2 = str(imagehash.phash(image2))

我是否将哈希保存在错误的数据类型字段中,或者这种方法有什么问题?

标签: pythonmysqlimage-comparisonphash

解决方案


推荐阅读