import os import cv2 import requests import json import base64 import numpy as np from collections import OrderedDict from concurrent.futures import ThreadPoolExecutor # 进程池模块 import time def getByte(path): with open(path, 'rb') as f: img_byte = base64.b64encode(f.read()) img_str = img_byte.decode('ascii') return img_str def GetOcr(image): url="http://192.168.62.33:8090/api/tr-run/" data = {"img":str(image)} res = requests.post(url=url,data=data,timeout=100) res.encoding = res.apparent_encoding json_str = json.loads(res.content,object_pairs_hook=OrderedDict) raw= json_str["data"]["raw_out"] value="" for item in raw: value+=item[1] return value def CutImage(path,savepath,key): #a= getByte(path) image = cv2.imread(path) cropImg = image[440:565,415:1400] cv2.imencode('.jpg', cropImg)[1].tofile(savepath) name= GetOcr(getByte(savepath)) try: cv2.imencode('.jpg', image)[1].tofile("newimages\\"+str(name)+".jpg") except Exception as e: print('出现异常:', e) print(str(key)) def main(): pool = ThreadPoolExecutor(5) path = "images\\" fileList = os.listdir(path) if len(fileList)<=0: return t0 = time.time() for item in fileList: pool.submit(CutImage, path+item, "cutimages\\"+str(item),fileList.index(item)) pool.shutdown(wait=True) print("完成裁剪,耗时:") print (time.time() - t0) if __name__ == "__main__": main()
文章评论