-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
138 lines (119 loc) · 3.61 KB
/
app.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
127
128
129
130
131
132
133
134
135
136
137
138
from fastapi import FastAPI
from api_module import *
from helpers import *
from fastapi import FastAPI, UploadFile, File, Response
from PIL import Image
import numpy as np
from fastapi.middleware.cors import CORSMiddleware
from fastapi.responses import StreamingResponse
import io
import pandas as pd
import subprocess
from fastapi import FastAPI, Request
from starlette.responses import RedirectResponse
from pydantic import BaseModel
class Payload(BaseModel):
csv: str
app = FastAPI()
model = Searching()
app.add_middleware(
CORSMiddleware,
allow_origins=["*"],
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get("/text")
async def read_item(
text_query: str,
topk: int = 20,
object_filter: str = "",
voice_filter: str = "",
color_filter: str = "",
swap_top: str = "",
):
images, paths, csv = model.search(
text_query=text_query,
top_k=int(topk),
objects_filter_text=object_filter,
voice_filter_text=voice_filter,
color_filter=color_filter,
swap_top=swap_top,
)
return {"paths": paths, "csv": csv}
@app.post("/image")
async def read_item(
file: UploadFile = File(...),
topk: int = 20,
object_filter: str = "",
voice_filter: str = "",
color_filter: str = "",
swap_top: str = "",
):
image_query = convert_image(await file.read())
images, paths, csv = model.search(
image_query=image_query,
top_k=int(topk),
objects_filter_text=object_filter,
voice_filter_text=voice_filter,
color_filter=color_filter,
swap_top=swap_top,
)
return {"paths": paths, "csv": csv}
@app.post("/drawing") #route này buộc phải có filter mới chạy nổi ví dụ : Chopsticks (object)
async def read_item(
file: UploadFile = File(...),
topk: int = 20,
object_filter: str = "",
voice_filter: str = "",
color_filter: str = "",
swap_top: str = "",
):
image_query = np.array(Image.open(BytesIO(await file.read())))
images, paths, csv = model.search(
drawing_query=image_query,
top_k=int(topk),
objects_filter_text=object_filter,
voice_filter_text=voice_filter,
color_filter=color_filter,
swap_top=swap_top,
)
return {"paths": paths, "csv": csv}
@app.get("/stream/{_:path}")
async def show_image(path: str):
try:
image = Image.open(r"{}".format(path))
buf = BytesIO()
image.save(buf, format="JPEG")
return Response(content=buf.getvalue(), media_type="image/jpg")
except Exception as e:
print(e)
return "Error when stream image: {}".format(path)
@app.get("/explore/{_:path}")
def open_folder(path: str = ""):
path = path[:-11] #chỗ này không cố định, cần kiểm tra xóa cái đuôi ảnh ra
print("path", path)
try:
os.startfile(path)
return {"status": "sucess"}
except Exception as e:
print(e)
subprocess.Popen(["xdg-open", path])
return {"status": "sucess1"}
@app.get("/ytb/{_:path}")
def redirect_link(path: str):
# path: <tên video>/<Keyframe ID>.jpg Ví dụ: C00_V0135/029928.jpg
video_name = path.split("/")[0]
link_path = f'{META_LINK}\{video_name}.json'
f = open(link_path, encoding="utf8")
data = json.load(f)
url = data['watch_url']
print(url)
return RedirectResponse(url)
@app.post("/map")
def csv_mapping(payload: Payload):
TESTDATA = payload.csv
df = reverse_to_csv(TESTDATA)
map_df = map_keyframe(df)
csv = map_df.to_csv(header=False, index=False).encode('utf-8')
return {"csv": csv}