Update services.py
Browse files1234567891011121314151617181920212223242526272829303132333435# !/usr/bin/python3# -*- coding: utf-8 -*-"""@Author : Ailove------------------------------------@File : services.py@CreateTime : 2025/5/22 20:37------------------------------------"""from ImageSimilarity.utils import readimg, is_bottom_third_blank, detect_features, get_embedding, get_cos_similardef analyze_image(body):inputs = readimg(body, keys=['url'])img = list(inputs.values())[0]if img is None:raise ValueError("Image not found or invalid.")result = {"embedding": get_embedding(img).round(6).tolist(),"blank": bool(is_bottom_third_blank(img)),"features": detect_features(img)}return resultdef compare_images(body):print(body)keys = ["img_1", "url_1", "img_2", "url_2"]inputs = readimg(body, keys=[k for k in keys if k in body])img1, img2 = inputs.get("img_1") or inputs.get("url_1"), inputs.get("img_2") or inputs.get("url_2")if not img1 or not img2:raise ValueError("Both images must be provided.")emb1 = get_embedding(img1)emb2 = get_embedding(img2)sim = get_cos_similar(emb1, emb2)return {"similarity": float(round(sim, 6))}1234567891011121314151617181920212223242526272829303132333435# !/usr/bin/python3# -*- coding: utf-8 -*-"""@Author : Ailove------------------------------------@File : services.py@CreateTime : 2025/5/22 20:37------------------------------------"""from .utils import readimg, is_bottom_third_blank, detect_features, get_embedding, get_cos_similardef analyze_image(body):inputs = readimg(body, keys=['url'])img = list(inputs.values())[0]if img is None:raise ValueError("Image not found or invalid.")result = {"embedding": get_embedding(img).round(6).tolist(),"blank": bool(is_bottom_third_blank(img)),"features": detect_features(img)}return resultdef compare_images(body):print(body)keys = ["img_1", "url_1", "img_2", "url_2"]inputs = readimg(body, keys=[k for k in keys if k in body])img1, img2 = inputs.get("img_1") or inputs.get("url_1"), inputs.get("img_2") or inputs.get("url_2")if not img1 or not img2:raise ValueError("Both images must be provided.")emb1 = get_embedding(img1)emb2 = get_embedding(img2)sim = get_cos_similar(emb1, emb2)return {"similarity": float(round(sim, 6))}
services.py+35-35