Spaces:
Running
Running
File size: 5,505 Bytes
38a2ef1 0d2544a de0f224 38a2ef1 45b771c 38a2ef1 45b771c 38a2ef1 35d85ac 38a2ef1 45b771c 38a2ef1 45b771c a0787f2 9c36111 38a2ef1 55f7150 38a2ef1 55f7150 0d2544a 14b8c90 c8aef77 38a2ef1 a0787f2 45b771c c8aef77 45b771c 0d2544a 38a2ef1 0d2544a 38a2ef1 0d2544a 38a2ef1 0d2544a 8f0b65d 2a8236b 8f0b65d 2a8236b 8f0b65d 38a2ef1 |
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 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 |
from typing import List
from fastapi import FastAPI, HTTPException
from fastapi.responses import JSONResponse
from models import RequestModel
import os
import json
import cv2
import numpy as np
import httpx
BASE_DIR = "saved_data"
app = FastAPI()
def orb_sim(img1, img2):
# ORB
orb = cv2.ORB_create()
kp_a, desc_a = orb.detectAndCompute(img1, None)
kp_b, desc_b = orb.detectAndCompute(img2, None)
# Brute-force matcher
bf = cv2.BFMatcher(cv2.NORM_HAMMING, crossCheck=True)
matches = bf.match(desc_a, desc_b)
similar_regions = [i for i in matches if i.distance < 20]
if len(matches) == 0:
return 0
return len(similar_regions) / len(matches)
def load_image_url(source):
Image.MAX_IMAGE_PIXELS = None
if source.startswith('http'):
response = requests.get(source)
img = np.asarray(bytearray(response.content), dtype=np.uint8)
img = cv2.imdecode(img, cv2.IMREAD_GRAYSCALE)
else:
img = base64.b64decode(source)
img = Image.open(BytesIO(img))
img = np.array(img)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
return img
app = FastAPI()
BASE_DIR = "/tmp/data"
@app.post("/save")
async def save(image_data: RequestModel):
os.makedirs(BASE_DIR, exist_ok=True)
filename = os.path.join(BASE_DIR, f"{image_data.originId}_{image_data.assetCode}.json")
img1 = load_image(image_data.originSource)
img2 = load_image(image_data.source)
similarity_orb = None
if img1 is not None and img2 is not None:
similarity_orb = orb_sim(img1, img2)
print(f"Similaridade ORB entre {image_data.originSource} e {image_data.source}: {similarity_orb}")
data_to_save = image_data.dict()
if similarity_orb is not None:
data_to_save["similarityOrb"] = similarity_orb
with open(filename, "w") as f:
json.dump(data_to_save, f, indent=4)
return True
@app.get("/files")
async def list_files():
try:
files_data = []
for filename in os.listdir(BASE_DIR):
filepath = os.path.join(BASE_DIR, filename)
if os.path.isfile(filepath):
try:
with open(filepath, "r") as f:
file_content = f.read() # Lê o conteúdo do ficheiro
# Tenta decodificar o conteúdo como JSON, se possível
try:
file_content_json = json.loads(file_content)
files_data.append({"filename": filename, "content": file_content_json})
except json.JSONDecodeError:
files_data.append({"filename": filename, "content": file_content}) # Se não for JSON, retorna o texto
except (IOError, OSError) as e:
raise HTTPException(status_code=500, detail=f"Erro ao ler o ficheiro {filename}: {e}")
return JSONResponse({"files_data": files_data})
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Diretório de dados não encontrado")
@app.get("/files/similar")
async def list_similar_files():
try:
files_data = []
for filename in os.listdir(BASE_DIR):
filepath = os.path.join(BASE_DIR, filename)
if os.path.isfile(filepath):
try:
with open(filepath, "r") as f:
file_content = f.read()
try:
file_content_json = json.loads(file_content)
# Check for similarityOrb and filter
if "similarityOrb" in file_content_json and file_content_json["similarityOrb"] > 0:
files_data.append({"filename": filename, "content": file_content_json})
except json.JSONDecodeError:
pass # Skip files that are not valid JSON
except (IOError, OSError) as e:
raise HTTPException(status_code=500, detail=f"Erro ao ler o ficheiro {filename}: {e}")
return JSONResponse({"files_data": files_data})
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Diretório de dados não encontrado")
@app.get("/files/find/{origin_id}")
async def get_file_by_origin_id(origin_id: int):
try:
for filename in os.listdir(BASE_DIR):
if filename.startswith(f"{origin_id}_") and filename.endswith(".json"):
filepath = os.path.join(BASE_DIR, filename)
if os.path.isfile(filepath):
try:
with open(filepath, "r") as f:
file_content = f.read()
try:
file_content_json = json.loads(file_content)
return JSONResponse({"filename": filename, "content": file_content_json})
except json.JSONDecodeError:
return JSONResponse({"filename": filename, "content": file_content})
except (IOError, OSError) as e:
raise HTTPException(status_code=500, detail=f"Erro ao ler o ficheiro {filename}: {e}")
raise HTTPException(status_code=404, detail=f"Ficheiro com originId '{origin_id}' não encontrado")
except FileNotFoundError:
raise HTTPException(status_code=404, detail="Diretório de dados não encontrado") |