import cv2 import math from copy import deepcopy import numpy as np import os from TextModel import text_model import pandas as pd from image_tools import * import requests import json import time from tools import get_text def run_cut(path,draw=False,tabel=True): """ 切割图片,生成小图片放在文件夹里 :param path: :param draw: :param tabel: :return: """ if os.path.exists(r'D:\PaperCut\result\image'): os.system(r'rd /s/q D:\PaperCut\result\image') if os.path.exists(r'D:\PaperCut\result\text_img'): os.system(r'rd /s/q D:\PaperCut\result\text_img') if not os.path.exists(r'D:\PaperCut\result\image'): os.makedirs(r'D:\PaperCut\result\image') if not os.path.exists(r'D:\PaperCut\result\text_img'): os.makedirs(r'D:\PaperCut\result\text_img') t0 = time.time() text_bbx = text_model(path) img = cv2.imread(path) t1 = time.time() print('text时间',t1-t0) gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) cv2.imwrite('gray.png', gray) ret, binary = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY) if draw: cv2.imwrite('ret.png', ret) cv2.imwrite('binary.png', binary) contours, hierarchy = cv2.findContours(binary, cv2.RETR_LIST, cv2.CHAIN_APPROX_TC89_L1) t2= time.time() print('轮廓时间',t2-t1) text_bbx = get_range(text_bbx) # 坐标转化 i_box = is_text(text_bbx, contours) # 得到图片坐标和未知坐标 i_box, text_bbx = processed(np.array(text_bbx), i_box = np.array(i_box)) t3 = time.time() print('预处理',t3-t2) for pixel in i_box: x_min, y_min, x_max, y_max = pixel if (x_max - x_min) * (y_max - y_min) > 30: pts = np.array([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max]], np.int32) # 顶点个数:4,矩阵变成4*1*2维 # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵 # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的 if draw: pts = pts.reshape((-1, 1, 2)) cv2.polylines(img, [pts], True, (0, 0, 255)) # 画文字图 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]) if tabel:# and (x_max - x_min) * (y_max - y_min) * 8 > img.shape[0] * img.shape[1]: files = {'file': open(r'./result/image/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), 'rb')} # 此处是重点!我们操作文件上传的时候,把目标文件以open打开,然后存储到变量file里面存到一个字典里面 upload_data = {"parentId": "", "fileCategory": "personal", "fileName": '%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), "uoType": 1} tabel_img = np.array( json.loads(requests.post('http://192.168.1.192:25255/img2maple', upload_data, files=files).text)) if np.sum(tabel_img): cv2.imwrite('./result/image/%d-%d-%d-%d.png' % (y_min, y_max, x_min, x_max), tabel_img) for pixel in text_bbx: x_min, y_min, x_max, y_max = pixel pts = np.array([[x_min, y_min], [x_max, y_min], [x_max, y_max], [x_min, y_max]], np.int32) # 顶点个数:4,矩阵变成4*1*2维 # OpenCV中需要将多边形的顶点坐标变成顶点数×1×2维的矩阵 # 这里 reshape 的第一个参数为-1, 表示“任意”,意思是这一维的值是根据后面的维度的计算出来的 if draw: pts = pts.reshape((-1, 1, 2)) cv2.polylines(img, [pts], True, (0, 255, 0)) # 画插图 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]) get_text() t4=time.time() print('画图+ocr',t4-t3) print('总时间',t4-t0) # contours 轮廓所有的点 # cv2.drawContours(img, contours, -1, (0, 0, 255), 3) if __name__ == '__main__': cv2.imshow("img", img) cv2.waitKey(0) cv2.imwrite('bbb.png', img) if __name__ == '__main__': start = time.time() #19 23 29 30 # #36?33 for i in range(10): run_cut('./image_dir/1.jpg',draw=True,tabel=False)