예제 #1
0
파일: vgg_ssd.py 프로젝트: GXWORLD/samples
 def __init__(self, acl_resource, model_width, model_height):
     self._acl_resource = acl_resource
     self._model_width = model_width
     self._model_height = model_height
     #Use dvpp to process images, when using opencv or PIL,
     # you don't need to create a dvpp instance
     self._dvpp = Dvpp(acl_resource)
예제 #2
0
 def init(self):    
     """
     init
     """ 
     self._dvpp = Dvpp() 
     self._model = Model(self._model_path)
 
     return constants.SUCCESS
예제 #3
0
파일: main.py 프로젝트: Judithsq/samples
    def init(self):
        """
        Initialize
        """
        self._dvpp = Dvpp()
        # Load model
        self._model = Model(self._model_path)

        return const.SUCCESS
예제 #4
0
파일: main.py 프로젝트: Judithsq/samples
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 = Dvpp()
        # Load model
        self._model = Model(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.load_default()
        draw.text((10, 50), CLS[pre_index], font=font, fill=255)
        origin_img.save(output_path)
예제 #5
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 = Dvpp()

        # Load model
        self._model = Model(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("../outputs", os.path.basename(image_file))
        cv2.imwrite(output_path, img)
예제 #6
0
def main():
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclResource()
    acl_resource.init()
    model = Model(MODEL_PATH)
    dvpp = Dvpp(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, "../outputs")):
        os.mkdir(os.path.join(SRC_PATH, "../outputs"))

    image_info = construct_image_info()
    for image_file in images_list:
        image = AclImage(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")
예제 #7
0
def main():
    """main"""
    #acl init
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)
    acl_resource = AclResource()
    acl_resource.init()
    model = Model(MODEL_PATH)
    dvpp = Dvpp(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, "../outputs")):
        os.mkdir(os.path.join(SRC_PATH, "../outputs"))

    #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")
예제 #8
0
def main():
    """
    Program execution with picture directory parameters
    """
    
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)
    
    acl_resource = AclResource()
    acl_resource.init()
    model = Model(MODEL_PATH)
    dvpp = Dvpp(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('../outputs'):
        os.mkdir('../outputs')

    image_info = construct_image_info()

    for image_file in images_list:
        #read picture
        image = AclImage(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)
예제 #9
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.AclVideo(self._stream_name)
        self._dvpp = Dvpp() 
        self._status = STATUS_PREPROC_RUNNING
        frame_cnt = 0
        while self._status == STATUS_PREPROC_RUNNING: 
            ret, image = self._cap.read()
            if ret or (image is None):
                if ret == const.VIDEO_DECODE_FINISH:
                    log_info("Video %s decode finish"%(self._stream_name))
                    self._status = STATUS_PREPROC_EXIT
                else:
                    log_info("Video %s decode failed"%(self._stream_name))
                    self._status = STATUS_PREPROC_ERROR
                break
            if (int(frame_cnt) % 5 == 0):
                self._process_frame(image)  
            time.sleep(0.0)

        self._thread_exit()        
예제 #10
0
def main():
    """main"""
    if (len(sys.argv) != 2):
        print("The App arg is invalid")
        exit(1)

    acl_resource = AclResource()
    acl_resource.init()
    model = Model(MODEL_PATH)
    dvpp = Dvpp(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('./outputs'):
        os.mkdir('./outputs')

    # 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 = AclImage(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 = AclImage(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
예제 #11
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 = Dvpp()

        # Load model
        self._model = Model(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, "../outputs"),
                                       os.path.basename(image_file))
            origin_img = Image.open(image_file)
            draw = ImageDraw.Draw(origin_img)
            font = ImageFont.load_default()
            draw.text((10, 50), object_class, font=font, fill=255)
            origin_img.save(output_path)
예제 #12
0
class 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_width
        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._start == 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.AclVideo(self._stream_name)
        self._dvpp = Dvpp() 
        self._status = STATUS_PREPROC_RUNNING
        frame_cnt = 0
        while self._status == STATUS_PREPROC_RUNNING: 
            ret, image = self._cap.read()
            if ret or (image is None):
                if ret == const.VIDEO_DECODE_FINISH:
                    log_info("Video %s decode finish"%(self._stream_name))
                    self._status = STATUS_PREPROC_EXIT
                else:
                    log_info("Video %s decode failed"%(self._stream_name))
                    self._status = STATUS_PREPROC_ERROR
                break
            if (int(frame_cnt) % 5 == 0):
                self._process_frame(image)  
            time.sleep(0.0)

        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 != None:
            del self._dvpp
            self._dvpp = None

        if self._cap != None:
            del self._cap
            self._cap = None

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

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

    def is_finished(self):
        return self._status > STATUS_PREPROC_RUNNING

    def get_data(self):
        ret = True
        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()
        if preproc_data is None:
            ret = False
        
        return ret, preproc_data  

    def __del__(self):
        self._thread_exit()
예제 #13
0
파일: vgg_ssd.py 프로젝트: GXWORLD/samples
class VggSsd(object):
    """vggssd"""
    def __init__(self, acl_resource, model_width, model_height):
        self._acl_resource = acl_resource
        self._model_width = model_width
        self._model_height = model_height
        #Use dvpp to process images, when using opencv or PIL,
        # you don't need to create a dvpp instance
        self._dvpp = Dvpp(acl_resource)

    def __del__(self):
        if self._dvpp:
            del self._dvpp
        print("Release yolov3 resource finished")

    def pre_process(self, image):
        """Use dvpp to scale the image to the required size of the model"""
        resized_image = self._dvpp.resize(image, self._model_width,
                                          self._model_height)
        if resized_image is None:
            print("Resize image failed")
            return None
        #Output the scaled image and image information as inference input data
        return [
            resized_image,
        ]

    def post_process(self, infer_output, origin_img):
        """Analyze inference output data"""
        detection_result_list = self._analyze_inference_output(
            infer_output, origin_img)
        #Convert yuv image to jpeg image
        jpeg_image = self._dvpp.jpege(origin_img)
        return jpeg_image, detection_result_list

    def _analyze_inference_output(self, infer_output, origin_img):
        #vgg ssd has two outputs, the first output
        # infer_output[0] is the number of detected objects, and the shape is (1,8)
        box_num = int(infer_output[0][0, 0])
        #The second output infer_output[1] is the detected object information, the shape is (1, 200, 8)
        box_info = infer_output[1][0]
        detection_result_list = []
        for i in range(box_num):
            #Detected object confidence
            score = box_info[i, SCORE]
            if score < 0.9:
                break

            detection_item = presenter_datatype.ObjectDetectionResult()
            detection_item.confidence = score
            #Person face position frame coordinates, normalized coordinates,
            # need to be multiplied by the width and height of the picture to convert to the coordinates on the picture
            detection_item.box.lt.x = int(box_info[i, TOP_LEFT_X] *
                                          origin_img.width)
            detection_item.box.lt.y = int(box_info[i, TOP_LEFT_Y] *
                                          origin_img.height)
            detection_item.box.rb.x = int(box_info[i, BOTTOM_RIGHT_X] *
                                          origin_img.width)
            detection_item.box.rb.y = int(box_info[i, BOTTOM_RIGHT_Y] *
                                          origin_img.height)
            #Organize the confidence into a string
            detection_item.result_text = str(
                round(detection_item.confidence * 100, 2)) + "%"
            detection_result_list.append(detection_item)

        return detection_result_list
예제 #14
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 = Dvpp() 
        self._model = Model(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, infer_output, image_file):
        """
        Post-processing, analysis of inference results
        """
        orig = cv2.imread(image_file, 1)
        orig = cv2.resize(orig, (1200, 800))
        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[:800, :1200]
        heatMap = heatMap.astype(np.uint8)
        heatMap = cv2.GaussianBlur(heatMap, (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("./outputs", os.path.basename(image_file))
        cv2.imwrite(output_path, add_img)