def enqueue_packet(project_string_id, session, media_url = None, media_type = None, file_id = None, file_name = None, job_id = None, batch_id = None, directory_id = None, source_directory_id = None, instance_list = None, video_split_duration = None, frame_packet_map = None, remove_link = None, add_link = None, copy_instance_list = None, commit_input = False, task_id = None, video_parent_length = None, type = None, task_action = None, external_map_id = None, original_filename = None, external_map_action = None, enqueue_immediately = False, mode = None, allow_duplicates = False, extract_labels_from_batch = False): """ Creates Input() object and enqueues it for media processing Returns Input() object that was created :param packet_data: :return: """ diffgram_input = Input() project = Project.get(session, project_string_id) diffgram_input.file_id = file_id diffgram_input.task_id = task_id diffgram_input.batch_id = batch_id diffgram_input.video_parent_length = video_parent_length diffgram_input.remove_link = remove_link diffgram_input.add_link = add_link diffgram_input.copy_instance_list = copy_instance_list diffgram_input.external_map_id = external_map_id diffgram_input.original_filename = original_filename diffgram_input.external_map_action = external_map_action diffgram_input.task_action = task_action diffgram_input.mode = mode diffgram_input.project = project diffgram_input.media_type = media_type diffgram_input.type = "from_url" diffgram_input.url = media_url diffgram_input.video_split_duration = video_split_duration diffgram_input.allow_duplicates = allow_duplicates if instance_list: diffgram_input.instance_list = {} diffgram_input.instance_list['list'] = instance_list if frame_packet_map: diffgram_input.frame_packet_map = frame_packet_map # print(diffgram_input.frame_packet_map) session.add(diffgram_input) session.flush() if batch_id and extract_labels_from_batch: upload_tools = Upload(session = session, project = project, request = None) upload_tools.extract_instance_list_from_batch(input = diffgram_input, input_batch_id = batch_id, file_name = file_name) # Expect temp dir to be None here. # because each machine should assign it's own temp dir # Something else to consider for future here! # Once this is part of input, it will be smoothly handled at right time as part of # processing queue diffgram_input.job_id = job_id # Process media handles checking if the directory id is valid diffgram_input.directory_id = directory_id diffgram_input.source_directory_id = source_directory_id diffgram_input_id = diffgram_input.id queue_limit = 0 if media_type == "image": queue_limit = 30 # 50 if media_type == "video": queue_limit = 1 if settings.PROCESS_MEDIA_ENQUEUE_LOCALLY_IMMEDIATELY is True or enqueue_immediately: print('diffgram_input_id', diffgram_input_id) if commit_input: regular_methods.commit_with_rollback(session = session) item = PrioritizedItem( priority = 10000, # individual frames have a priority here. input_id = diffgram_input_id, media_type = media_type) add_item_to_queue(item) else: diffgram_input.processing_deferred = True # Default return diffgram_input
def input_from_local(session, log, project_string_id, http_input, file, directory_id): # TODO review how we want to handle header options # Especially if needs to be outside of function for python requests... # immediate_mode = request.headers['immediate_mode'] # Issues to be careful with ie string treamtment of 'True' vs True... immediate_mode = True input = Input() input.directory_id = directory_id if http_input['instance_list']: input.instance_list = {} input.instance_list['list'] = http_input['instance_list'] if http_input['frame_packet_map']: input.frame_packet_map = http_input['frame_packet_map'] # only need to make temp dir if file doesn't already exist... original_filename = secure_filename( file.filename ) # http://flask.pocoo.org/docs/0.12/patterns/fileuploads/ input.extension = os.path.splitext(original_filename)[1].lower() input.original_filename = os.path.split(original_filename)[1] input.temp_dir = tempfile.mkdtemp() input.temp_dir_path_and_filename = input.temp_dir + \ "/" + original_filename + input.extension project = Project.get(session, project_string_id) input.project = project input.media_type = None input.media_type = Process_Media.determine_media_type(input.extension) if not input.media_type: input.status = "failed" input.status_text = "Invalid file type: " + input.extension return False, log, input session.add(input) session.flush() with open(input.temp_dir_path_and_filename, "wb") as f: f.write(file.stream.read()) # For LOCAL not normal upload file_size_limit = 9 * 1024 * 1024 * 1024 file_size = os.path.getsize( input.temp_dir_path_and_filename) # gets size in bytes if file_size > file_size_limit: input.status = "failed" input.status_text = "Exceeded max file size" return False, log, input if immediate_mode == True or immediate_mode is None: # Leave this as a direct call for time being, as we pass # the input back to thing on front end process_media = Process_Media(session=session, input=input) result = process_media.main_entry() # Always return input along with file? if result == True: return True, log, input if result == False: return False, log, input # Default priority = 100 item = PrioritizedItem(priority=priority, input_id=input.id, media_type=input.media_type) add_item_to_queue(item) return True, log, input