-
Notifications
You must be signed in to change notification settings - Fork 1
/
vectorizer_api.py
81 lines (61 loc) · 2.22 KB
/
vectorizer_api.py
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
from re import I
from flask import Flask, request, jsonify
from PIL import Image
from datastore import Datastore
from vectorizer_deepface import Vectorizer
import uvicorn
from fastapi import FastAPI, File, UploadFile, Response
from starlette.responses import StreamingResponse
from fastapi.responses import FileResponse,HTMLResponse
import io
import cv2
import numpy as np
import utils
import time
app = FastAPI()
file_name_mappings = utils.get_file_name_mappings()
mappings = utils.get_mappings()
@app.post("/vectorize/image")
async def vectorize_api(identifier: int, file: UploadFile = File(...)):
ret = {}
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png","jfif")
if not extension:
return "Image must be of proper format!"
contents = await file.read()
image = Image.open(io.BytesIO(contents))
opencvImage = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
vec = Vectorizer()
embeddings = vec.vectorize_single(opencvImage)
ret["id"] = identifier
ret["embeddings"] = embeddings
print(ret)
return ret
@app.post("/vectoriz/search")
async def search_api(file: UploadFile = File(...)):
ret = {}
extension = file.filename.split(".")[-1] in ("jpg", "jpeg", "png", "jfif")
if not extension:
return "Image must be of proper format"
contents = await file.read()
image = Image.open(io.BytesIO(contents))
opencvImage = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
vec = Vectorizer()
store = Datastore()
emb = np.array(vec.vectorize_single(opencvImage), dtype = np.float32)
emb = emb.reshape(1, 128)
starttime = time.time()
Distances, Identifiers = store.search(emb)
endtime = time.time()
print(f"time taken {endtime-starttime}")
print(Distances, Identifiers)
# print(type(Identifiers[0][0]))
# print(type(Distances[0][0]))
paths = [file_name_mappings[str(idx)] for idx in Identifiers[0]]
possible_names = [mappings[str(idx)] for idx in Identifiers[0]]
ret["ids"] = Identifiers[0].tolist()
ret["distance"] = Distances[0].tolist()
ret["results"] = possible_names
ret["paths"] = paths
return ret
if __name__ == "__main__":
uvicorn.run(app, debug=True)