def main(): ''' The main method ''' logger.info("Starting server on host : %s , port : %s !", config.HOST, config.PORT) start_http_server()
def do_post(request): ''' Process the uploaded images and align the raw image as per template ''' # step 1 : Check for JSON Payload content_type = request.headers['Content-Type'].split(";")[0] if (content_type != 'application/json'): logger.info(request.headers['Content-Type']) raise Exception("Content Type should be json") # Get POST parameters input_json = request.json image1_text = input_json["raw_image"] raw_img = image_utils.read_b64(image1_text) if raw_img is None: raise Exception("Could not read raw image") logger.info("Raw Image shape : %s", str(raw_img.shape)) image2_text = input_json["ref_image"] ref_img = image_utils.read_b64(image2_text) if ref_img is None: raise Exception("Could not read ref image") logger.info("Ref Image shape : %s", str(ref_img.shape)) aligned_img, key_point_match_img = image_aligner.align_image( raw_img, ref_img) logger.info("Aligned Image shape : %s", str(aligned_img.shape)) return { "aligned_img": image_utils.image_to_b64(aligned_img), "key_point_match_img": image_utils.image_to_b64(key_point_match_img) }
def align_endpoint(): ''' Align image to a given template ''' logger.info("Processing request for /api/align endpoint") status = config.HTTP_STATUS_OK try: message = do_post(request) except Exception as ex: logger.exception("Something went wrong !") message = {"error": str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(message), status) resp.headers['Content-Type'] = 'application/json' return resp
def scan_barcode(): ''' Scan barcode from uploaded image ''' logger.info("Processing request for /api/scan-barcode endpoint") status = config.HTTP_STATUS_OK try: message = do_post(request) except Exception as ex: logger.exception("Something went wrong !") message = {"error": str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(message), status) resp.headers['Content-Type'] = 'application/json' return resp
def do_post(request): ''' Process the uploaded images and align the raw image as per template ''' # step 1 : Check for JSON Payload content_type = request.headers['Content-Type'].split(";")[0] if (content_type != 'application/json'): logger.info(request.headers['Content-Type']) raise Exception("Content Type should be json") # Get POST parameters input_json = request.json image1_text = input_json["image"] img = image_utils.read_b64(image1_text) if img is None: raise Exception("Could not read raw image") logger.info("Uploaded Image shape : %s", str(img.shape)) code = -1 return {"code": code}
def pdf_to_image(): logger.info("Processing POST request for /api/pdf-to-image ") status = config.HTTP_STATUS_OK try: file_list = list(request.files.keys()) num_files = len(file_list) logger.info(file_list) if num_files == 0: raise Exception( "Needs 1 pdf file but received {} files".format(num_files)) elif len(file_list) > 1: err_msg = "More than 1 file uploaded. Got {} files".format(num_files) raise Exception(err_msg) uploaded_file = request.files[file_list[0]] uploaded_file_name = uploaded_file.filename if uploaded_file_name == "": raise Exception("Empty file uploaded") elif os.path.splitext(uploaded_file_name)[-1] not in (".pdf", ".PDF"): raise Exception("Please upload a pdf file only") # Read pdf file pages = pdf2image.convert_from_bytes(uploaded_file.read()) images = [] for page in pages: img = cv2.cvtColor(np.array(page), cv2.COLOR_RGB2BGR) images.append(image_utils.image_to_b64(img)) result={"images": images} except Exception as ex: logger.exception("Something went wrong") result = {"error" : str(ex)} status = config.HTTP_STATUS_ERROR resp = make_response(json.dumps(result), status ) resp.headers['Content-Type'] = 'application/json' return resp