Give `UploadFile` another try

This commit is contained in:
niten 2023-01-09 10:53:11 -08:00
parent 8254b94a88
commit 1e3012bf7f
1 changed files with 8 additions and 4 deletions

View File

@ -111,14 +111,18 @@ def result_to_dict(res, base_url):
# return result_to_dict(result, base_url) # return result_to_dict(result, base_url)
@app.post("/images") @app.post("/images")
def analyze_image(request: Request, image: bytes = File()): def analyze_image(request: Request, image: UploadFile):
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 / image.filename)
file_hash = hashlib.sha256() file_hash = hashlib.sha256()
file_hash.update(image) file_hash.update(image)
with open(infile, "w") as f: with open(infile, "wb") as f:
f.write(image) chunk = image.read(buffer_size)
while chunk:
print ("writing " + buffer_size + " bytes")
f.write(chunk)
chunk = image.read(buffer_size)
print("save complete") print("save 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")