Spaces:
Running
Running
Update main.py
Browse files
main.py
CHANGED
@@ -4,6 +4,41 @@ from fastapi.responses import JSONResponse
|
|
4 |
import os
|
5 |
import json
|
6 |
from models import RequestModel
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
7 |
|
8 |
app = FastAPI()
|
9 |
|
@@ -15,8 +50,21 @@ async def save(image_data: RequestModel, request: Request):
|
|
15 |
print("RAW BODY RECEIVED →", body.decode())
|
16 |
os.makedirs(BASE_DIR, exist_ok=True)
|
17 |
filename = os.path.join(BASE_DIR, f"{image_data.originId}_{image_data.assetCode}.json")
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
with open(filename, "w") as f:
|
19 |
-
json.dump(
|
20 |
return True
|
21 |
|
22 |
@app.get("/files")
|
|
|
4 |
import os
|
5 |
import json
|
6 |
from models import RequestModel
|
7 |
+
import httpx # Para baixar imagens de URLs, se necessário
|
8 |
+
|
9 |
+
BASE_DIR = "saved_data"
|
10 |
+
app = FastAPI()
|
11 |
+
|
12 |
+
def orb_sim(img1, img2):
|
13 |
+
# ORB
|
14 |
+
orb = cv2.ORB_create()
|
15 |
+
kp_a, desc_a = orb.detectAndCompute(img1, None)
|
16 |
+
kp_b, desc_b = orb.detectAndCompute(img2, None)
|
17 |
+
|
18 |
+
# Brute-force matcher
|
19 |
+
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
|
20 |
+
matches = bf.match(desc_a, desc_b)
|
21 |
+
similar_regions = [i for i in matches if i.distance < 50]
|
22 |
+
if len(matches) == 0:
|
23 |
+
return 0
|
24 |
+
return len(similar_regions) / len(matches)
|
25 |
+
|
26 |
+
async def load_image(image_path: str):
|
27 |
+
"""Carrega uma imagem a partir de um caminho de arquivo ou URL."""
|
28 |
+
try:
|
29 |
+
if image_path.startswith("http://") or image_path.startswith("https://"):
|
30 |
+
async with httpx.AsyncClient() as client:
|
31 |
+
response = await client.get(image_path)
|
32 |
+
response.raise_for_status()
|
33 |
+
image_bytes = np.frombuffer(response.content, np.uint8)
|
34 |
+
img = cv2.imdecode(image_bytes, cv2.IMREAD_COLOR)
|
35 |
+
return img
|
36 |
+
else:
|
37 |
+
img = cv2.imread(image_path)
|
38 |
+
return img
|
39 |
+
except Exception as e:
|
40 |
+
print(f"Erro ao carregar a imagem {image_path}: {e}")
|
41 |
+
return None
|
42 |
|
43 |
app = FastAPI()
|
44 |
|
|
|
50 |
print("RAW BODY RECEIVED →", body.decode())
|
51 |
os.makedirs(BASE_DIR, exist_ok=True)
|
52 |
filename = os.path.join(BASE_DIR, f"{image_data.originId}_{image_data.assetCode}.json")
|
53 |
+
|
54 |
+
img1 = await load_image(image_data.originSource)
|
55 |
+
img2 = await load_image(image_data.source)
|
56 |
+
|
57 |
+
similarity_orb = None
|
58 |
+
if img1 is not None and img2 is not None:
|
59 |
+
similarity_orb = orb_sim(img1, img2)
|
60 |
+
print(f"Similaridade ORB entre {image_data.originSource} e {image_data.source}: {similarity_orb}")
|
61 |
+
|
62 |
+
data_to_save = image_data.dict()
|
63 |
+
if similarity_orb is not None:
|
64 |
+
data_to_save["similarityOrb"] = similarity_orb
|
65 |
+
|
66 |
with open(filename, "w") as f:
|
67 |
+
json.dump(data_to_save, f, indent=4)
|
68 |
return True
|
69 |
|
70 |
@app.get("/files")
|