diff --git a/src/objectifier.py b/src/objectifier.py index d0cf2a2..84ea028 100644 --- a/src/objectifier.py +++ b/src/objectifier.py @@ -1,6 +1,6 @@ #!/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 detector import Detector import tempfile @@ -89,20 +89,36 @@ def result_to_dict(res, base_url): "output": base_url + d.outfile, } -@app.put("/images/") -def analyze_image(file: UploadFile, request: Request): +# @app.put("/images") +# 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)) 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") + file_hash.update(incoming_data) + with open(infile, "w") as f: + f.write(incoming_data) + print("save complete") print("analyzing image") result = detector.detect_objects(infile, file_hash.hexdigest() + ".png") print("image analyzed")