-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathapp.py
More file actions
64 lines (52 loc) · 1.65 KB
/
Copy pathapp.py
File metadata and controls
64 lines (52 loc) · 1.65 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
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
from fastapi import FastAPI, UploadFile, File
from fastapi.staticfiles import StaticFiles
import numpy as np
import faiss
import pickle
import torch
from torchvision import transforms
from torchvision import models
from PIL import Image
import io
app = FastAPI()
app.mount("/images", StaticFiles(directory="dataset/images"), name="images")
index = faiss.read_index("faiss_index.bin")
with open("image_paths.pkl", "rb") as f:
image_paths = pickle.load(f)
model = models.resnet50(pretrained = True)
model = torch.nn.Sequential(*list(model.children())[:-1])
model.eval()
transform = transforms.Compose([
transforms.Resize((224,224)),
transforms.ToTensor(),
transforms.Normalize(
mean = [0.485, 0.456, 0.405],
std = [0.229, 0.224, 0.229]
)
])
@app.get("/")
def home():
return {"message" : "API is running"}
def get_embedding(image):
image = transform(image).unsqueeze(0)
with torch.no_grad():
embedding = model(image)
return embedding.squeeze().numpy().astype("float32")
@app.post("/search")
async def search(file: UploadFile = File(...)):
contents = await file.read()
image = Image.open(io.BytesIO(contents)).convert("RGB")
query_vector = get_embedding(image)
query_vector = np.expand_dims(query_vector, axis = 0)
k = 5
distances, indices = index.search(query_vector, k)
results = [f"http://127.0.0.1:8000/images/{image_paths[i].split("/")[-1]}" for i in indices[0]]
return {"results" : results}
from fastapi.middleware.cors import CORSMiddleware
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)