dev_image.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import cv2
  2. import math
  3. from copy import deepcopy
  4. import numpy as np
  5. import os
  6. from TextModel import text_model
  7. import pandas as pd
  8. from image_tools import *
  9. import requests
  10. import json
  11. import time
  12. from tools import get_text
  13. def run_cut(path,draw=False,tabel=True):
  14. """
  15. 切割图片,生成小图片放在文件夹里
  16. :param path:
  17. :param draw:
  18. :param tabel:
  19. :return:
  20. """
  21. if os.path.exists(r'D:\PaperCut\result\image'):
  22. os.system(r'rd /s/q D:\PaperCut\result\image')
  23. if os.path.exists(r'D:\PaperCut\result\text_img'):
  24. os.system(r'rd /s/q D:\PaperCut\result\text_img')
  25. if not os.path.exists(r'D:\PaperCut\result\image'):
  26. os.makedirs(r'D:\PaperCut\result\image')
  27. if not os.path.exists(r'D:\PaperCut\result\text_img'):
  28. os.makedirs(r'D:\PaperCut\result\text_img')
  29. t0 = time.time()
  30. text_bbx = text_model(path)
  31. img = cv2.imread(path)
  32. t1 = time.time()
  33. print('text时间',t1-t0)
  34. gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
  35. cv2.imwrite('gray.png', gray)
  36. ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY)
  37. if draw:
  38. cv2.imwrite('ret.png', ret)
  39. cv2.imwrite('binary.png', binary)
  40. contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_L1)
  41. t2= time.time()
  42. print('轮廓时间',t2-t1)
  43. text_bbx = get_range(text_bbx) # 坐标转化
  44. i_box = is_text(text_bbx, contours) # 得到图片坐标和未知坐标
  45. i_box, text_bbx = processed(np.array(text_bbx), i_box = np.array(i_box))
  46. t3 = time.time()
  47. print('预处理',t3-t2)
  48. for pixel in i_box:
  49. x_min, y_min, x_max, y_max = pixel
  50. if (x_max - x_min) * (y_max - y_min) > 30:
  51. pts = np.array([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max]], np.int32)
  52. # 顶点个数:4,矩阵变成4*1*2维
  53. # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵
  54. # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的
  55. if draw:
  56. pts = pts.reshape((-1, 1, 2))
  57. cv2.polylines(img, [pts], True, (0, 0, 255)) # 画文字图
  58. cv2.imwrite('./result/image/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), img[y_min:y_max, x_min:x_max])
  59. if tabel:# and (x_max - x_min) * (y_max - y_min) * 8 > img.shape[0] * img.shape[1]:
  60. files = {'file': open(r'./result/image/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max),
  61. 'rb')} # 此处是重点!我们操作文件上传的时候,把目标文件以open打开,然后存储到变量file里面存到一个字典里面
  62. upload_data = {"parentId": "", "fileCategory": "personal",
  63. "fileName": '%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max),
  64. "uoType": 1}
  65. tabel_img = np.array(
  66. json.loads(requests.post('http://192.168.1.192:25255/img2maple', upload_data, files=files).text))
  67. if np.sum(tabel_img):
  68. cv2.imwrite('./result/image/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), tabel_img)
  69. for pixel in text_bbx:
  70. x_min, y_min, x_max, y_max = pixel
  71. pts = np.array([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max]], np.int32)
  72. # 顶点个数:4,矩阵变成4*1*2维
  73. # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵
  74. # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的
  75. if draw:
  76. pts = pts.reshape((-1, 1, 2))
  77. cv2.polylines(img, [pts], True, (0, 255, 0)) # 画插图
  78. cv2.imwrite('./result/text_img/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), img[y_min:y_max, x_min:x_max])
  79. get_text()
  80. t4=time.time()
  81. print('画图+ocr',t4-t3)
  82. print('总时间',t4-t0)
  83. # contours 轮廓所有的点
  84. # cv2.drawContours(img, contours, -1, (0, 0, 255), 3)
  85. if __name__ == '__main__':
  86. cv2.imshow("img", img)
  87. cv2.waitKey(0)
  88. cv2.imwrite('bbb.png', img)
  89. if __name__ == '__main__':
  90. start = time.time()
  91. #19 23 29 30
  92. #
  93. #36?33
  94. for i in range(10):
  95. run_cut('./image_dir/1.jpg',draw=True,tabel=False)