Try using File() instead of UploadFile()

This commit is contained in:
niten 2023-01-08 16:26:09 -08:00
parent 8c6b0fa2fe
commit 24327fe351
1 changed files with 27 additions and 11 deletions

View File

@ -1,6 +1,6 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from fastapi import FastAPI, HTTPException, Request, UploadFile from fastapi import FastAPI, HTTPException, Request, UploadFile, File
from fastapi.responses import FileResponse from fastapi.responses import FileResponse
from detector import Detector from detector import Detector
import tempfile import tempfile
@ -89,20 +89,36 @@ def result_to_dict(res, base_url):
"output": base_url + d.outfile, "output": base_url + d.outfile,
} }
@app.put("/images/") # @app.put("/images")
def analyze_image(file: UploadFile, request: Request): # def analyze_image(file: UploadFile(), request: Request):
# print("Initiating file receipt, url: " + str(request.url))
# base_url = re.sub(r'\/images\/$', '/analyzed_images/', str(request.url))
# infile = open(incoming_dir / file.filename)
# file_hash = hashlib.sha256()
# with open(infile, mode="wb") as f:
# chunk = f.read(buffer_size)
# while chunk:
# print("writing chunk")
# file_hash.update(chunk)
# infile.write(chunk)
# chunk=f.read(buffer_size)
# print("saving complete")
# print("analyzing image")
# result = detector.detect_objects(infile, file_hash.hexdigest() + ".png")
# print("image analyzed")
# remove(infile)
# return result_to_dict(result, base_url)
@app.put("/images")
def analyze_image(request: Request, incoming_data: bytes = File()):
print("Initiating file receipt, url: " + str(request.url)) print("Initiating file receipt, url: " + str(request.url))
base_url = re.sub(r'\/images\/$', '/analyzed_images/', str(request.url)) base_url = re.sub(r'\/images\/$', '/analyzed_images/', str(request.url))
infile = open(incoming_dir / file.filename) infile = open(incoming_dir / file.filename)
file_hash = hashlib.sha256() file_hash = hashlib.sha256()
with open(infile, mode="wb") as f: file_hash.update(incoming_data)
chunk = f.read(buffer_size) with open(infile, "w") as f:
while chunk: f.write(incoming_data)
print("writing chunk") print("save complete")
file_hash.update(chunk)
infile.write(chunk)
chunk=f.read(buffer_size)
print("saving complete")
print("analyzing image") print("analyzing image")
result = detector.detect_objects(infile, file_hash.hexdigest() + ".png") result = detector.detect_objects(infile, file_hash.hexdigest() + ".png")
print("image analyzed") print("image analyzed")