Пример #1
0
def get_object_detection_res(dev_idx, inf_size, frames):
    """Gets detection results."""
    det_res = []

    inf_res = (ctypes.c_char * inf_size)()
    api.kdp_dme_retrieve_res(dev_idx, 0, inf_size, inf_res)

    od_header_res = ctypes.cast(ctypes.byref(inf_res),
                                ctypes.POINTER(
                                    constants.ObjectDetectionRes)).contents
    box_count = od_header_res.box_count
    #det_res.append(od_header_res.class_count)
    #det_res.append(od_header_res.box_count)
    #print("image -> {} object(s)\n".format(box_count))

    r_size = 4
    if r_size >= 4:
        header_result = ctypes.cast(
            ctypes.byref(inf_res),
            ctypes.POINTER(constants.ObjectDetectionRes)).contents
        box_result = ctypes.cast(
            ctypes.byref(header_result.boxes),
            ctypes.POINTER(constants.BoundingBox *
                           header_result.box_count)).contents
        for box in box_result:
            x1 = int(box.x1)
            y1 = int(box.y1)
            x2 = int(box.x2)
            y2 = int(box.y2)
            score = float(box.score)
            class_num = int(box.class_num)
            res = [x1, y1, x2, y2, class_num, score]
            det_res.append(res)

    return det_res
def get_detection_res(dev_idx, inf_size):
    """Gets detection results."""
    inf_res = (ctypes.c_char * inf_size)()
    # Get the data for all output nodes: TOTAL_OUT_NUMBER + (H/C/W/RADIX/SCALE) +
    # (H/C/W/RADIX/SCALE) + ... + FP_DATA + FP_DATA + ...
    api.kdp_dme_retrieve_res(dev_idx, 0, inf_size, inf_res)

    # Prepare for postprocessing
    listdata = [ord(byte) for byte in inf_res]
    npdata = np.asarray(listdata)

    fp_header_res = ctypes.cast(ctypes.byref(inf_res),
                                ctypes.POINTER(
                                    constants.RawFixpointData)).contents
    output_num = fp_header_res.output_num

    outnode_params_res = ctypes.cast(
        ctypes.byref(fp_header_res.out_node_params),
        ctypes.POINTER(constants.OutputNodeParams * output_num)).contents

    height = 0
    channel = 0
    width = 0
    radix = 0
    scale = 0.0
    npraw_data_array = []
    data_offset = 0
    for param in outnode_params_res:
        height = param.height
        channel = param.channel
        width = param.width
        radix = param.radix
        scale = param.scale

        # print(output_num, height, channel, width, pad_up_16(width), radix, scale)

        # offset in bytes for TOTAL_OUT_NUMBER + (H/C/W/RADIX/SCALE) + (H/C/W/RADIX/SCALE)
        offset = ctypes.sizeof(ctypes.c_int) + output_num * ctypes.sizeof(
            constants.OutputNodeParams)
        # print("offset ", offset, ctypes.sizeof(c_int), ctypes.sizeof(OutputNodeParams))

        # get the fixed-point data
        npdata = npdata.astype("int8")
        raw_data = []

        raw_data = npdata[offset + data_offset:offset + data_offset +
                          height * channel * pad_up_16(width)]
        data_offset += height * channel * pad_up_16(width)
        # print(raw_data.shape, offset, offset + height*channel*pad_up_16(width), height*channel*pad_up_16(width))
        raw_data = raw_data.reshape(height, channel, pad_up_16(width))
        raw_data = raw_data[:, :, :width]

        # save the fp data into numpy array and convert to float
        npraw_data = np.array(raw_data)
        npraw_data = npraw_data.transpose(0, 2, 1) / (2**radix) / scale
        npraw_data_array.append(npraw_data)

    return npraw_data_array
Пример #3
0
def get_landmark_res(dev_idx, inf_size, frames):
    """Gets landmark results."""
    inf_res = (ctypes.c_char * inf_size)()
    api.kdp_dme_retrieve_res(dev_idx, 0, inf_size, inf_res)

    lm_res = ctypes.cast(ctypes.byref(inf_res),
                         ctypes.POINTER(constants.LandmakrResult)).contents
    score = lm_res.score
    blur = lm_res.blur
    print(score, blur)

    return lm_res
Пример #4
0
def get_age_gender_res(dev_idx, inf_size):
    #inf_res = (ctypes.c_char * inf_size)()
    inf_res = constants.FDAgeGenderRes()
    api.kdp_dme_retrieve_res(
        dev_idx, 0, inf_size,
        ctypes.cast(ctypes.byref(inf_res), ctypes.c_char_p))
    det_res = []
    FACE_SCORE_THRESHOLD = 0.8
    if inf_res.fd_res.score > FACE_SCORE_THRESHOLD:
        # print("[INFO] FACE DETECT (x1, y1, x2, y2, score) = {}, {}, {}, {}, {}\n".format(
        #     inf_res.fd_res.x1, inf_res.fd_res.y1, inf_res.fd_res.x2, inf_res.fd_res.y2,
        #     inf_res.fd_res.score))
        if not inf_res.ag_res.age and not inf_res.ag_res.ismale:
            #print("[INFO] FACE TOO SMALL\n")
            res = [
                int(inf_res.fd_res.x1),
                int(inf_res.fd_res.y1),
                int(inf_res.fd_res.x2),
                int(inf_res.fd_res.y2),
                float(inf_res.fd_res.score), 0, 3
            ]  # age:0 gender:3
        else:
            #gender = "Male" if inf_res.ag_res.ismale else "Female"
            # print("[INFO] AGE_GENDER (Age, Gender) = {}, {}\n".format(
            #     inf_res.ag_res.age, gender))
            res = [
                int(inf_res.fd_res.x1),
                int(inf_res.fd_res.y1),
                int(inf_res.fd_res.x2),
                int(inf_res.fd_res.y2),
                float(inf_res.fd_res.score),
                int(inf_res.ag_res.age),
                int(inf_res.ag_res.ismale)
            ]  # male:1 female:2
        det_res.append(res)
    # else:
    #     print("[INFO] NO FACE OR FACE SCORE TOO LOW!!!\n")

    return det_res