PaddleOCR 文字检测部分源码学习(1)
生活随笔
收集整理的這篇文章主要介紹了
PaddleOCR 文字检测部分源码学习(1)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
2021SC@SDUSC
代碼位置:tools->infer->predict_det.py
class TextDetector(object):def __init__(self, args):self.args = argsself.det_algorithm = args.det_algorithmpre_process_list = [{'DetResizeForTest': {'limit_side_len': args.det_limit_side_len,'limit_type': args.det_limit_type,}}, {'NormalizeImage': {'std': [0.229, 0.224, 0.225],'mean': [0.485, 0.456, 0.406],'scale': '1./255.','order': 'hwc'}}, {'ToCHWImage': None}, {'KeepKeys': {'keep_keys': ['image', 'shape']}}]postprocess_params = {}這里是一些參數的初始化。
if self.det_algorithm == "DB":postprocess_params['name'] = 'DBPostProcess'postprocess_params["thresh"] = args.det_db_threshpostprocess_params["box_thresh"] = args.det_db_box_threshpostprocess_params["max_candidates"] = 1000postprocess_params["unclip_ratio"] = args.det_db_unclip_ratiopostprocess_params["use_dilation"] = args.use_dilationpostprocess_params["score_mode"] = args.det_db_score_modeelif self.det_algorithm == "EAST":postprocess_params['name'] = 'EASTPostProcess'postprocess_params["score_thresh"] = args.det_east_score_threshpostprocess_params["cover_thresh"] = args.det_east_cover_threshpostprocess_params["nms_thresh"] = args.det_east_nms_threshelif self.det_algorithm == "SAST":pre_process_list[0] = {'DetResizeForTest': {'resize_long': args.det_limit_side_len}}postprocess_params['name'] = 'SASTPostProcess'postprocess_params["score_thresh"] = args.det_sast_score_threshpostprocess_params["nms_thresh"] = args.det_sast_nms_threshself.det_sast_polygon = args.det_sast_polygonif self.det_sast_polygon:postprocess_params["sample_pts_num"] = 6postprocess_params["expand_scale"] = 1.2postprocess_params["shrink_ratio_of_width"] = 0.2else:postprocess_params["sample_pts_num"] = 2postprocess_params["expand_scale"] = 1.0postprocess_params["shrink_ratio_of_width"] = 0.3else:logger.info("unknown det_algorithm:{}".format(self.det_algorithm))sys.exit(0)這是對三大文字檢測算法的選擇,即EAST算法,SAST算法和DB算法。
EAST算法:
SAST算法:百度自研文字檢測算法,一種基于分割的one-shot任意形狀文本檢測器
實際上就是EAST算法的擴展,一階段,輸出為multitask,各個分支相互校正。
它利用基于全卷積網絡(FCN)的上下文多任務學習框架來學習文本區域的各種幾何特征,從而構造文本區域的多邊形表示。考慮到文本的連續性特征,通過引入Context Attention Block 捕捉像素的長范圍相關性,一次來獲得更加可靠的分割結果。在后處理過程中,提出一個點到邊的對齊方法,來將像素聚類稱為文本實力,這樣就通過一次采樣圖片,把高級別的特征和低級別的特征結合在一起。此外,利用所提出的幾何性質可以更有效地提取任意形狀文本的多邊形表示。
作用:順時針對點進行排序,首先根據橫坐標找到最左邊和最右邊的的點,然后針對最左邊的數據根據他們的縱坐標進行排序,我們可以找到最上面和最下面的點
代碼位置:deploy->hubserving->ocr_det->module.py
def predict(self, images=[], paths=[]):if images != [] and isinstance(images, list) and paths == []:predicted_data = imageselif images == [] and isinstance(paths, list) and paths != []:predicted_data = self.read_images(paths)else:raise TypeError("The input data is inconsistent with expectations.")assert predicted_data != [], "There is not any image to be predicted. Please check the input data."all_results = []for img in predicted_data:if img is None:logger.info("error in loading image")all_results.append([])continuedt_boxes, elapse = self.text_detector(img)logger.info("Predict time : {}".format(elapse))rec_res_final = []for dno in range(len(dt_boxes)):rec_res_final.append({'text_region': dt_boxes[dno].astype(np.int).tolist()})all_results.append(rec_res_final)return all_results作用:獲取預測圖像中的文本框。
輸入參數:def predict(self, images=[], paths=[])
總結
以上是生活随笔為你收集整理的PaddleOCR 文字检测部分源码学习(1)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 有限空间作业存在的安全风险有哪些
- 下一篇: 三年级乘法计算机应用题目,三年级数学加减