图片哈希计算算子:支持 URL、Base64、二进制三类输入格式,统一输出十六进制与二进制哈希。
ahash、dhash、phash、whash、md5image_url、image_base64、image_binaryimage_src_type 参数匹配md5 与感知哈希的输出将统一为 64 位二进制与 16 位十六进制imagededup.methods,若依赖不可用将返回空字符串ahashdhashphashwhashmd5输入列名 | 说明 |
|---|---|
image | 包含图片数据的数组,元素类型为字符串或二进制 ( |
一个结构化结果数组,其中每个元素包含以下字段:
hash_hex (str): 图像的十六进制哈希值。对于 md5 方法,该值为 16 个字符(截取完整摘要的前 64 位)。hash_bin (str): 图像的二进制哈希值。对于 md5 方法,该值为 64 位。如参数没有默认值,则为必填参数
参数名称 | 类型 | 默认值 | 描述 |
|---|---|---|---|
| str |
| 输入图片的来源类型。可选值: |
| str |
| 哈希计算方法。可选值: |
| int |
| 批处理大小,用于在吞吐量和内存消耗之间进行平衡。 |
下面的代码展示了如何使用 daft 调用 ImageHash 算子来计算图像的感知哈希值。
from __future__ import annotations import os import daft from daft import col from daft.las.functions.image.image_hash import ImageHash from daft.las.functions.udf import las_udf if __name__ == "__main__": if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.context.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "image": [ f"https://{tos_dir_url}/public/shared_image_dataset/cat_ip_adapter.jpeg" ] } ds = daft.from_pydict(samples) ds = ds.with_column( "image_hash", las_udf( ImageHash, construct_args={ "image_src_type": "image_url", "method": "phash", }, num_gpus=0, batch_size=1, concurrency=1, )(col("image")), ) ds.show() # ╭────────────────────────────────┬────────────────────────────────────────╮ # │ image ┆ image_hash │ # │ --- ┆ --- │ # │ Utf8 ┆ Struct[hash_hex: Utf8, hash_bin: Utf8] │ # ╞════════════════════════════════╪════════════════════════════════════════╡ # │ https://las-cn-beijing-public… ┆ {hash_hex: 8d3986a636e768ad, │ # │ ┆ … │ # ╰────────────────────────────────┴────────────────────────────────────────╯