Exemplo n.º 1
0
    def init(self):
        """
        init
        """
        self._dvpp = AclLiteImageProc()
        self._model = AclLiteModel(self._model_path)

        return constants.SUCCESS
Exemplo n.º 2
0
    def init(self):
        """
        Initialize
        """
        self._dvpp = AclLiteImageProc()
        # Load model
        self._model = AclLiteModel(self._model_path)

        return const.SUCCESS
Exemplo n.º 3
0
class Classify(object):
    """
    Class for portrait segmentation
    """
    def __init__(self, model_path, model_width, model_height):
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self._img_width = 0
        self._img_height = 0
        self._model = None
        self._dvpp = None

    def init(self):
        """
        Initialize
        """
        self._dvpp = AclLiteImageProc()
        # Load model
        self._model = AclLiteModel(self._model_path)

        return const.SUCCESS

    @utils.display_time
    def pre_process(self, image):
        """
        preprocess 
        """
        image_dvpp = image.copy_to_dvpp()
        yuv_image = self._dvpp.jpegd(image_dvpp)
        resized_image = self._dvpp.resize(yuv_image, self._model_width,
                                          self._model_height)
        return resized_image

    @utils.display_time
    def inference(self, input_data):
        """
        model inference
        """
        return self._model.execute(input_data)

    @utils.display_time
    def post_process(self, infer_output, image_file):
        """
        Post-processing, analysis of inference results
        """
        output_path = os.path.join(OUTPUT_DIR, os.path.basename(image_file))
        infer_result = infer_output[0]
        vals = infer_result.flatten()
        pre_index = vals.argsort()[-1]

        origin_img = Image.open(image_file)
        draw = ImageDraw.Draw(origin_img)
        font = ImageFont.truetype(
            "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", size=20)
        draw.text((10, 50), CLS[pre_index], font=font, fill=255)
        origin_img.save(output_path)
Exemplo n.º 4
0
    def __init__(self, acl_resource, model_path, model_width, model_height):
        self.total_buffer = None
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height

        self._model = AclLiteModel(model_path)
        self._dvpp = AclLiteImageProc(acl_resource)
        print("The App arg is __init__")
Exemplo n.º 5
0
class Seg(object):
    """
    Class for portrait segmentation
    """
    def __init__(self, model_path, model_width, model_height):
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self.device_id = 0
        self._dvpp = None
        self._model = None

    def init(self):
        """
        Initialize
        """
        # Initialize dvpp
        self._dvpp = AclLiteImageProc()

        # Load model
        self._model = AclLiteModel(self._model_path)

        return const.SUCCESS

    @utils.display_time
    def pre_process(self, image):
        """
        image preprocess
        """
        image_dvpp = image.copy_to_dvpp()
        yuv_image = self._dvpp.jpegd(image_dvpp)
        resized_image = self._dvpp.resize(yuv_image, self._model_width,
                                          self._model_height)
        return resized_image

    @utils.display_time
    def inference(self, input_data):
        """
        model inference
        """
        return self._model.execute(input_data)

    @utils.display_time
    def post_process(self, infer_output, image_name):
        """
        get mask
        """
        data = infer_output[0]
        vals = data.flatten()
        mask = np.clip((vals * 255), 0, 255)
        mask = mask.reshape(224, 224, 2)
        cv2.imwrite(os.path.join(MASK_DIR, image_name), mask[:, :, 0])
        return mask
Exemplo n.º 6
0
class Cartoonization(object):
    """
    class for Cartoonization
    """
    def __init__(self, model_path, model_width, model_height):
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self.device_id = 0
        self._model = None
        self._dvpp = None

    def init(self):
        """
        Initialize
        """
        self._dvpp = AclLiteImageProc()
        # Load model
        self._model = AclLiteModel(self._model_path)

        return const.SUCCESS

    @utils.display_time
    def pre_process(self, image, size=[256, 256]):
        """
        image preprocess
        """
        image_dvpp = image.copy_to_dvpp()
        yuv_image = self._dvpp.jpegd(image_dvpp)
        crop_and_paste_image = self._dvpp.crop_and_paste_get_roi(yuv_image, image.width, image.height, \
                                self._model_width, self._model_height)
        return crop_and_paste_image

    def inference(self, resized_image):
        """
        model inference
        """
        return self._model.execute(resized_image)

    @utils.display_time
    def post_process(self, infer_output, image_file):
        """
        post process
        """
        origin_image = cv2.imread(image_file).astype(np.float32)
        h, w = origin_image.shape[:2]

        image = ((np.squeeze(infer_output[0]) + 1) / 2 * 255)
        image = np.clip(image, 0, 255).astype(np.uint8)
        image = cv2.resize(image, (w, h))

        output_path = os.path.join("../out", os.path.basename(image_file))
        cv2.imwrite(output_path, cv2.cvtColor(image, cv2.COLOR_BGR2RGB))
Exemplo n.º 7
0
class Cartoonization(object):
    """
    class for Cartoonization
    """
    def __init__(self, model_path, model_width, model_height):
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self.device_id = 0
        self._dvpp = None
        self._model = None

    def init(self):
        """
        Initialize
        """
        # Initialize dvpp
        self._dvpp = AclLiteImageProc()

        # Load model
        self._model = AclLiteModel(self._model_path)

        return const.SUCCESS

    @utils.display_time
    def pre_process(self, image):
        """
        image preprocess
        """
        image_dvpp = image.copy_to_dvpp()
        yuv_image = self._dvpp.jpegd(image_dvpp)
        crop_and_paste_image = self._dvpp.crop_and_paste_get_roi(yuv_image, image.width, image.height, \
                                self._model_width, self._model_height)
        return crop_and_paste_image

    @utils.display_time
    def inference(self, resized_image):
        """
        model inference
        """
        return self._model.execute(resized_image)

    @utils.display_time
    def post_process(self, infer_output, image_file, origin_image):
        """
        post process
        """
        data = ((np.squeeze(infer_output[0]) + 1) * 127.5)
        img = cv2.cvtColor(data, cv2.COLOR_RGB2BGR)
        img = cv2.resize(img, (origin_image.width, origin_image.height))
        output_path = os.path.join("../out", os.path.basename(image_file))
        cv2.imwrite(output_path, img)
Exemplo n.º 8
0
class Classify(object):
    def __init__(self, acl_resource, model_path, model_width, model_height):
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self._dvpp = AclLiteImageProc(acl_resource)
        self._model = AclLiteModel(model_path)

    def __del__(self):
        if self._dvpp:
            del self._dvpp
        print("[Sample] class Samle release source success")

    def pre_process(self, image):
        yuv_image = self._dvpp.jpegd(image)
        resized_image = self._dvpp.resize(yuv_image, self._model_width,
                                          self._model_height)
        print("resize yuv end")
        return resized_image

    def inference(self, resized_image):
        return self._model.execute([
            resized_image,
        ])

    def post_process(self, infer_output, image_file):
        print("post process")
        data = infer_output[0]
        vals = data.flatten()
        top_k = vals.argsort()[-1:-6:-1]
        print("images:{}".format(image_file))
        print("======== top5 inference results: =============")
        for n in top_k:
            object_class = get_image_net_class(n)
            print("label:%d  confidence: %f, class: %s" %
                  (n, vals[n], object_class))

        #using pillow, the category with the highest confidence is written on the image and saved locally
        if len(top_k):
            object_class = get_image_net_class(top_k[0])
            output_path = os.path.join(os.path.join(SRC_PATH, "../out"),
                                       os.path.basename(image_file))
            origin_img = Image.open(image_file)
            draw = ImageDraw.Draw(origin_img)
            font = ImageFont.truetype(
                "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
                size=20)
            draw.text((10, 50), object_class, font=font, fill=255)
            origin_img.save(output_path)
Exemplo n.º 9
0
def main():
    """main"""
    #acl init
    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]

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

    #infer picture
    for pic in images_list:
        #get pic data
        orig_shape, l_data = preprocess(pic)
        #inference
        result_list = model.execute([l_data])    
        #postprocess
        postprocess(result_list, pic, orig_shape, pic)
    print("Execute end")
Exemplo n.º 10
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")
Exemplo n.º 11
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)
Exemplo n.º 12
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)
Exemplo n.º 13
0
    def _thread_entry(self, args_list):
        self._context, ret = acl.rt.create_context(0)
        utils.check_ret("acl.rt.create_context", ret)
        self._cap = video.VideoCapture(self._stream_name)
        self._dvpp = AclLiteImageProc()
        self._status = STATUS_PREPROC_RUNNING
        frame_cnt = 0
        while self._status == STATUS_PREPROC_RUNNING:
            ret, image = self._cap.read()
            if ret:
                log_error("Video %s decode failed" % (self._stream_name))
                self._status = STATUS_PREPROC_ERROR
                break

            if (image is None) and self._cap.is_finished():
                log_info("Video %s decode finish" % (self._stream_name))
                self._status = STATUS_PREPROC_EXIT
                break

            if image and (int(frame_cnt) % 5 == 0):
                self._process_frame(image)
            frame_cnt += 1

        self._thread_exit()
Exemplo n.º 14
0
class Gesture(object):
    """
	define gesture class
    """
    def __init__(self, model_path, model_width, model_height):
        self.device_id = 0
        self.context = None
        self.stream = None
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self._dvpp = None
        self._model = None

    def init(self):
        """
        Initialize
        """
        # Initialize dvpp
        self._dvpp = AclLiteImageProc()

        # Load model
        self._model = AclLiteModel(self._model_path)

        return const.SUCCESS

    def pre_process(self, image):
        """
        pre_precess
        """
        image_dvpp = image.copy_to_dvpp()
        yuv_image = self._dvpp.jpegd(image_dvpp)
        print("decode jpeg end")
        resized_image = self._dvpp.resize(yuv_image, self._model_width,
                                          self._model_height)
        print("resize yuv end")
        return resized_image

    def inference(self, resized_image):
        """
	    inference
        """
        return self._model.execute(resized_image)

    def post_process(self, infer_output, image_file):
        """
	    post_process
        """
        print("post process")
        data = infer_output[0]
        vals = data.flatten()
        top_k = vals.argsort()[-1:-2:-1]
        print("images:{}".format(image_file))
        print("======== top5 inference results: =============")
        for n in top_k:
            object_class = get_gesture_categories(n)
            print("label:%d  confidence: %f, class: %s" %
                  (n, vals[n], object_class))

        if len(top_k):
            object_class = get_gesture_categories(top_k[0])
            output_path = os.path.join(os.path.join(SRC_PATH, "../out"),
                                       os.path.basename(image_file))
            origin_img = Image.open(image_file)
            draw = ImageDraw.Draw(origin_img)
            font = ImageFont.truetype(
                "/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf",
                size=20)
            draw.text((10, 50), object_class, font=font, fill=255)
            origin_img.save(output_path)
Exemplo n.º 15
0
class Classify(object):
    def __init__(self, acl_resource, model_path, model_width, model_height):
        self.total_buffer = None
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height

        self._model = AclLiteModel(model_path)
        self._dvpp = AclLiteImageProc(acl_resource)
        print("The App arg is __init__")

    def __del__(self):
        if self.total_buffer:
            acl.rt.free(self.total_buffer)  
        if self._dvpp:
            del self._dvpp
        print("[Sample] class Samle release source success")

    def pre_process(self, image):
        yuv_image = self._dvpp.jpegd(image)
        print("decode jpeg end")
        resized_image = self._dvpp.resize(yuv_image, 
                        self._model_width, self._model_height)
        print("resize yuv end")
        return resized_image
    
    def batch_process(self, resized_image_list, batch):
        resized_img_data_list = []
        resized_img_size = resized_image_list[0].size
        total_size = batch * resized_img_size
        stride = 0
        for resized_image in resized_image_list:
            resized_img_data_list.append(resized_image.data())
        self.total_buffer, ret = acl.rt.malloc(total_size, ACL_MEM_MALLOC_HUGE_FIRST)
        check_ret("acl.rt.malloc", ret)    
        for i in range(len(resized_image_list)):
            ret = acl.rt.memcpy(self.total_buffer + stride, resized_img_size,\
                        resized_img_data_list[i], resized_img_size,\
                        ACL_MEMCPY_DEVICE_TO_DEVICE)
            check_ret("acl.rt.memcpy", ret)
            stride += resized_img_size
        return total_size
    
    def inference(self, resized_image_list, batch):
        total_size = self.batch_process(resized_image_list, batch)
        batch_buffer = {'data': self.total_buffer, 'size':total_size}
        return self._model.execute([batch_buffer, ])
    
    def post_process(self, infer_output, batch_image_files, number_of_images):
        print("post process") 
        datas = infer_output[0]
        
        for number in range(number_of_images):
            data = datas[number]
            vals = data.flatten()
            top_k = vals.argsort()[-1:-6:-1]
            print("images:{}".format(batch_image_files[number]))
            print("======== top5 inference results: =============")
            for n in top_k:
                object_class = get_image_net_class(n)
                print("label:%d  confidence: %f, class: %s" % (n, vals[n], object_class))
            
            #Use Pillow to write the categories with the highest confidence on the image and save them locally
            if len(top_k):
                object_class = get_image_net_class(top_k[0])
                output_path = os.path.join("../out", os.path.basename(batch_image_files[number]))
                origin_img = Image.open(batch_image_files[number])
                draw = ImageDraw.Draw(origin_img)
                font = ImageFont.truetype("/usr/share/fonts/truetype/dejavu/DejaVuSans-Bold.ttf", size=20)
                draw.text((10, 50), object_class, font=font, fill=255)
                origin_img.save(output_path)
Exemplo n.º 16
0
class CrowdCount(object):
    """
    crowdCount
    """
    def __init__(self, model_path, model_width, model_height):
        self.device_id = 0
        self.context = None
        self.stream = None
        self._model = None
        self.run_mode = None
        self._model_path = model_path
        self._model_width = model_width
        self._model_height = model_height
        self._dvpp = None
        self._model = None

    def init(self):
        """
        init
        """
        self._dvpp = AclLiteImageProc()
        self._model = AclLiteModel(self._model_path)

        return constants.SUCCESS

    def pre_process(self, image):
        """
        image preprocess
        """
        image_dvpp = image.copy_to_dvpp()
        yuv_image = self._dvpp.jpegd(image_dvpp)
        crop_and_paste_image = \
            self._dvpp.crop_and_paste(yuv_image, image.width, image.height, self._model_width, self._model_height)
        return crop_and_paste_image

    def inference(self, input_data):
        """
        model inference
        """
        return self._model.execute(input_data)

    def post_process(self, image, infer_output, image_file):
        """
        Post-processing, analysis of inference results
        """
        orig = cv2.imread(image_file, 1)
        orig = cv2.resize(orig, (image.width, image.height))
        data = infer_output[0]
        vals = data.flatten()
        res = np.sum(vals, axis=0)
        result = round(res / 1000.0)
        data_2 = data.reshape(800, 1408)
        heatMap = data_2[:image.height, :image.width]
        heatMap = heatMap.astype(np.uint8)
        heatMap = cv2.GaussianBlur(heatMap, (0, 0), 5, 5, cv2.BORDER_DEFAULT)
        cv2.normalize(heatMap, heatMap, 0, 255, cv2.NORM_MINMAX, cv2.CV_8UC1)
        heatMap = cv2.applyColorMap(heatMap, cv2.COLORMAP_JET)
        add_img = cv2.addWeighted(orig, 1, heatMap, 0.5, 0.0)
        cv2.putText(add_img, str(result), (30, 60), cv2.FONT_HERSHEY_PLAIN, 5,
                    (0, 0, 255), 8)
        output_path = os.path.join("../out", os.path.basename(image_file))
        cv2.imwrite(output_path, add_img)
Exemplo n.º 17
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
Exemplo n.º 18
0
 def __init__(self, acl_resource, model_width, model_height):
     self._acl_resource = acl_resource
     self._model_width = model_width
     self._model_height = model_height
     self._dvpp = AclLiteImageProc(acl_resource)
Exemplo n.º 19
0
class VggSsd(object):
    def __init__(self, acl_resource, model_width, model_height):
        self._acl_resource = acl_resource
        self._model_width = model_width
        self._model_height = model_height
        self._dvpp = AclLiteImageProc(acl_resource)

    def __del__(self):
        print("Release yolov3 resource finished")

    def pre_process(self, image):
        """
        pre_process
        """
        resized_image = self._dvpp.resize(image, self._model_width,
                                          self._model_height)
        if resized_image is None:
            print("Resize image failed")
            return None
        return [resized_image,]

    def post_process(self, infer_output, origin_img):
        """
        post_process
        """
        detection_result_list = self._analyze_inference_output(infer_output, 
                                                               origin_img)
        jpeg_image = self._dvpp.jpege(origin_img)
        return jpeg_image, detection_result_list

    def overlap(self, x1, x2, x3, x4):
        """
        overlap
        """
        left = max(x1, x3)
        right = min(x2, x4)
        return right - left
    
    def cal_iou(self, box, truth):
        """
        cal_iou
        """
        w = self.overlap(box[0], box[2], truth[0], truth[2])
        h = self.overlap(box[1], box[3], truth[1], truth[3])
        if w <= 0 or h <= 0:
            return 0
        inter_area = w * h
        union_area = (box[2] - box[0]) * (box[3] - box[1]) + (truth[2] - truth[0]) * (truth[3] - truth[1]) - inter_area
        return inter_area * 1.0 / union_area
    
    def apply_nms(self, all_boxes, thres):
        """
        apply_nms
        """
        res = []
        for cls in range(class_num):
            cls_bboxes = all_boxes[cls]
            sorted_boxes = sorted(cls_bboxes, key=lambda d: d[5])[::-1]
    
            p = dict()
            for i in range(len(sorted_boxes)):
                if i in p:
                    continue
                truth = sorted_boxes[i]
                for j in range(i + 1, len(sorted_boxes)):
                    if j in p:
                        continue
                    box = sorted_boxes[j]
                    iou = self.cal_iou(box, truth)
                    if iou >= thres:
                        p[j] = 1
            for i in range(len(sorted_boxes)):
                if i not in p:
                    res.append(sorted_boxes[i])
        return res
    
    def decode_bbox(self, conv_output, anchors, img_w, img_h, x_scale, y_scale, shift_x_ratio, shift_y_ratio):
        """
        decode_bbox
        """
        def _sigmoid(x):
            s = 1 / (1 + np.exp(-x))
            return s
    
        h, w, _ = conv_output.shape
        pred = conv_output.reshape((h * w, 3, 5 + class_num))
    
        pred[..., 4:] = _sigmoid(pred[..., 4:])
        pred[..., 0] = (_sigmoid(pred[..., 0]) + np.tile(range(w), (3, h)).transpose((1, 0))) / w
        pred[..., 1] = (_sigmoid(pred[..., 1]) + np.tile(np.repeat(range(h), w), (3, 1)).transpose((1, 0))) / h
        pred[..., 2] = np.exp(pred[..., 2]) * anchors[:, 0:1].transpose((1, 0)) / w
        pred[..., 3] = np.exp(pred[..., 3]) * anchors[:, 1:2].transpose((1, 0)) / h
    
        bbox = np.zeros((h * w, 3, 4))
        bbox[..., 0] = np.maximum((pred[..., 0] - pred[..., 2] / 2.0 - shift_x_ratio) * x_scale * img_w, 0)  # x_min
        bbox[..., 1] = np.maximum((pred[..., 1] - pred[..., 3] / 2.0 - shift_y_ratio) * y_scale * img_h, 0)  # y_min
        bbox[..., 2] = np.minimum((pred[..., 0] + pred[..., 2] / 2.0 - shift_x_ratio) * x_scale * img_w, img_w)  # x_max
        bbox[..., 3] = np.minimum((pred[..., 1] + pred[..., 3] / 2.0 - shift_y_ratio) * y_scale * img_h, img_h)  # y_max
    
        pred[..., :4] = bbox
        pred = pred.reshape((-1, 5 + class_num))
        pred[:, 4] = pred[:, 4] * pred[:, 5:].max(1)
        pred = pred[pred[:, 4] >= conf_threshold]
        pred[:, 5] = np.argmax(pred[:, 5:], axis=-1)
    
        all_boxes = [[] for ix in range(class_num)]
        for ix in range(pred.shape[0]):
            box = [int(pred[ix, iy]) for iy in range(4)]
            box.append(int(pred[ix, 5]))
            box.append(pred[ix, 4])
            all_boxes[box[4] - 1].append(box)
        
        return all_boxes
    
    def convert_labels(self, label_list):
        """
        convert_labels
        """
        if isinstance(label_list, np.ndarray):
            label_list = label_list.tolist()
            label_names = [labels[int(index)] for index in label_list]
        return label_names
    
    def _analyze_inference_output(self, infer_output, origin_img):
        """
        _analyze_inference_output
        """
        result_return = dict()
        img_h = origin_img.height
        img_w = origin_img.width
        scale = min(float(MODEL_WIDTH) / float(img_w), float(MODEL_HEIGHT) / float(img_h))
        new_w = int(img_w * scale)
        new_h = int(img_h * scale)
        shift_x_ratio = (MODEL_WIDTH - new_w) / 2.0 / MODEL_WIDTH
        shift_y_ratio = (MODEL_HEIGHT- new_h) / 2.0 / MODEL_HEIGHT
        class_num = len(labels)
        num_channel = 3 * (class_num + 5)
        x_scale = MODEL_WIDTH / float(new_w)
        y_scale = MODEL_HEIGHT / float(new_h)
        all_boxes = [[] for ix in range(class_num)]
        for ix in range(3):
            pred = infer_output[2 - ix].reshape((MODEL_HEIGHT // stride_list[ix], MODEL_WIDTH // stride_list[ix], num_channel))
            anchors = anchor_list[ix]
            boxes = self.decode_bbox(pred, anchors, img_w, img_h, x_scale, y_scale, shift_x_ratio, shift_y_ratio)
            all_boxes = [all_boxes[iy] + boxes[iy] for iy in range(class_num)]

        res = self.apply_nms(all_boxes, iou_threshold)
        if not res:
            result_return['detection_classes'] = []
            result_return['detection_boxes'] = []
            result_return['detection_scores'] = []
        else:
            new_res = np.array(res)
            picked_boxes = new_res[:, 0:4]
            picked_boxes = picked_boxes[:, [1, 0, 3, 2]]
            picked_classes = self.convert_labels(new_res[:, 4])
            picked_score = new_res[:, 5]
            result_return['detection_classes'] = picked_classes
            result_return['detection_boxes'] = picked_boxes.tolist()
            result_return['detection_scores'] = picked_score.tolist()

        detection_result_list = []
        for i in range(len(result_return['detection_classes'])):
            box = result_return['detection_boxes'][i]
            class_name = result_return['detection_classes'][i]
            confidence = result_return['detection_scores'][i]
            detection_item = presenter_datatype.ObjectDetectionResult()            
            detection_item.confidence = confidence
            detection_item.box.lt.x = int(box[1])
            detection_item.box.lt.y = int(box[0])
            detection_item.box.rb.x = int(box[3])
            detection_item.box.rb.y = int(box[2])
            detection_item.result_text = str(class_name)
            detection_result_list.append(detection_item)
        return detection_result_list
Exemplo n.º 20
0
class Preprocess(object):
    """preprocess"""
    def __init__(self, stream_name, channel, resize_width, resize_height):
        self._stream_name = str(stream_name)
        self._channel = int(channel)
        self._resize_width = resize_width
        self._resize_height = resize_height
        self._status = STATUS_PREPROC_INIT
        self._display = False
        self._dvpp = None
        self._cap = None
        self._context = None
        self._image_queue = queue.Queue(64)

    def _start(self):
        thread_id, ret = acl.util.start_thread(self._thread_entry, [])
        utils.check_ret("acl.util.start_thread", ret)

        log_info("Start sub thread ok, wait init...")
        while self._status == STATUS_PREPROC_INIT:
            time.sleep(0.001)
        log_info("Status changed to ", self._status)

        while self._status == STATUS_PREPROC_RUNNING:
            if self._image_queue.qsize() > 0:
                break
            time.sleep(0.001)

        return self._status != STATUS_PREPROC_ERROR

    def _thread_entry(self, args_list):
        self._context, ret = acl.rt.create_context(0)
        utils.check_ret("acl.rt.create_context", ret)
        self._cap = video.VideoCapture(self._stream_name)
        self._dvpp = AclLiteImageProc()
        self._status = STATUS_PREPROC_RUNNING
        frame_cnt = 0
        while self._status == STATUS_PREPROC_RUNNING:
            ret, image = self._cap.read()
            if ret:
                log_error("Video %s decode failed" % (self._stream_name))
                self._status = STATUS_PREPROC_ERROR
                break

            if (image is None) and self._cap.is_finished():
                log_info("Video %s decode finish" % (self._stream_name))
                self._status = STATUS_PREPROC_EXIT
                break

            if image and (int(frame_cnt) % 5 == 0):
                self._process_frame(image)
            frame_cnt += 1

        self._thread_exit()

    def _process_frame(self, frame):
        resized_image = self._dvpp.resize(frame, self._resize_width,
                                          self._resize_height)
        if resized_image is None:
            log_error("dvpp resize image failed")
            return

        jpg_image = None
        if self._display:
            jpg_image = self._dvpp.jpege(frame)
            if jpg_image is None:
                log_error("dvpp jpege failed")
                return

        data = PreProcData(self._channel, frame.width, frame.height,
                           resized_image, jpg_image, self._display)

        self._image_queue.put(data)

    def _thread_exit(self):
        self._status = STATUS_PREPROC_EXIT
        log_info("Channel %d thread exit..." % (self._channel))
        if self._dvpp is not None:
            del self._dvpp
            self._dvpp = None

        if self._cap is not None:
            while self._cap._dextory_dvpp_flag == False:
                time.sleep(0.001)
            del self._cap
            self._cap = None

        if self._context is not None:
            acl.rt.destroy_context(self._context)
            self._context = None
        log_info("Channel %d thread exit ok" % (self._channel))

    def set_display(self, display):
        """set display"""
        self._display = display

    def is_finished(self):
        """
        Function description:
            Judge whether the process is completed
        Parameter:
            none
        Return Value:
            True or False
        Exception Description:
            none
        """
        return self._status > STATUS_PREPROC_RUNNING

    def get_data(self):
        """
        The method for getting data 
        """
        if self._status >= STATUS_PREPROC_EXIT:
            return False, None
        elif self._status == STATUS_PREPROC_INIT:
            ret = self._start()
            if ret == False:
                log_error("decode channel %d failed" % (self._channel))
                return False, None

        if self._image_queue.empty():
            return True, None

        preproc_data = self._image_queue.get_nowait()
        return True, preproc_data

    def __del__(self):
        self._thread_exit()