Repository: opencv/opencv
File: samples/dnn/auto_white_balance.py (Python)

Code before:
    p.add_argument('--target', default="cpu", type=str, choices=targets,
            help="Choose one of target computation devices: "
            "cpu: CPU target (by default), "
            "opencl: OpenCL, "
            "opencl_fp16: OpenCL fp16 (half-float precision), "
            "ncs2_vpu: NCS2 VPU, "
            "hddl_vpu: HDDL VPU, "
            "vulkan: Vulkan, "
            "cuda: CUDA, "
            "cuda_fp16: CUDA fp16 (half-float preprocess)")

    args, _ = p.parse_known_args()
    add_preproc_args(args.zoo, p, 'auto_white_balance')
    p = argparse.ArgumentParser(
        parents=[p],
        description="FC4 Color Constancy (ONNX): " \
        "predicts illuminant and applies white balance.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    return p.parse_args(func_args)



def main(func_args=None):
    args = get_args_parser(func_args)
    args.model = findModel(args.model, args.sha1)

    try:
        net = cv.dnn.readNetFromONNX(args.model)
        net.setPreferableBackend(get_backend_id(args.backend))
        net.setPreferableTarget(get_target_id(args.target))
    except cv.error as e:
        print(f"Error loading model: {e}", file=sys.stderr)
        sys.exit(1)

    img = cv.imread(findFile(args.input), cv.IMREAD_COLOR)
    if img is None:
        print(f"Cannot load image: {args.input}", file=sys.stderr)
        sys.exit(1)

    blob = cv.dnn.blobFromImage(
        img, scalefactor=args.scale, size=(img.shape[1], img.shape[0]),
        mean=args.mean, swapRB=args.rgb, crop=False, ddepth=cv.CV_32F
    )
    net.setInput(blob)

    try:
        out = net.forward()
    except cv.error as e:
        print(f"Forward error: {e}", file=sys.stderr)
        sys.exit(1)

Review comment (suggestion):
set default alias here like: `add_preproc_args(args.zoo, p, 'auto_white_balance', prefix="", alias="fc4")`

Code after:
    p.add_argument('--zoo',
                   default=os.path.join(os.path.dirname(os.path.abspath(__file__)), 'models.yml'),
                   help='An optional path to file with preprocessing parameters.')
    p.add_argument("--input", help="Path to input image", default="castle.png")
    p.add_argument('--backend', default="default", type=str, choices=backends,
            help="Choose one of computation backends: "
            "default: automatically (by default), "
            "openvino: Intel's Deep Learning Inference Engine (https://software.intel.com/openvino-toolkit), "
            "opencv: OpenCV implementation, "
            "vkcom: VKCOM, "
            "cuda: CUDA, "
            "webnn: WebNN")
    p.add_argument('--target', default="cpu", type=str, choices=targets,
            help="Choose one of target computation devices: "
            "cpu: CPU target (by default), "
            "opencl: OpenCL, "
            "opencl_fp16: OpenCL fp16 (half-float precision), "
            "ncs2_vpu: NCS2 VPU, "
            "hddl_vpu: HDDL VPU, "
            "vulkan: Vulkan, "
            "cuda: CUDA, "
            "cuda_fp16: CUDA fp16 (half-float preprocess)")

    args, _ = p.parse_known_args()
    add_preproc_args(args.zoo, p, 'auto_white_balance', prefix="", alias="fc4")
    p = argparse.ArgumentParser(
        parents=[p],
        description="FC4 Color Constancy (ONNX): " \
        "predicts illuminant and applies white balance.",
        formatter_class=argparse.ArgumentDefaultsHelpFormatter
    )
    return p.parse_args(func_args)



def main(func_args=None):
    args = get_args_parser(func_args)
    args.model = findModel(args.model, args.sha1)

    try:
        net = cv.dnn.readNetFromONNX(args.model)
        net.setPreferableBackend(get_backend_id(args.backend))
        net.setPreferableTarget(get_target_id(args.target))
    except cv.error as e:
        print(f"Error loading model: {e}", file=sys.stderr)
        sys.exit(1)

    img = cv.imread(findFile(args.input), cv.IMREAD_COLOR)
    if img is None:
        print(f"Cannot load image: {args.input}", file=sys.stderr)
        sys.exit(1)