paddleocr 识别的核心部分的代码

Lib\site-packages\paddleocr\tools\infer\predict_system.py(用的python39目录里边的库

def __call__(self, img, cls=True, slice={}):
    time_dict = {"det": 0, "rec": 0, "cls": 0, "all": 0}

    if img is None:
        logger.debug("no valid image provided")
        return None, None, time_dict

    start = time.time()
    ori_im = img.copy()
    if slice:
        slice_gen = slice_generator(
            img,
            horizontal_stride=slice["horizontal_stride"],
            vertical_stride=slice["vertical_stride"],
        )
        elapsed = []
        dt_slice_boxes = []
        for slice_crop, v_start, h_start in slice_gen:
            dt_boxes, elapse = self.text_detector(slice_crop, use_slice=True)
            if dt_boxes.size:
                dt_boxes[:, :, 0] += h_start
                dt_boxes[:, :, 1] += v_start
                dt_slice_boxes.append(dt_boxes)
                elapsed.append(elapse)
        dt_boxes = np.concatenate(dt_slice_boxes)

        dt_boxes = merge_fragmented(
            boxes=dt_boxes,
            x_threshold=slice["merge_x_thres"],
            y_threshold=slice["merge_y_thres"],
        )
        elapse = sum(elapsed)
    else:
        dt_boxes, elapse = self.text_detector(img)

    time_dict["det"] = elapse

    if dt_boxes is None:
        logger.debug("no dt_boxes found, elapsed : {}".format(elapse))
        end = time.time()
        time_dict["all"] = end - start
        return None, None, time_dict
    else:
        logger.debug(
            "dt_boxes num : {}, elapsed : {}".format(len(dt_boxes), elapse)
        )
    img_crop_list = []

    dt_boxes = sorted_boxes(dt_boxes)

    for bno in range(len(dt_boxes)):
        tmp_box = copy.deepcopy(dt_boxes[bno])
        if self.args.det_box_type == "quad":
            img_crop = get_rotate_crop_image(ori_im, tmp_box)
        else:
            img_crop = get_minarea_rect_crop(ori_im, tmp_box)
        img_crop_list.append(img_crop)
    if self.use_angle_cls and cls:
        img_crop_list, angle_list, elapse = self.text_classifier(img_crop_list)
        time_dict["cls"] = elapse
        logger.debug(
            "cls num  : {}, elapsed : {}".format(len(img_crop_list), elapse)
        )
    if len(img_crop_list) > 1000:
        logger.debug(
            f"rec crops num: {len(img_crop_list)}, time and memory cost may be large."
        )

    rec_res, elapse = self.text_recognizer(img_crop_list)
    time_dict["rec"] = elapse
    logger.debug("rec_res num  : {}, elapsed : {}".format(len(rec_res), elapse))
    if self.args.save_crop_res:
        self.draw_crop_rec_res(self.args.crop_res_save_dir, img_crop_list, rec_res)
    filter_boxes, filter_rec_res = [], []
    for box, rec_result in zip(dt_boxes, rec_res):
        text, score = rec_result[0], rec_result[1]
        if score >= self.drop_score:
            filter_boxes.append(box)
            filter_rec_res.append(rec_result)
    end = time.time()
    time_dict["all"] = end - start
    return filter_boxes, filter_rec_res, time_dict

 

耗时测试


timestamp = time.time()
current_time = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime())
milliseconds = int((timestamp - int(timestamp)) * 1000)
current_time_with_ms = f"{current_time}.{milliseconds:03d}"
print("当前时间(到毫秒):", current_time_with_ms, __file__)

 

processes=1就行,开了俩进程但是ocr的只有一个

 

 

艹,发现占用时间最长的不是ocr,而是预处理

当前时间(到毫秒): 2025-01-05 12:03:29.344 ....py
当前时间(到毫秒): 2025-01-05 12:03:29.344 lib\site-packages\paddleocr\paddleocr.py
当前时间(到毫秒): 2025-01-05 12:03:29.630 lib\site-packages\paddleocr\paddleocr.py 可以看到预处理花了0.3秒
当前时间(到毫秒): 2025-01-05 12:03:29.659 而ocr花了0.03秒

 

posted @ 2025-01-05 12:32  hrdom  阅读(6)  评论(0编辑  收藏  举报