Example #1
0
def kdp_dme_load_yolo_model(dev_idx, _model_path):
    """Load dme model."""
    model_id = 0
    data = (ctypes.c_char * DME_FWINFO_SIZE)()
    p_buf = (ctypes.c_char * DME_MODEL_SIZE)()
    ret_size = 0

    # read firmware setup data
    print("loading models to Kneron Device: ")
    n_len = api.read_file_to_buf(data, DME_YOLO_224_FW_SETUP, DME_FWINFO_SIZE)
    if n_len <= 0:
        print("reading fw setup file failed: {}...\n".format(n_len))
        return -1

    dat_size = n_len

    n_len = api.read_file_to_buf(p_buf, DME_YOLO_224_MODEL_FILE,
                                 DME_MODEL_SIZE)
    if n_len <= 0:
        print("reading model file failed: {}...\n".format(n_len))
        return -1

    buf_len = n_len
    model_size = n_len

    print("starting DME mode ...\n")
    ret, ret_size = api.kdp_start_dme(dev_idx, model_size, data, dat_size,
                                      ret_size, p_buf, buf_len)
    if ret:
        print("could not set to DME mode:{}..\n".format(ret_size))
        return -1

    print("DME mode succeeded...\n")
    print("Model loading successful")
    sleep(SLEEP_TIME)

    # dme configuration
    model_id = 19  # model id when compiling in toolchain
    output_num = 2  # number of output node for the model
    image_col = 640
    image_row = 480
    image_ch = 3
    image_format = (constants.IMAGE_FORMAT_SUB128 | constants.NPU_FORMAT_RGB565
                    | constants.IMAGE_FORMAT_RAW_OUTPUT)

    dme_cfg = constants.KDPDMEConfig(model_id, output_num, image_col,
                                     image_row, image_ch, image_format)

    dat_size = ctypes.sizeof(dme_cfg)
    print("starting DME configure ...\n")
    ret, model_id = api.kdp_dme_configure(
        dev_idx, ctypes.cast(ctypes.byref(dme_cfg), ctypes.c_char_p), dat_size,
        model_id)
    if ret:
        print("could not set to DME configure mode..\n", model_id)
        return -1

    print("DME configure model [{}] succeeded...\n".format(model_id))
    sleep(SLEEP_TIME)
    return 0
Example #2
0
def kdp_get_nef_model_metadata(model_path):
    """Request for metadata from nef model file.

    Returns 0 on success and -1 on failure.

    Arguments:
         model_path: nef model file
         model_size: size of nef model
    """
    p_buf = (ctypes.c_char * DME_MODEL_SIZE)()
    n_len = api.read_file_to_buf(p_buf, model_path, DME_MODEL_SIZE)

    r_data = (ctypes.c_char *
              ctypes.sizeof(constants_kl520.NefModelMetaData))()
    # Get the metadata for nef model file
    ret = api.kdp_get_nef_model_metadata(p_buf, n_len, r_data)

    metadata = ctypes.cast(ctypes.byref(r_data),
                           ctypes.POINTER(
                               constants_kl520.NefModelMetaData)).contents

    metadata_info = []
    metadata_info.append(metadata.platform)
    metadata_info.append(metadata.target)
    metadata_info.append(metadata.crc)
    metadata_info.append(metadata.kn_num)
    metadata_info.append(metadata.enc_type)
    metadata_info.append(metadata.tc_ver)
    metadata_info.append(metadata.compiler_ver)

    return ret, metadata_info
Example #3
0
def user_test_app(dev_idx, user_id):
    """User test update firmware"""
    # udt firmware
    module_id = user_id
    img_buf = (ctypes.c_char * FW_FILE_SIZE)()

    if module_id not in (0, 1, 2):
        print("invalid module id: {}...\n".format(user_id))
        return -1

    print("starting update fw ...\n")

    # update scpu
    module_id = 1
    # print(FW_SCPU_FILE)
    buf_len_ret = api.read_file_to_buf(img_buf, FW_SCPU_FILE, FW_FILE_SIZE)

    if buf_len_ret <= 0:
        print("reading file to buf failed: {}...\n".format(buf_len_ret))
        return -1

    buf_len = buf_len_ret
    ret, module_id = api.kdp_update_fw(dev_idx, module_id, img_buf, buf_len)
    if ret:
        print("could not update fw..\n")
        return -1

    print("update SCPU firmware succeeded...\n")

    # update ncpu
    buf_len_ret = api.read_file_to_buf(img_buf, FW_NCPU_FILE, FW_FILE_SIZE)
    module_id = 2
    if buf_len_ret <= 0:
        print("reading file to buf failed: {}...\n".format(buf_len_ret))
        return -1

    buf_len = buf_len_ret
    ret, module_id = api.kdp_update_fw(dev_idx, module_id, img_buf, buf_len)
    if ret:
        print("could not update fw..\n")
        return -1

    print("update NCPU firmware succeeded...\n")

    return 0
Example #4
0
def read_file_to_buf(image_file, image_size):
    """Reads input image into a buffer.

    Arguments:
        image_file: File containing the input image.
        image_size: Size of the input image.
    """
    buffer = (ctypes.c_char * image_size)()
    length = api.read_file_to_buf(buffer, image_file, image_size)
    if length <= 0:
        print("reading image file, {}, failed: {}...\n".format(
            image_file, length))
        return None
    return buffer
Example #5
0
def kdp_dme_load_age_gender_model(dev_idx, _model_path):
    """Load dme model."""
    model_id = 0
    p_buf = (ctypes.c_char * DME_MODEL_SIZE)()
    ret_size = 0

    print("loading models to Kneron Device: ")

    # read model data
    model_file = _model_path + "/models_520.nef"

    n_len = api.read_file_to_buf(p_buf, model_file, DME_MODEL_SIZE)
    if n_len <= 0:
        print("reading model file failed: {}...\n".format(n_len))
        return -1

    buf_len = n_len

    print("starting DME mode ...\n")

    ret, ret_size = api.kdp_start_dme_ext(dev_idx, p_buf, buf_len, ret_size)

    if ret:
        print("could not set to DME mode:{}..\n".format(ret_size))
        return -1

    print("DME mode succeeded...\n")
    print("Model loading successful")
    sleep(SLEEP_TIME)

    # dme configuration
    model_id = 32  # model id when compiling in toolchain
    output_num = 1  # number of output node for the model
    image_col = 640
    image_row = 480
    image_ch = 3
    image_format = (constants_kl520.IMAGE_FORMAT_MODEL_AGE_GENDER
                    | constants_kl520.IMAGE_FORMAT_SUB128
                    | constants_kl520.NPU_FORMAT_RGB565)
    c_int_p = ctypes.POINTER(ctypes.c_int)
    crop_box = np.array([0, 0, 0, 0])  # for future use
    crop_box = crop_box.astype(np.int32)
    crop_box = crop_box.ctypes.data_as(c_int_p)
    pad_value = np.array([0, 0, 0, 0])  # for future use
    pad_value = pad_value.astype(np.int32)
    pad_value = pad_value.ctypes.data_as(c_int_p)
    c_float_p = ctypes.POINTER(ctypes.c_float)
    extra_param = np.array([0.0])
    extra_param = extra_param.astype(np.float32)
    extra_param = extra_param.ctypes.data_as(c_float_p)

    dme_cfg = constants_kl520.KDPDMEConfig(model_id, output_num, image_col,
                                           image_row, image_ch, image_format,
                                           crop_box, pad_value, extra_param)

    dat_size = ctypes.sizeof(dme_cfg)
    print("starting DME configure ...\n")
    ret, model_id = api.kdp_dme_configure(
        dev_idx, ctypes.cast(ctypes.byref(dme_cfg), ctypes.c_char_p), dat_size,
        model_id)
    if ret:
        print("could not set to DME configure mode..\n", model_id)
        return -1

    print("DME configure model [{}] succeeded...\n".format(model_id))
    sleep(SLEEP_TIME)
    return 0