File size: 1,179 Bytes
876d54c
 
 
 
 
 
 
 
 
191713a
876d54c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# !/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_similar

def 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 result


def 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))}