from paddleocr import PaddleOCR # 初始化OCR引擎 ocr = PaddleOCR(use_angle_cls=True, lang="ch") # 使用中文模型 # 对图像进行OCR识别 img_path = './imgs/img_3.png' result = ocr.ocr(img_path, cls=True) # 按y坐标对文本块进行排序 sorted_result = sorted(result, key=lambda x: x[0][1]) # 假设每个元素是(bbox, text)的元组 # 初始化行列表 rows = [] current_row = [] last_y = None y_threshold = 10 # 行的y坐标差异阈值 for line in sorted_result: for node in line: bbox, text = node y1, y2 = bbox[1][1], bbox[3][1] if last_y is None or y1 - last_y > y_threshold: # 如果这是第一行或与前一行有足够的垂直距离,则开始新行 if current_row: rows.append(current_row) current_row = [(bbox, text)] last_y = y1 else: # 否则,将文本块添加到当前行 current_row.append((bbox, text)) # 不要忘记添加最后一行(如果有的话) if current_row: rows.append(current_row) # 输出结果 for idx, row in enumerate(rows, start=1): print(f"Row {idx-1}:") for bbox, text in row: print(f" Text: {text}, Box: {bbox}")
标签:rows,last,row,paddleocr,current,按行,text,识别,bbox From: https://www.cnblogs.com/chen1880/p/18312052