Example #1
0
def anonymize(image: UploadFile = File(...),
              configuration: UploadFile = File(...)):
    """
    Anonymize the given image
    :param image: Image file
    :param configuration: Json file
    :return: The anonymized image
    """
    try:
        result, errors = anonymizationservice.anonymize(image, configuration)
        if not errors:
            _, im_png = cv2.imencode(".png", result)
            response = StreamingResponse(io.BytesIO(im_png.tobytes()),
                                         media_type="image/jpeg")
            return response
        else:
            return ApiResponse(
                success=False,
                error=
                "Some data in your configuration file need to be modified. Check the /available_methods/ endpoint",
                data=errors)
    except ApplicationError as e:
        return ApiResponse(success=False, error=e)
    except Exception:
        return ApiResponse(success=False, error='unexpected server error')
Example #2
0
def list_urls():
    """
    list all available urls in the url json file
    :return: ApiResponse
    """
    try:
        payload = helpers.parse_json(url_config_path)
    except Exception as e:
        return ApiResponse(success=False, error=e)
    return ApiResponse(data=payload)
Example #3
0
def load_custom():
    """
	Loads all the available models.
	:return: All the available models with their respective hashed values
	"""
    try:
        return dl_service.load_all_models()
    except ApplicationError as e:
        return ApiResponse(success=False, error=e)
    except Exception:
        return ApiResponse(success=False, error='unexpected server error')
Example #4
0
async def load(model_name: str, force: bool = False):
    """
	Loads a model specified as a query parameter.
	:param model_name: Model name
	:param force: Boolean for model force reload on each call
	:return: APIResponse
	"""
    try:
        dl_service.load_model(model_name, force)
        return ApiResponse(success=True)
    except ApplicationError as e:
        return ApiResponse(success=False, error=e)
async def list_models(user_agent: str = Header(None)):
    """
	Lists all available models.
	:param user_agent:
	:return: APIResponse
	"""
    try:
        error_logging.info('request successful;')
        return ApiResponse(data={'models': dl_service.list_models()})
    except Exception as e:
        error_logging.error(str(e))
        return ApiResponse(success=False, error='unexpected server error')
Example #6
0
def anonymize_video(video: UploadFile = File(...),
                    configuration: UploadFile = File(...)):
    """
    Anonymize the given video  and save it to src/main/anonymized_video as original_video_name_TIMESTAMP.mp4
    :param video: Video file
    :param configuration: Json file
    """
    try:
        return anonymizationservice.anonymize_video(video, configuration)
    except ApplicationError as e:
        return ApiResponse(success=False, error=e)
    except Exception:
        return ApiResponse(success=False, error='unexpected server error')
def load_custom():
    """
	Loads all the available models.
	:return: All the available models with their respective hashed values
	"""
    try:
        error_logging.info('request successful;')
        return dl_service.load_all_models()
    except ApplicationError as e:
        error_logging.warning(str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(str(e))
        return ApiResponse(success=False, error='unexpected server error')
def get_labels_custom(model: str = Form(...)):
    """
	Lists the model's labels with their hashed values.
	:param model: Model name or model hash
	:return: A list of the model's labels with their hashed values
	"""
    try:
        error_logging.info('request successful;')
        return dl_service.get_labels_custom(model)
    except ModelNotFound as e:
        error_logging.warning(model + ' ' + str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(model + ' ' + str(e))
        return ApiResponse(success=False, error='unexpected server error')
async def list_model_config(model_name: str):
    """
	Lists all the model's configuration.
	:param model_name: Model name
	:return: List of model's configuration
	"""
    try:
        config = dl_service.get_config(model_name)
        error_logging.info('request successful;' + str(config))
        return ApiResponse(data=config)
    except ModelNotFound as e:
        error_logging.warning(model_name + ';' + str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(model_name + ' ' + str(e))
        return ApiResponse(success=False, error='unexpected server error')
Example #10
0
async def list_models(user_agent: str = Header(None)):
    """
	Lists all available models.
	:param user_agent:
	:return: APIResponse
	"""
    return ApiResponse(data={'models': dl_service.list_models()})
async def list_model_config(model_name: str):
    """=======
app = FastAPI(version='1.0', title='BMW InnovationLab YOLOv3-v4 darknet inference Automation',
>>>>>>> master
e
	:return: List of model's configuration
	"""
    try:
        config = dl_service.get_config(model_name)
        error_logging.info('request successful;' + str(config))
        return ApiResponse(data=config)
    except ModelNotFound as e:
        error_logging.warning(model_name + ';' + str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(model_name + ' ' + str(e))
        return ApiResponse(success=False, error='unexpected server error')
async def load(model_name: str, force: bool = False):
    """
	Loads a model specified as a query parameter.
	:param model_name: Model name
	:param force: Boolean for model force reload on each call
	:return: APIResponse
	"""
    try:
        dl_service.load_model(model_name, force)
        error_logging.info('request successful;')
        return ApiResponse(success=True)
    except ApplicationError as e:
        error_logging.warning(str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(str(e))
        return ApiResponse(success=False, error='unexpected server error')
Example #13
0
async def predict_image(model_name: str, input_data: UploadFile = File(...)):
    """
    Draws bounding box(es) on image and returns it.
    :param model_name: Model name
    :param input_data: Image file
    :return: Image file
    """
    try:
        output = await dl_service.run_model(model_name, input_data, draw=True, predict_batch=False)
        error_logging.info('request successful;' + str(output))
        return FileResponse("/main/result.jpg", media_type="image/jpg")
    except ApplicationError as e:
        error_logging.warning(model_name + ';' + str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(model_name + ' ' + str(e))
        return ApiResponse(success=False, error='unexpected server error')
Example #14
0
async def run_model(model_name: str, input_data: UploadFile = File(...)):
    """
    Performs a prediction by giving both model name and image file.
    :param model_name: Model name
    :param input_data: An image file
    :return: APIResponse containing the prediction's bounding boxes
    """
    try:
        output = await dl_service.run_model(model_name, input_data, draw=False, predict_batch=False)
        error_logging.info('request successful;' + str(output))
        return ApiResponse(data=output)
    except ApplicationError as e:
        error_logging.warning(model_name + ';' + str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(model_name + ' ' + str(e))
        return ApiResponse(success=False, error='unexpected server error')
Example #15
0
async def list_model_labels(model_name: str):
    """
	Lists all the model's labels.
	:param model_name: Model name
	:return: List of model's labels
	"""
    labels = dl_service.get_labels(model_name)
    return ApiResponse(data=labels)
Example #16
0
async def list_model_config(model_name: str):
    """
	Lists all the model's configuration.
	:param model_name: Model name
	:return: List of model's configuration
	"""
    config = dl_service.get_config(model_name)
    return ApiResponse(data=config)
Example #17
0
def remove_all_urls():
    """
    Remove all available urls in the url json file
    :return: ApiResponse
    """
    payload = {"urls": []}
    helpers.write_json(payload, url_config_path)
    return ApiResponse(data="all urls removed successfully")
Example #18
0
def remove_url(url: str = Form(...)):
    """
        Remove url from the url json file
        :param url: api url in the format: http://ip:port/
        :return: ApiResponse
        """
    try:
        payload = helpers.parse_json(url_config_path)
    except Exception as e:
        return ApiResponse(success=False, error=e)
    if url in payload['urls']:
        payload['urls'].remove(url)
        helpers.write_json(payload, url_config_path)
        return ApiResponse(data={"url removed successfully"})
    else:
        return ApiResponse(success=False,
                           error="url is not present in config file")
Example #19
0
def get_available_methods():
    """
    :return: A list that shows the model name, the urls and the model types that support each label in addition to the anonymization techniques that can be applied to each of them
    """
    try:
        config.master_dict = labels_methods()
        return config.master_dict
    except Exception:
        return ApiResponse(success=False, error='unexpected server error')
Example #20
0
async def detect_custom(model: str = Form(...), image: UploadFile = File(...)):
    """
	Performs a prediction for a specified image using one of the available models.
	:param model: Model name or model hash
	:param image: Image file
	:return: Model's Bounding boxes
	"""
    draw_boxes = False
    try:
        output = await dl_service.run_model(model, image, draw_boxes)
        error_logging.info('request successful;' + str(output))
        return output
    except ApplicationError as e:
        error_logging.warning(model + ';' + str(e))
        return ApiResponse(success=False, error=e)
    except Exception as e:
        error_logging.error(model + ' ' + str(e))
        return ApiResponse(success=False, error='unexpected server error')
Example #21
0
async def list_models(user_agent: str = Header(None)):
	"""
	Lists all available models.
	:param user_agent:
	:return: APIResponse
	"""
	try:
		return ApiResponse(data={'models': dl_service.list_models()})
	except ApplicationError as e:
		raise e
	except Exception:
		raise ApplicationError('unexpected server error')
Example #22
0
async def list_model_config(model_name: str):
	"""
	Lists all the model's configuration.
	:param model_name: Model name
	:return: List of model's configuration
	"""
	try:
		return ApiResponse(data=dl_service.get_config(model_name))
	except ApplicationError as e:
		raise e
	except Exception:
		raise ApplicationError('unexpected server error')
async def list_model_labels(model_name: str):
    """
	Lists all the model's labels.
	:param model_name: Model name
	:return: List of model's labels
	"""
    try:
        labels = FetchLabels().get_labels(model_name)
        return ApiResponse(data=labels)
    except Exception:
        raise HTTPException(
            status_code=404,
            detail="something went wrong! please check if the model exist")
async def list_model_palette(model_name: str):
    """
	Lists all the model's palette.
	:param model_name: Model name
	:return: List of model's palette
	"""
    try:
        palette = FetchPalette().get_palette(model_name)
        return ApiResponse(data=palette)
    except Exception:
        raise HTTPException(
            status_code=404,
            detail="something went wrong! please check if the model exist")
async def list_model_configuration(model_name: str):
    """
	Lists all the model's configuration.
	:param model_name: Model name
	:return: List of model's configuration
	"""
    try:
        configuration = FetchConfiguration().get_configuration(model_name)
        return ApiResponse(data=configuration)
    except Exception:
        raise HTTPException(
            status_code=404,
            detail="something went wrong! please check if the model exist")
Example #26
0
def set_url(url: str = Form(...)):
    """
    Add url to the url json file
    :param url: api url in the format: http://ip:port/
    :return: ApiResponse
    """
    try:
        payload = helpers.parse_json(url_config_path)
        response = helpers.check_api_availability(url)
    except Exception as e:
        return ApiResponse(success=False, error=e)
    if response.status_code == 200:
        if url not in payload['urls']:
            payload['urls'].append(url)
            helpers.write_json(payload, url_config_path)
            data = "url added successfully"
        else:
            data = "url already exist"
        return ApiResponse(data=data)
    else:
        return ApiResponse(success=False,
                           error="url trying to add is not reachable")
Example #27
0
async def detect_robotron(request: Request, background_tasks: BackgroundTasks, model: str = Form(...), image: UploadFile = File(...)):
	"""
	Performs a prediction for a specified image using one of the available models.
	:param request: Used if background tasks was enabled
	:param background_tasks: Used if background tasks was enabled
	:param model: Model name or model hash
	:param image: Image file
	:return: Model's Bounding boxes
	"""
	draw_boxes = False
	predict_batch = False
	try:
		request_start = time.time()
		output = await dl_service.run_model(model, image, draw_boxes, predict_batch)
		# background_tasks.add_task(metrics_collector,'detect',image, output, request, request_start)
		error_logging.info('request successful;' + str(output))
		return output
	except ApplicationError as e:
		error_logging.warning(model+';'+str(e))
		return ApiResponse(success=False, error=e)
	except Exception as e:
		error_logging.error(model+' '+str(e))
		return ApiResponse(success=False, error='unexpected server error')
Example #28
0
async def get_inference_type():
    return ApiResponse(data="classification")
Example #29
0
def api_response(response):
    # map to response model
    json_data = json.loads(response.text)
    return ApiResponse.from_json(json_data)
Example #30
0
async def app_exception_handler(request: Request, exc: ApplicationError):
	return JSONResponse(
        status_code=exc.status_code,
        content=ApiResponse(success=False, error=str(exc)).__dict__,
    )