-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
126 lines (101 loc) · 3.49 KB
/
main.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
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
# Packages
from fastapi import Form, File, UploadFile, FastAPI
from fastapi.staticfiles import StaticFiles
from fastapi.encoders import jsonable_encoder
from typing import List
from pydantic import BaseModel
import uuid
import os
from PIL import Image
from sscar import Search_Setup,Load_Data # Our custom script base on DeepImageSearch library
import json
from bson.json_util import dumps
# Import the MongoDB collection that we will use to store information
from mongoDB.mongodb import collection
#Generate unique ID to identify listing
def get_uuid_id():
return str(uuid.uuid4())
#APP
app = FastAPI()
app.mount("/sscar", StaticFiles(directory="sscar"), name="sscar")
#Model of the information that we need to store
class CarInformation(BaseModel):
id: str
name: str
price: int
images: List[str]
# Save multiple images into one single folder named with ID
def save_images_into_folder(listing_path,images):
os.mkdir(listing_path)
for image in images:
img = Image.open(image.file)
path = listing_path+image.filename
img.save(path)
# Save information and images path in MongoDB
def save_data_to_mongodb(new_listing):
collection.insert_one(jsonable_encoder(new_listing))
# Get Data from MongoDB that match id
def get_data_from_mongodb(ids):
result = collection.find({"id":{"$in":ids}})
return json.loads(dumps(result))
# Save the query image into a folder
def save_image(image):
queryImagesPath = f'sscar/query/'
img = Image.open(image.file)
path = queryImagesPath+image.filename
img.save(path)
return path
# Find cars ids that match the query
def get_similar_result(path,nbr_of_image):
img_list = st.get_similar_images(image_path=path,number_of_images=nbr_of_image)
imge_links = [img_list[id] for id in img_list.keys()]
cars = [car.split('/')[2] for car in imge_links[:-1]]
cars = list(set(cars))
return cars
# Setup image search
image_list = Load_Data().from_folder(["sscar/cars/logo"])
st = Search_Setup(image_list=image_list, image_count=2)
st.run_index()
# Add new car Algorithm
@app.post("/car")
async def add_new_listing(
name: str = Form(...),
price: float = Form(...),
images: List[UploadFile] = File(...),
):
# 1 - Generate a unique ID
id = get_uuid_id()
# 2 - Generate a path to save new listing images init
new_listing_path = f'sscar/cars/{id}/'
# 3 - Generate a specific path for each image (this list we save it in Mongodb)
image_list = [new_listing_path+file.filename for file in images]
# 4 - Save images into the folder
save_images_into_folder(new_listing_path,images)
# 5 - Extract features and index images
st.add_images_to_index(image_list)
# 6 - Create an object that we save in Mongodb
new_listing = CarInformation(
id=id,
name=name,
price=price,
images= image_list
)
# 7 - Save object into MongoDB
save_data_to_mongodb(new_listing)
# 8 - Return the object as a response
return {
"Ok! Car add to sscar" : new_listing,
}
# Search using image algorithm
@app.post('/search')
async def search_by_img(image: UploadFile):
# 1 - Get and save the query image into the path
path = save_image(image)
# 2 - Get cars id that matches query image
car_ids = get_similar_result(path,3)
# 3 - Get information using id from Mongodb
result = get_data_from_mongodb(car_ids)
# 4 - Return results
return {"result": result}
if __name__ == '__main__':
app.run(debug=True)