forked from Chronopt-Research/ImageRetrievalAPI
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
36 lines (31 loc) · 1.27 KB
/
Copy pathmain.py
File metadata and controls
36 lines (31 loc) · 1.27 KB
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
from fastapi import FastAPI, File, UploadFile
from fastapi.responses import FileResponse
from utils.VectorDatabase import CosineSimVecDB
import cv2
from PIL import Image
import io
import secrets
import os
import time
import base64
# tempory_folder_path = "./temp/"
# def generate_random_token():
# token = secrets.token_hex(16)
# return token
app = FastAPI()
inMemDatabase = CosineSimVecDB("./embed_data","/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/","cuda")
# inMemDatabase = CosineSimVecDB(r"D:\resFes\Vector_database","/kaggle/input/imagenet-object-localization-challenge/ILSVRC/Data/CLS-LOC/train/","cpu")
@app.post("/reverseSearchImage/")
async def videoColorization(img: UploadFile):
img_bytes = await img.read()
# rand_file_name=generate_random_token()+".jpg"
img_search = Image.open(io.BytesIO(img_bytes)).convert("RGB")
response = {}
resp_img_paths = inMemDatabase.get_nearest_img_path(img_search)
# img_search.save(tempory_folder_path+"/"+rand_file_name)
for i in len(resp_img_paths):
# print(resp_img_paths[i])
with open(resp_img_paths[i], "rb") as image_file:
encoded_string = base64.b64encode(image_file.read())
response[f"img{str(i)}"]=encoded_string
return response