style: reformat
This commit is contained in:
parent
3960d106c8
commit
fb20c58fe4
22
src/main.py
22
src/main.py
@ -47,15 +47,17 @@ def root():
|
||||
|
||||
return Response(renderTemplate("main.html", {"results": result}))
|
||||
|
||||
|
||||
class Image(BaseModel):
|
||||
file_name: str
|
||||
file_contents: str
|
||||
|
||||
|
||||
@app.post("/detect/")
|
||||
def file(file: Image):
|
||||
file_name = unquote(file.file_name)
|
||||
|
||||
with open("data/"+file_name, "wb") as f:
|
||||
with open("data/" + file_name, "wb") as f:
|
||||
f.write(b64decode(file.file_contents))
|
||||
|
||||
print("Working on: " + file_name)
|
||||
@ -68,25 +70,37 @@ def file(file: Image):
|
||||
assume_straight_pages=False,
|
||||
preserve_aspect_ratio=True,
|
||||
)
|
||||
doc = DocumentFile.from_images("data/"+file_name)
|
||||
doc = DocumentFile.from_images("data/" + file_name)
|
||||
pred_res = predictor(doc)
|
||||
json_res = pred_res.export()
|
||||
converted = convert_dict_items_to_list(json_res)
|
||||
|
||||
os.unlink("data/" + file_name)
|
||||
|
||||
return JSONResponse(content=converted)
|
||||
|
||||
except Exception as e:
|
||||
raise HTTPException(status_code=422, detail=str(e))
|
||||
|
||||
|
||||
def convert_to_list(value):
|
||||
if isinstance(value, dict):
|
||||
return {k:convert_to_list(v) for k,v in value.items()}
|
||||
return {k: convert_to_list(v) for k, v in value.items()}
|
||||
elif isinstance(value, list):
|
||||
return [convert_to_list(item) if isinstance(item, (dict, np.ndarray)) else item.tolist() if isinstance(item, np.ndarray) else item for item in value]
|
||||
return [
|
||||
convert_to_list(item)
|
||||
if isinstance(item, (dict, np.ndarray))
|
||||
else item.tolist()
|
||||
if isinstance(item, np.ndarray)
|
||||
else item
|
||||
for item in value
|
||||
]
|
||||
elif isinstance(value, np.ndarray):
|
||||
return value.tolist()
|
||||
else:
|
||||
return value
|
||||
|
||||
|
||||
def convert_dict_items_to_list(d: dict):
|
||||
converted = {}
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user