{ "metadata": { "language_info": { "codemirror_mode": { "name": "ipython", "version": 3 }, "file_extension": ".py", "mimetype": "text/x-python", "name": "python", "nbconvert_exporter": "python", "pygments_lexer": "ipython3", "version": "3.8.5" }, "orig_nbformat": 4, "kernelspec": { "name": "python3", "display_name": "Python 3.8.5 64-bit" }, "interpreter": { "hash": "31f2aee4e71d21fbe5cf8b01ff0e069b9275f58929596ceb00d14d90e3e16cd6" } }, "nbformat": 4, "nbformat_minor": 2, "cells": [ { "cell_type": "code", "execution_count": 25, "metadata": {}, "outputs": [], "source": [ "import onnxruntime\n", "import torch\n", "from PIL import Image\n", "import cv2\n", "import torchvision.transforms as transforms\n", "import numpy as np\n" ] }, { "cell_type": "code", "execution_count": 26, "metadata": {}, "outputs": [], "source": [ "handwrite_mean=(0.877, 0.877, 0.877)\n", "handwrite_std=(0.200, 0.200, 0.200)\n", "\n", "# 减少图像白边\n", "def cutImg(img):\n", " dst = 255- img\n", " gray = cv2.cvtColor(dst,cv2.COLOR_BGR2GRAY) \n", " ret, binary = cv2.threshold(gray,127,255,cv2.THRESH_BINARY) \n", " \n", " contours, hierarchy = cv2.findContours(binary,cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)\n", "\n", " minx = 65523\n", " miny = 65523\n", " maxx = 0\n", " maxy = 0\n", "\n", " for contour in contours:\n", " boundRect = cv2.boundingRect(contour)\n", " (x,y,w,h)=boundRect\n", " if xmaxx:\n", " maxx=x+w\n", " if y+h>maxy:\n", " maxy=y+h\n", " cutw=maxx-minx\n", " cuth=maxy-miny\n", " # w>h\n", " # if cutw>cuth:\n", " # cuty1=int((maxy+miny)/2-cutw/2)\n", " # cuty2=int((maxy+miny)/2+cutw/2)\n", " # img = img[cuty1:cuty2,minx:maxx]\n", " # if cutw