Ejemplo n.º 1
0
def preandinfer(image_queue, images_list):
    acl_start = time.time()
    #print('Start preandinfer ')
    if not os.path.exists(OUTPUT_DIR):
        os.mkdir(OUTPUT_DIR)
    acl_resource = AclLiteResource()
    acl_resource.init()
    dvpp_ = AclLiteImageProc()
    model = AclLiteModel(model_path)
    print('------------------------------acl processing time',
          time.time() - acl_start)
    for pic in images_list:
        image = AclLiteImage(pic)
        image_dvpp = image.copy_to_dvpp()
        yuv_image = dvpp_.jpegd(image_dvpp)
        resized_image = dvpp_.resize(yuv_image, MODEL_WIDTH, MODEL_HEIGHT)
        result_list = model.execute([
            resized_image,
        ])
        data = ProcData(result_list, pic, OUTPUT_DIR)
        image_queue.put(data)
    post_num = 6
    while (post_num):
        post_num -= 1
        data = "Post process thread exit"
        image_queue.put(data)
    print('End preandinfer')
    print('------------------------------preandinfer time',
          time.time() - acl_start)
Ejemplo n.º 2
0
def main():
    """
    Program execution with picture directory parameters
    """
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)
    acl_resource = AclLiteResource()
    acl_resource.init()
    #Instantiation classification detection, incoming om model path, model input width and height parameters
    classify = Classify(acl_resource, MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)

    #Get the picture storage directory from the parameters, and infer picture by picture
    image_dir = sys.argv[1]
    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in IMG_EXT
    ]

    #Create a directory and save the infer results
    if not os.path.isdir(os.path.join(SRC_PATH, "../out")):
        os.mkdir(os.path.join(SRC_PATH, "../out"))

    for image_file in images_list:
        #read the picture
        image = AclLiteImage(image_file)
        image_dvpp = image.copy_to_dvpp()
        #preprocess image
        resized_image = classify.pre_process(image_dvpp)
        print("pre process end")
        #inference
        result = classify.inference(resized_image)
        #post process
        classify.post_process(result, image_file)
Ejemplo n.º 3
0
def main():
    """
    Program execution with picture directory parameters
    """
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclLiteResource()
    acl_resource.init()
    #Instance classification detection, pass into the OM model storage path, model input width and height parameters
    classify = Classify(acl_resource, MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)
    
    #From the parameters of the picture storage directory, reasoning by a picture
    image_dir = sys.argv[1]
    images_list = [os.path.join(image_dir, img)
                   for img in os.listdir(image_dir)
                   if os.path.splitext(img)[1] in IMG_EXT]
    
    #Create a directory to store the inference results
    if not os.path.isdir('../out'):
        os.mkdir('../out')

    resized_image_list = []
    batch_image_files = []
    num = 0
    batch_amount = len(images_list) // BATCH
    left =  len(images_list) % BATCH

    for image_file in images_list:
        num += 1
        #Read the pictures
        image = AclLiteImage(image_file)
        image_dvpp = image.copy_to_dvpp()
        #preprocess image
        resized_image = classify.pre_process(image_dvpp)
        print("pre process end")

        batch_image_files.append(image_file) 
        resized_image_list.append(resized_image)
        if batch_amount > 0:
            #Each set of BATCH pictures, reasoning and post-processing
            if num == BATCH:
                #Reasoning pictures
                result = classify.inference(resized_image_list, BATCH)
                #process inference results
                classify.post_process(result, batch_image_files, BATCH)
                batch_amount -= 1
                num = 0
                batch_image_files = []
                resized_image_list = []
        else:
            #remaining images are inferred and post-processed
            if num == left:
                #Reasoning pictures
                result = classify.inference(resized_image_list, BATCH)
                #The inference results are processed
                classify.post_process(result, batch_image_files, left)
Ejemplo n.º 4
0
def main():
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclLiteResource()
    acl_resource.init()
    model = AclLiteModel(MODEL_PATH)
    dvpp = AclLiteImageProc(acl_resource)

    image_dir = sys.argv[1]
    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]

    #Create a directory to store the inference results
    if not os.path.isdir(os.path.join(SRC_PATH, "../out")):
        os.mkdir(os.path.join(SRC_PATH, "../out"))

    image_info = construct_image_info()
    for image_file in images_list:
        image = AclLiteImage(image_file)
        resized_image = pre_process(image, dvpp)
        print("pre process end")

        result = model.execute([
            resized_image,
        ])
        post_process(result, image_file)

        print("process " + image_file + " end")
Ejemplo n.º 5
0
def main():
    """
    main
    """
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclLiteResource()
    acl_resource.init()

    crowdcount = CrowdCount(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)
    ret = crowdcount.init()

    if not os.path.isdir(os.path.join(SRC_PATH, "../out")):
        os.mkdir(os.path.join(SRC_PATH, "../out"))

    image_dir = sys.argv[1]
    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in constants.IMG_EXT
    ]

    for image_file in images_list:
        image = AclLiteImage(image_file)
        crop_and_paste_image = crowdcount.pre_process(image)
        print("pre process end")
        result = crowdcount.inference([crop_and_paste_image])
        result_img_encode = crowdcount.post_process(crop_and_paste_image,
                                                    result, image_file)
    return result_img_encode
Ejemplo n.º 6
0
    def jpegd(self, image):
        """
        jepg image to yuv image
        """
        stride_width = utils.align_up128(image.width)
        stride_height = utils.align_up16(image.height)
        stride_size = utils.yuv420sp_size(stride_width, stride_height)
        # Create conversion output image desc
        output_desc, out_buffer = self._gen_jpegd_out_pic_desc(
            image, stride_size)
        ret = acl.media.dvpp_jpeg_decode_async(self._dvpp_channel_desc,
                                               image.data(), image.size,
                                               output_desc, self._stream)
        if ret != constants.ACL_SUCCESS:
            log_error("dvpp_jpeg_decode_async failed ret={}".format(ret))
            return None

        ret = acl.rt.synchronize_stream(self._stream)
        if ret != constants.ACL_SUCCESS:
            log_error("dvpp_jpeg_decode_async failed ret={}".format(ret))
            return None

        # Return the decoded AclLiteImage instance
        return AclLiteImage(out_buffer, image.width, image.height,
                            stride_width, stride_height, stride_size,
                            constants.MEMORY_DVPP)
Ejemplo n.º 7
0
def main():
    """
    Program execution with picture directory parameters
    """
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclLiteResource()
    acl_resource.init()
    model = AclLiteModel(MODEL_PATH)
    dvpp = AclLiteImageProc(acl_resource)

    #From the parameters of the picture storage directory, reasoning by a picture
    image_dir = sys.argv[1]
    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]
    #Create a directory to store the inference results
    if not os.path.isdir('../out'):
        os.mkdir('../out')

    image_info = construct_image_info()

    for image_file in images_list:
        #read picture
        image = AclLiteImage(image_file)
        #preprocess image
        resized_image = pre_process(image, dvpp)
        print("pre process end")
        #reason pictures
        result = model.execute([resized_image, image_info])
        #process resresults
        post_process(result, image, image_file)
Ejemplo n.º 8
0
def main():
    """
    acl resource initialization
    """
    
    if not os.path.exists(OUTPUT_DIR):
        os.mkdir(OUTPUT_DIR)
     #ACL resource initialization
    acl_resource = AclLiteResource()
    acl_resource.init()
    dvpp_ = AclLiteImageProc()
    model = AclLiteModel(model_path)
    images_list = [os.path.join(INPUT_DIR, img)
                   for img in os.listdir(INPUT_DIR)
                   if os.path.splitext(img)[1] in IMG_EXT]
    
    print(images_list)
    
    start = time.time()
    for pic in images_list:
        image = AclLiteImage(pic)       

        l_data = preprocess(image, dvpp_)
        result_list = inference(model, [l_data,])
        postprocess(result_list, pic, OUTPUT_DIR)
Ejemplo n.º 9
0
def main():
    # check param
    if (len(sys.argv) != 3):
        print("The App arg is invalid")
        exit(1)
    # get all pictures
    image_dir = sys.argv[1]

    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]

    acl_resource = AclLiteResource()
    acl_resource.init()

    # instantiation Cartoonization object
    cartoonization = Cartoonization(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)
    ret = cartoonization.init()
    utils.check_ret("Cartoonization.init ", ret)

    # create dir to save result
    if not os.path.isdir('../out'):
        os.mkdir('../out')

    for image_file in images_list:
        # preprocess
        image = AclLiteImage(image_file)
        test_img = cartoonization.pre_process(image)
        # inference
        y_pred = cartoonization.inference([
            test_img,
        ])
        # postprocess
        cartoonization.post_process(y_pred, image_file)
Ejemplo n.º 10
0
def main(model_path):
    """main"""
    ## Initialization ##
    #initialize acl runtime
    acl_resource = AclLiteResource()
    acl_resource.init()

    ## Prepare Model ##
    # parameters for model path and model inputs
    model_parameters = {
        'model_dir': model_path,
        'width': 368,  # model input width      
        'height': 368,  # model input height
    }
    # perpare model instance: init (loading model from file to memory)
    # model_processor: preprocessing + model inference + postprocessing
    model_processor = ModelProcessor(acl_resource, model_parameters)

    ## Get Input ##
    # Initialize Camera
    cap = CameraCapture(camera_id=0, fps=10)

    ## Set Output ##
    # open the presenter channel

    chan = presenter_channel.open_channel(BODYPOSE_CONF)
    if chan is None:
        print("Open presenter channel failed")
        return

    while True:
        ## Read one frame from Camera ##
        img_original = cap.read()
        if not img_original:
            print('Error: Camera read failed')
            break
        # Camera Input (YUV) to RGB Image
        img_original = img_original.byte_data_to_np_array()
        img_original = YUVtoRGB(img_original)
        # img_original = cv2.flip(img_original, 1)

        ## Model Prediction ##
        # model_processor.predict: processing + model inference + postprocessing
        # canvas: the picture overlayed with human body joints and limbs
        canvas = model_processor.predict(img_original)

        ## Present Result ##
        # convert to jpeg image for presenter server display
        _, jpeg_image = cv2.imencode('.jpg', canvas)
        # construct AclLiteImage object for presenter server
        jpeg_image = AclLiteImage(jpeg_image, img_original.shape[0],
                                  img_original.shape[1], jpeg_image.size)
        # send to presenter server
        chan.send_detection_data(img_original.shape[0], img_original.shape[1],
                                 jpeg_image, [])

    # release the resources
    cap.release()
Ejemplo n.º 11
0
    def read(self):
        """Read frame from camera"""
        frame_data = CameraOutputC()
        ret = libacllite.ReadCameraFrame(self._id, byref(frame_data))
        if (ret != CAMERA_OK):
            log_error("Read camera %d failed" % (self._id))
            return None

        return AclLiteImage(addressof(frame_data.data.contents), self._width,
                            self._height, 0, 0, self._size, const.MEMORY_DVPP)
Ejemplo n.º 12
0
    def crop_and_paste(self, image, width, height, crop_and_paste_width,
                       crop_and_paste_height):
        """
        crop_and_paste
        """
        print('[AclLiteImageProc] vpc crop and paste stage:')
        input_desc = self._gen_input_pic_desc(image)
        stride_width = utils.align_up16(crop_and_paste_width)
        stride_height = utils.align_up2(crop_and_paste_height)
        out_buffer_size = utils.yuv420sp_size(stride_width, stride_height)
        out_buffer, ret = acl.media.dvpp_malloc(out_buffer_size)
        output_desc = self._gen_output_pic_desc(crop_and_paste_width,
                                                crop_and_paste_height,
                                                out_buffer, out_buffer_size)
        self._crop_config = acl.media.dvpp_create_roi_config(
            0, (width >> 1 << 1) - 1, 0, (height >> 1 << 1) - 1)
        # set crop area:
        rx = float(width) / float(crop_and_paste_width)
        ry = float(height) / float(crop_and_paste_height)
        if rx > ry:
            dx = 0
            r = rx
            dy = int((crop_and_paste_height - height / r) / 2)
        else:
            dy = 0
            r = ry
            dx = int((crop_and_paste_width - width / r) / 2)
        pasteRightOffset = int(crop_and_paste_width - 2 * dx)
        pasteBottomOffset = int(crop_and_paste_height - 2 * dy)
        if (pasteRightOffset % 2) == 0:
            pasteRightOffset = pasteRightOffset - 1
        if (pasteBottomOffset % 2) == 0:
            pasteBottomOffset = pasteBottomOffset - 1
        self._paste_config = acl.media.dvpp_create_roi_config(
            0, pasteRightOffset, 0, pasteBottomOffset)
        ret = acl.media.dvpp_vpc_crop_and_paste_async(self._dvpp_channel_desc,
                                                      input_desc, output_desc,
                                                      self._crop_config,
                                                      self._paste_config,
                                                      self._stream)
        utils.check_ret("acl.media.dvpp_vpc_crop_and_paste_async", ret)
        ret = acl.rt.synchronize_stream(self._stream)
        utils.check_ret("acl.rt.synchronize_stream", ret)
        print('[AclLiteImageProc] vpc crop and paste stage success')
        stride_width = crop_and_paste_width - 2 * dx
        stride_height = crop_and_paste_height - 2 * dy
        acl.media.dvpp_destroy_pic_desc(input_desc)
        acl.media.dvpp_destroy_pic_desc(output_desc)

        return AclLiteImage(out_buffer, image.width, image.height,
                            stride_width, stride_height, out_buffer_size,
                            constants.MEMORY_DVPP)
Ejemplo n.º 13
0
 def _get_pic_desc_data(self, pic_desc, user_data):
     pic_data = acl.media.dvpp_get_pic_desc_data(pic_desc)
     pic_data_size = acl.media.dvpp_get_pic_desc_size(pic_desc)
     ret_code = acl.media.dvpp_get_pic_desc_ret_code(pic_desc)
     if ret_code:
         channel_id, frame_id = user_data
         acl_log.log_error("Decode channel %d frame %d failed, error %d"
                           % (channel_id, frame_id, ret_code))
         acl.media.dvpp_free(pic_data)
     else:
         image = AclLiteImage(pic_data, self._width, self._height, 0, 0,
                          pic_data_size, const.MEMORY_DVPP)
         self._frame_queue.put(image)
     acl.media.dvpp_destroy_pic_desc(pic_desc)
Ejemplo n.º 14
0
    def jpege(self, image):
        """
        Convert yuv420sp pictures to jpeg pictures
        """
        # create input image
        input_desc = self._gen_input_pic_desc(image)
        # Predict the memory size required for conversion
        output_size, ret = acl.media.dvpp_jpeg_predict_enc_size(
            input_desc, self._jpege_config)
        if (ret != constants.ACL_SUCCESS):
            log_error("Predict jpege output size failed")
            return None
        # Request memory required for conversion
        output_buffer, ret = acl.media.dvpp_malloc(output_size)
        if (ret != constants.ACL_SUCCESS):
            log_error("Malloc jpege output memory failed")
            return None
        output_size_array = np.array([output_size], dtype=np.int32)
        if "bytes_to_ptr" in dir(acl.util):
            bytes_data = output_size_array.tobytes()
            output_size_ptr = acl.util.bytes_to_ptr(bytes_data)
        else:
            output_size_ptr = acl.util.numpy_to_ptr(output_size_array)

        # Call jpege asynchronous interface to convert pictures
        ret = acl.media.dvpp_jpeg_encode_async(self._dvpp_channel_desc,
                                               input_desc, output_buffer,
                                               output_size_ptr,
                                               self._jpege_config,
                                               self._stream)
        if (ret != constants.ACL_SUCCESS):
            log_error("Jpege failed, ret ", ret)
            return None
        # Wait for the conversion to complete
        ret = acl.rt.synchronize_stream(self._stream)
        if (ret != constants.ACL_SUCCESS):
            print("Jpege synchronize stream, failed, ret ", ret)
            return None
        # Release resources
        acl.media.dvpp_destroy_pic_desc(input_desc)
        if "bytes_to_ptr" in dir(acl.util):
            output_size_array = np.frombuffer(
                bytes_data,
                dtype=output_size_array.dtype).reshape(output_size_array.shape)
        return AclLiteImage(output_buffer, image.width, image.height, 0, 0,
                            int(output_size_array[0]), constants.MEMORY_DVPP)
Ejemplo n.º 15
0
def main():
    """
    main
    """
    image_dir = os.path.join(currentPath, "data")

    if not os.path.exists(OUT_DIR):
        os.mkdir(OUT_DIR)
    if not os.path.exists(OUTPUT_DIR):
        os.mkdir(OUTPUT_DIR)
    if not os.path.exists(MASK_DIR):
        os.mkdir(MASK_DIR)

    acl_resource = AclLiteResource()
    acl_resource.init()

    seg = Seg(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)
    ret = seg.init()
    utils.check_ret("seg.init ", ret)

    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]

    for image_file in images_list:
        image_name = os.path.basename(image_file)
        if image_name != 'background.jpg':
            print('====' + image_name + '====')
            # read image
            image = AclLiteImage(image_file)
            # Preprocess the picture
            resized_image = seg.pre_process(image)
            # Inference
            result = seg.inference([
                resized_image,
            ])
            # Post-processing
            mask = seg.post_process(result, image_name)
            # Fusion of segmented portrait and background image
            background_replace(os.path.join(image_dir, 'background.jpg'), \
                                        image_file, os.path.join(MASK_DIR, image_name))
Ejemplo n.º 16
0
 def resize(self, image, resize_width, resize_height):
     """
     Scale yuvsp420 picture to specified size
     """
     # Generate input picture desc
     input_desc = self._gen_input_pic_desc(image)
     # Calculate the image size after scaling
     stride_width = utils.align_up16(resize_width)
     stride_height = utils.align_up2(resize_height)
     output_size = utils.yuv420sp_size(stride_width, stride_height)
     # Request memory for the zoomed picture
     out_buffer, ret = acl.media.dvpp_malloc(output_size)
     if ret != constants.ACL_SUCCESS:
         log_error("AclLiteImageProc malloc failed, error: ", ret)
         return None
     # Create output image
     output_desc = self._gen_output_pic_desc(resize_width, resize_height,
                                             out_buffer, output_size)
     if output_desc is None:
         log_error("Gen resize output desc failed")
         return None
     # Call dvpp asynchronous zoom interface to zoom pictures
     ret = acl.media.dvpp_vpc_resize_async(self._dvpp_channel_desc,
                                           input_desc, output_desc,
                                           self._resize_config,
                                           self._stream)
     if ret != constants.ACL_SUCCESS:
         log_error("Vpc resize async failed, error: ", ret)
         return None
     # Wait for the zoom operation to complete
     ret = acl.rt.synchronize_stream(self._stream)
     if ret != constants.ACL_SUCCESS:
         log_error("Resize synchronize stream failed, error: ", ret)
         return None
     # Release the resources requested for scaling
     acl.media.dvpp_destroy_pic_desc(input_desc)
     acl.media.dvpp_destroy_pic_desc(output_desc)
     return AclLiteImage(out_buffer, resize_width, resize_height,
                         stride_width, stride_height, output_size,
                         constants.MEMORY_DVPP)
Ejemplo n.º 17
0
 def crop_and_paste_get_roi(self, image, width, height,
                            crop_and_paste_width, crop_and_paste_height):
     """
     :image: input image
     :width: input image width
     :height: input image height
     :crop_and_paste_width: crop_and_paste_width
     :crop_and_paste_height: crop_and_paste_height
     :return: return AclLiteImage
     """
     print('[AclLiteImageProc] vpc crop and paste stage:')
     input_desc = self._gen_input_pic_desc(image)
     stride_width = utils.align_up16(crop_and_paste_width)
     stride_height = utils.align_up2(crop_and_paste_height)
     out_buffer_size = utils.yuv420sp_size(stride_width, stride_height)
     out_buffer, ret = acl.media.dvpp_malloc(out_buffer_size)
     output_desc = self._gen_output_pic_desc(crop_and_paste_width,
                                             crop_and_paste_height,
                                             out_buffer, out_buffer_size)
     self._crop_config = acl.media.dvpp_create_roi_config(
         0, (width >> 1 << 1) - 1, 0, (height >> 1 << 1) - 1)
     self._paste_config = acl.media.dvpp_create_roi_config(
         0, crop_and_paste_width - 1, 0, crop_and_paste_height - 1)
     ret = acl.media.dvpp_vpc_crop_and_paste_async(self._dvpp_channel_desc,
                                                   input_desc, output_desc,
                                                   self._crop_config,
                                                   self._paste_config,
                                                   self._stream)
     utils.check_ret("acl.media.dvpp_vpc_crop_and_paste_async", ret)
     ret = acl.rt.synchronize_stream(self._stream)
     utils.check_ret("acl.rt.synchronize_stream", ret)
     print('[AclLiteImageProc] vpc crop and paste stage success')
     stride_width = utils.align_up16(crop_and_paste_width)
     stride_height = utils.align_up2(crop_and_paste_height)
     acl.media.dvpp_destroy_pic_desc(input_desc)
     acl.media.dvpp_destroy_pic_desc(output_desc)
     return AclLiteImage(out_buffer, image.width, image.height,
                         stride_width, stride_height, out_buffer_size,
                         constants.MEMORY_DVPP)
Ejemplo n.º 18
0
def main():
    """
    main
    """
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclLiteResource()
    acl_resource.init()

    gesture = Gesture(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)

    ret = gesture.init()
    utils.check_ret("Gesture.init ", ret)

    image_dir = sys.argv[1]
    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]

    if not os.path.isdir(os.path.join(SRC_PATH, "../out")):
        os.mkdir(os.path.join(SRC_PATH, "../out"))

    for image_file in images_list:

        image = AclLiteImage(image_file)

        resized_image = gesture.pre_process(image)
        print("pre process end")

        result = gesture.inference([
            resized_image,
        ])

        gesture.post_process(result, image_file)
Ejemplo n.º 19
0
def main():
    """
    main
    """
    image_dir = os.path.join(currentPath, "data")

    if not os.path.exists(OUTPUT_DIR):
        os.mkdir(OUTPUT_DIR)

    acl_resource = AclLiteResource()
    acl_resource.init()

    classify = Classify(MODEL_PATH, MODEL_WIDTH, MODEL_HEIGHT)
    ret = classify.init()
    utils.check_ret("Classify init ", ret)

    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]

    for image_file in images_list:
        print('=== ' + os.path.basename(image_file) + '===')

        # read image
        image = AclLiteImage(image_file)

        # Preprocess the picture
        resized_image = classify.pre_process(image)

        # Inferencecd
        result = classify.inference([
            resized_image,
        ])

        # # Post-processing
        classify.post_process(result, image_file)
Ejemplo n.º 20
0
def main():
    """main"""
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclLiteResource()
    acl_resource.init()
    model = AclLiteModel(MODEL_PATH)
    dvpp = AclLiteImageProc(acl_resource)

    image_dir = sys.argv[1]
    images_list = [
        os.path.join(image_dir, img) for img in os.listdir(image_dir)
        if os.path.splitext(img)[1] in const.IMG_EXT
    ]

    # Create a directory to save inference results
    if not os.path.isdir('../out'):
        os.mkdir('../out')

    # Create a directory to save the intermediate results of the large image detection
    if not os.path.isdir('./bigpic'):
        os.mkdir('./bigpic')

    # Create a directory to save the results of the big picture cropping inference
    outCrop = os.path.join('./bigpic', 'output')
    if not os.path.isdir(outCrop):
        os.mkdir(outCrop)

    # Create a directory, save the large and cropped pictures
    cropImg = os.path.join('./bigpic', 'cropimg')
    if not os.path.isdir(cropImg):
        os.mkdir(cropImg)

    image_info = construct_image_info()

    for image_file in images_list:
        imagename = get_file_name(image_file)
        tempfile = os.path.splitext(imagename)[0]

        imgdic = {}
        imgdic['name'] = imagename
        obj_res = []

        img = cv2.imread(image_file, -1)
        (width, height, depth) = img.shape
        if width > 1000 and height > 1000:
            # Create a directory to save the divided pictures of each big picture
            crop_target = os.path.join(cropImg, tempfile)
            if not os.path.isdir(crop_target):
                os.mkdir(crop_target)

            # Create a directory to save the inference results of each large image
            out_target = os.path.join(outCrop, tempfile)
            if not os.path.isdir(out_target):
                os.mkdir(out_target)

            # Large image clipping function
            crop_picture(image_file, crop_target)
            cropimg_list = [
                os.path.join(crop_target, imgs)
                for imgs in os.listdir(crop_target)
                if os.path.splitext(imgs)[1] in const.IMG_EXT
            ]

            # After the execution of the crop function is over,
            # the small picture after the big picture crop should be saved in a folder crop_target
            for cropimg_file in cropimg_list:
                print("the crop filename is :\t", cropimg_file)
                image = AclLiteImage(cropimg_file)
                resized_image = pre_process(image, dvpp)
                result = model.execute([resized_image, image_info])
                resdic = post_process_big(result, image, cropimg_file,
                                          out_target)
                obj_res.extend(resdic)

            imgdic['object_result'] = obj_res

            merge_picture(out_target, tempfile)

        # Read in the picture, if the picture size is less than 1000x1000,
        # it will be read in and processed normally
        else:
            print("detect the small picture")
            image = AclLiteImage(image_file)
            resized_image = pre_process(image, dvpp)
            print("pre process end")
            result = model.execute([resized_image, image_info])
            resdic = post_process(result, image, image_file)
            obj_res.extend(resdic)
            imgdic['object_result'] = obj_res
Ejemplo n.º 21
0
def main(model_path, frames_input_src, output_dir, is_presenter_server):
    """main"""
    ## Initialization ##
    #initialize acl runtime
    acl_resource = AclLiteResource()
    acl_resource.init()

    ## Prepare Model ##
    # parameters for model path and model inputs
    model_parameters = {
        'model_dir': model_path,
        'width': 368,  # model input width      
        'height': 368,  # model input height
    }
    # perpare model instance: init (loading model from file to memory)
    # model_processor: preprocessing + model inference + postprocessing
    model_processor = ModelProcessor(acl_resource, model_parameters)

    ## Get Input ##
    # Read the video input using OpenCV
    cap = cv2.VideoCapture(frames_input_src)

    ## Set Output ##
    if is_presenter_server:
        # if using presenter server, then open the presenter channel
        chan = presenteragent.presenter_channel.open_channel(BODYPOSE_CONF)
        if chan is None:
            print("Open presenter channel failed")
            return
    else:
        # if saving result as video file (mp4), then set the output video writer using opencv
        video_output_path = '{}/demo-{}-{}.mp4'.format(
            output_dir, os.path.basename(frames_input_src),
            str(random.randint(1, 100001)))
        video_writer = cv2.VideoWriter(video_output_path, 0x7634706d, 25,
                                       (1280, 720))
        if video_writer is None:
            print('Error: cannot get video writer from openCV')

    while (cap.isOpened()):
        ## Read one frame of the input video ##
        ret, img_original = cap.read()

        if not ret:
            print('Cannot read more, Reach the end of video')
            break

        ## Model Prediction ##
        # model_processor.predict: processing + model inference + postprocessing
        # canvas: the picture overlayed with human body joints and limbs
        canvas = model_processor.predict(img_original)

        ## Present Result ##
        if is_presenter_server:
            # convert to jpeg image for presenter server display
            _, jpeg_image = cv2.imencode('.jpg', canvas)
            # construct AclLiteImage object for presenter server
            jpeg_image = AclLiteImage(jpeg_image, img_original.shape[0],
                                      img_original.shape[1], jpeg_image.size)
            # send to presenter server
            chan.send_detection_data(img_original.shape[0],
                                     img_original.shape[1], jpeg_image, [])

        else:
            # save to video
            video_writer.write(canvas)

    # release the resources
    cap.release()
    if not is_presenter_server:
        video_writer.release()