import gradio as gr import imagehash def compare_images(img1, img2): if img1 is None or img2 is None: return "画像を2つともアップロードしてください。" hashes = {} a_hash1 = imagehash.average_hash(img1) a_hash2 = imagehash.average_hash(img2) hashes["average_hash"] = a_hash1 - a_hash2 p_hash1 = imagehash.phash(img1) p_hash2 = imagehash.phash(img2) hashes["phash"] = p_hash1 - p_hash2 d_hash1 = imagehash.dhash(img1) d_hash2 = imagehash.dhash(img2) hashes["dhash"] = d_hash1 - d_hash2 d_hash_v1 = imagehash.dhash_vertical(img1) d_hash_v2 = imagehash.dhash_vertical(img2) hashes["dhash_vertical"] = d_hash_v1 - d_hash_v2 w_hash1 = imagehash.whash(img1) w_hash2 = imagehash.whash(img2) hashes["whash"] = w_hash1 - w_hash2 color_hash1 = imagehash.colorhash(img1) color_hash2 = imagehash.colorhash(img2) hashes["colorhash"] = color_hash1 - color_hash2 crop_resistant_hash1 = imagehash.crop_resistant_hash(img1) crop_resistant_hash2 = imagehash.crop_resistant_hash(img2) hashes["crop_resistant_hash"] = crop_resistant_hash1 - crop_resistant_hash2 return hashes with gr.Blocks() as iface: gr.Markdown("# ImageHash Playground") gr.Markdown("2つの画像をアップロードして、imagehashでハッシュの差を計算します。") with gr.Row(): img1 = gr.Image(type="pil") img2 = gr.Image(type="pil") output = gr.JSON() btn = gr.Button("計算") btn.click(compare_images, inputs=[img1, img2], outputs=output) iface.launch()