Hi @Steve_in_Denver,
First Thing regarding the saving the zip file, we get a post request to the server{end point} we extract the form data, add save the zip file contents temporarily. code will be below,
async def face_recognition(request: Request):
form_data = await request.form()
logging.info('buid is: %s', request.query_params.get('buid'))
asyncio.create_task(background_task(
await save_zip_file(form_data["zip_file"]),
json.loads(form_data["hashmap"]),
request.query_params.get('identification'),
request.query_params.get('buid')
))
return {"message": "Background task started"}
above code is for fetching the args from request,
async def background_task(zip_path, hashmap_dict, identification, buid):
logging.info('Starting background task: Detection, Verification and Identification')
await asyncio.to_thread(image_rekognition, zip_path, hashmap_dict, identification, buid)
async def save_zip_file(uploaded_file):
# Save the zip file temporarily
filename = secure_filename(uploaded_file.filename)
zip_path = os.path.join(common_path, filename)
async with aiofiles.open(zip_path, 'wb') as f:
contents = await uploaded_file.read()
await f.write(contents)
print(zip_path)
return zip_path
def image_rekognition(zip_path, hashmap, identification, buid):
logging.info('Detect faces starting')
trip_id = str(uuid.uuid4())
# update_poll_time(buid, trip_id, identification)
detect_faces2(zip_path, trip_id, hashmap, buid)
def detect_faces2(zip_path, trip_id, hashmap, buid):
try:
# Extract the zip file
with zipfile.ZipFile(zip_path, 'r') as zip_ref:
zip_ref.extractall(image_directory)
image_files = [file for file in os.listdir(image_directory) if file.endswith(('.jpg', '.jpeg', '.png'))]
detect_bounding_boxes(image_files, trip_id, hashmap, buid)
shutil.rmtree(image_directory)
os.remove(zip_path) # Remove the temporarily saved zip file
except Exception as e:
report_exception( e)
logging.error("Exception occurred in detect_faces2 function: %s", e)
this code is for temporarily saving the files in img_directory,
and coming to the storage part, we are saving them on the disk [not on VRAM] because of multiple requests at once [and each zip file may alone can have 25 MB and 100’s of requests should be handled. And the process is running on normal EC2 instance which have 2 core’s and 8 gigs RAM.
and profiling yes, i have used cprofile and htop to track the resource consumption. i got to know threads are not getting handled correctly. need to do something about that. yes, prioritizing the post request will help but all the zip files will be piled on instance and may take time to process all the images. at present i have no clue how to test correct existing flow. need some one who is good at fastapi as i’m just getting my hands on to it.