当前位置:   article > 正文

OCR图片矫正、表格检测及裁剪综合实践

OCR图片矫正、表格检测及裁剪综合实践

问题描述

实际工程中,我们经常需要对图片进行预处理,比如:

1、图片是倾斜的

2、图片背景需要处理掉

3、图片的公章需要剔除

4、图片过暗,过亮

5、图片表格检测

6、图片表格版面分析

。。。。。。等等各种情况。

结果展示

本文以表格图片为例,介绍如何进行矫正、表格检测及裁剪保存图片。

原始图片

矫正之后

表格检测

裁剪之后

代码详解

图片矫正

通过多次旋转计算最佳旋转角度并应用旋转矩阵矫正图片

  1. #coding=utf-8
  2. import cv2
  3. import numpy as np
  4. def rotate_image(image, angle):
  5. (h, w) = image.shape[: 2]
  6. center = (w // 2, h // 2)
  7. M = cv2.getRotationMatrix2D(center, angle, 1.0)
  8. corrected = cv2.warpAffine(image, M, (w, h), flags = cv2.INTER_CUBIC, \
  9. borderMode = cv2.BORDER_REPLICATE)
  10. return corrected
  11. def determine_score(arr):
  12. histogram = np.sum(arr, axis = 2, dtype = float)
  13. score = np.sum((histogram[..., 1 :] - histogram[..., : -1]) ** 2, \
  14. axis = 1, dtype = float)
  15. return score
  16. def correct_skew(image, delta = 0.05, limit = 10):
  17. thresh = cv2.threshold(image, 0, 255, cv2.THRESH_BINARY_INV + \
  18. cv2.THRESH_OTSU)[1]
  19. angles = np.arange(-limit, limit + delta, delta)
  20. img_stack = np.stack([rotate_image(thresh, angle) for angle \
  21. in angles], axis = 0)
  22. scores = determine_score(img_stack)
  23. best_angle = angles[np.argmax(scores)]
  24. corrected = rotate_image(image, best_angle)
  25. return best_angle, corrected
  26. if __name__ == "__main__":
  27. batch_folder = r'D:\temp\pics'
  28. out_folder = r'D:\temp\picsout/'
  29. for root, dirs, files in os.walk(batch_folder):
  30. for file in files:
  31. file_path = os.path.join(root, file)
  32. file_path = file_path.replace('\\', '/')
  33. img = cv2.imread(file_path, 0)
  34. angle, corrected = correct_skew(img)
  35. print(angle,file_path)
  36. cv2.imwrite(out_folder + file_path.split('/')[-1], corrected)

表格识别

通过微软的table-transformer-detection进行表格,该模型可在Hugging Face 官网下载。

图片裁剪

通过PIL里的Image的crop方法对指定的let_top,right_bottom进行裁剪。

相关代码见下:

  1. from PIL import Image
  2. import matplotlib.pyplot as plt
  3. file_path = r'D:\temp\pics\efb.jpg'
  4. image = Image.open(file_path).convert("RGB")
  5. width, height = image.size
  6. image.resize((int(width * 0.5), int(height * 0.5)))
  7. from transformers import DetrFeatureExtractor
  8. feature_extractor = DetrFeatureExtractor()
  9. encoding = feature_extractor(image, return_tensors="pt")
  10. encoding.keys()
  11. from transformers import TableTransformerForObjectDetection
  12. model = TableTransformerForObjectDetection.from_pretrained(r"D:\Modles\table-transformer-detection/")
  13. import torch
  14. with torch.no_grad():
  15. outputs = model(**encoding)
  16. COLORS = [[0.000, 0.447, 0.741], [0.850, 0.325, 0.098], [0.929, 0.694, 0.125],
  17. [0.494, 0.184, 0.556], [0.466, 0.674, 0.188], [0.301, 0.745, 0.933]]
  18. def plot_results(pil_img, scores, labels, boxes):
  19. plt.figure(figsize=(16, 10))
  20. plt.imshow(pil_img)
  21. ax = plt.gca()
  22. colors = COLORS * 100
  23. for score, label, (xmin, ymin, xmax, ymax), c in zip(scores.tolist(), labels.tolist(), boxes.tolist(), colors):
  24. ax.add_patch(plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin,
  25. fill=False, color=c, linewidth=3))
  26. text = f'{model.config.id2label[label]}: {score:0.2f}'
  27. ax.text(xmin, ymin, text, fontsize=15,
  28. bbox=dict(facecolor='yellow', alpha=0.5))
  29. plt.axis('off')
  30. plt.show()
  31. if __name__ == "__main__":
  32. width, height = image.size
  33. results = feature_extractor.post_process_object_detection(outputs, threshold=0.2, target_sizes=[(height, width)])[0]
  34. plot_results(image, results['scores'], results['labels'], results['boxes'])
  35. print(results['scores'])
  36. print(results['labels'])
  37. print(results['boxes'])
  38. print(results['boxes'][0][0],type((results['boxes'][0][0])))
  39. x0=int(results['boxes'][0][0].item())-50
  40. y0=int(results['boxes'][0][1].item())-50
  41. x1=int(results['boxes'][0][2].item())+50
  42. y1=int(results['boxes'][0][3].item())+50
  43. img2 = image.crop((x0,y0,x1,y1))
  44. img2.save(r"D:\\efb.jpg")

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/小桥流水78/article/detail/931657
推荐阅读
相关标签
  

闽ICP备14008679号