Have to move on to training and working on the app. have it crop and rotate and will let yolov7 train on that and I would imagine it will work fine. Don't have the time to try figure out the whiteout still. Signed-off-by: Ethan Wellenreiter <ewellenreiter@gmail.com>
722 lines
26 KiB
Plaintext
722 lines
26 KiB
Plaintext
{
|
|
"cells": [
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 1,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stderr",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"/usr/local/lib/python3.10/dist-packages/torchvision/datapoints/__init__.py:12: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning().\n",
|
|
" warnings.warn(_BETA_TRANSFORMS_WARNING)\n",
|
|
"/usr/local/lib/python3.10/dist-packages/torchvision/transforms/v2/__init__.py:54: UserWarning: The torchvision.datapoints and torchvision.transforms.v2 namespaces are still Beta. While we do not expect major breaking changes, some APIs may still change according to user feedback. Please submit any feedback you may have in this issue: https://github.com/pytorch/vision/issues/6753, and you can also check out https://github.com/pytorch/vision/issues/7319 to learn more about the APIs that we suspect might involve future changes. You can silence this warning by calling torchvision.disable_beta_transforms_warning().\n",
|
|
" warnings.warn(_BETA_TRANSFORMS_WARNING)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"import cv2\n",
|
|
"import myfunctions as mf\n",
|
|
"import numpy as np\n",
|
|
"import math\n",
|
|
"import scipy.stats as st"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 2,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"import os\n",
|
|
"import pathlib\n",
|
|
"import time\n",
|
|
"\n",
|
|
"def removeextensionandnumeric(filename):\n",
|
|
" suffix = pathlib.Path(filename).suffix\n",
|
|
" num = filename[:-len(suffix)]\n",
|
|
" numint = int(num)\n",
|
|
" return numint\n",
|
|
" \n",
|
|
"\n",
|
|
"def testondataset(pathtodataset, function):\n",
|
|
" imagefileextensions = [\".jpg\", \".png\"]\n",
|
|
" filenames = next(os.walk(pathtodataset), (None, None, []))[2]\n",
|
|
" \n",
|
|
" filenames.sort(key=removeextensionandnumeric)\n",
|
|
" # print(filenames)\n",
|
|
" outs = []\n",
|
|
" tdiffs = []\n",
|
|
" for filename in filenames:\n",
|
|
" suffix = pathlib.Path(filename).suffix\n",
|
|
" if (suffix not in imagefileextensions):\n",
|
|
" print(\"Not a valid image \"+filename)\n",
|
|
" continue\n",
|
|
" img = cv2.imread(pathtodataset+filename)\n",
|
|
" t1 = time.time()\n",
|
|
" outs.append(function(img))\n",
|
|
" tdiffs.append(time.time() - t1)\n",
|
|
" tdiffs = np.array(tdiffs)\n",
|
|
" print(\"average time: \" + str(np.mean(tdiffs))+\"(s)\")\n",
|
|
" return outs\n",
|
|
" "
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 3,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def showimgs(imgs):\n",
|
|
" if (isinstance(imgs, np.ndarray)):\n",
|
|
" if (imgs.shape[0] > imgs.shape[1]):\n",
|
|
" cv2.imshow(\"test\", mf.ResizeWithAspectRatio(imgs, height=1350))\n",
|
|
" else:\n",
|
|
" cv2.imshow(\"test\", mf.ResizeWithAspectRatio(imgs, width=1000))\n",
|
|
" else:\n",
|
|
" for i, out in enumerate(imgs):\n",
|
|
" if (out.shape[0] > out.shape[1]):\n",
|
|
" cv2.imshow(\"test\"+str(i), mf.ResizeWithAspectRatio(out, height=1350))\n",
|
|
" else:\n",
|
|
" cv2.imshow(\"test\"+str(i), mf.ResizeWithAspectRatio(out, width=1000))\n",
|
|
" cv2.waitKey(0)\n",
|
|
" cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 4,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def writeimgs(directorypath, imgs):\n",
|
|
" if (isinstance(imgs, np.ndarray)):\n",
|
|
" cv2.imwrite(directorypath+\"test.png\", imgs)\n",
|
|
" else:\n",
|
|
" for i, out in enumerate(imgs):\n",
|
|
" cv2.imwrite(directorypath+\"test\"+str(i)+\".png\", out)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 5,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"img = cv2.imread('/mnt/dataset/baseimages/12.jpg')\n",
|
|
"# img = cv2.imread('/mnt/code/autocropper/test_images/IMG_7605.jpg')\n",
|
|
"testall = False"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 6,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"## NEED TO FIX THE EARLIER PARTS SO THAT IT DOESN'T HAVE THOSE BLACK SECTIONS AFTER THE ROTATION\n",
|
|
"\n",
|
|
"\n",
|
|
"def whiteoutbackground(image):\n",
|
|
" ogshape = image.shape\n",
|
|
" shrunkdim=1000\n",
|
|
" if (image.shape[1] > image.shape[0]):\n",
|
|
" shrunkimg, scaler = mf.ResizeWithAspectRatio(image, width=shrunkdim, retscale=True)\n",
|
|
" else:\n",
|
|
" shrunkimg, scaler = mf.ResizeWithAspectRatio(image, height=shrunkdim, retscale=True)\n",
|
|
" \n",
|
|
" mainimage = shrunkimg\n",
|
|
" \n",
|
|
" sdim = int(min(mainimage.shape[0], mainimage.shape[1])/5)\n",
|
|
" srkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sdim, sdim))\n",
|
|
" skernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (sdim, sdim))\n",
|
|
" \n",
|
|
" \n",
|
|
" lab = cv2.cvtColor(mainimage, cv2.COLOR_BGR2LAB)\n",
|
|
" \n",
|
|
" imglist = []\n",
|
|
" # imglist.append(mainimage)\n",
|
|
" \n",
|
|
" labl = lab[:,:,0]\n",
|
|
" # imglist.append(labl)\n",
|
|
" # imglist.append(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))\n",
|
|
" laba = lab[:,:,1]\n",
|
|
" # imglist.append(laba)\n",
|
|
" labb = lab[:,:,2]\n",
|
|
" # imglist.append(labb)\n",
|
|
" \n",
|
|
" \n",
|
|
" # canny = cv2.Canny(labl, 0, 500)\n",
|
|
" threshl = cv2.threshold(labl, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
|
" # return threshl\n",
|
|
" \n",
|
|
" \n",
|
|
" dim = int(min(mainimage.shape[0], mainimage.shape[1])/100)\n",
|
|
" # dim = 2\n",
|
|
" # dim = dotsize\n",
|
|
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (dim, dim))\n",
|
|
" kernelell = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dim, dim))\n",
|
|
" \n",
|
|
" paddedl = mf.padWithColour(threshl, sdim*2, sdim*2, fill=0)\n",
|
|
" # return paddedl\n",
|
|
" \n",
|
|
" \n",
|
|
" # morphedl = 255-cv2.morphologyEx(255-threshl, cv2.MORPH_OPEN, kernel, iterations=3)\n",
|
|
" morphedl = paddedl\n",
|
|
" # morphedl = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
" morphed1l = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernelell, iterations=1)\n",
|
|
"\n",
|
|
" # return morphedl\n",
|
|
" \n",
|
|
" contours, heirarchy = cv2.findContours(morphed1l, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
|
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
|
" \n",
|
|
" \n",
|
|
" blank = np.full(labl.shape, 255, dtype=np.uint8)\n",
|
|
" mask1 = blank.copy()\n",
|
|
" mask1 = mf.padWithColour(mask1, sdim*2, sdim*2, fill=255)\n",
|
|
" mask1 = cv2.drawContours(mask1, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
|
" \n",
|
|
" \n",
|
|
" mask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, kernelell, iterations=2)\n",
|
|
" \n",
|
|
" \n",
|
|
" # mask1 = mask1[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" # return mask1\n",
|
|
" \n",
|
|
" # morphed2l = mf.padWithColour(morphedl, sdim*2, sdim*2, fill=255)\n",
|
|
" morphed2l = cv2.morphologyEx(morphedl, cv2.MORPH_OPEN, kernel, iterations=1)\n",
|
|
" # morphed2l = morphed2l[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" \n",
|
|
" # return morphed2l\n",
|
|
" # print(mask1.shape)\n",
|
|
" # print(morphed2l.shape)\n",
|
|
" morphed2l = cv2.bitwise_or(morphed2l, 255-mask1)\n",
|
|
" # return morphed2l\n",
|
|
" \n",
|
|
" morphed2l = morphed2l[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" temp_final = cv2.bitwise_or(threshl, 255-morphed2l)\n",
|
|
" return temp_final\n",
|
|
" \n",
|
|
" canny = cv2.Canny(morphed2l, 0, 500)\n",
|
|
" # return canny\n",
|
|
"\n",
|
|
" vminlength = mainimage.shape[0]//10\n",
|
|
" vmaxgap = mainimage.shape[0]//50\n",
|
|
" vlinesP = cv2.HoughLinesP(canny, 1, np.pi / 180, 10, None, vminlength, vmaxgap)\n",
|
|
" \n",
|
|
" hminlength = mainimage.shape[1]//15\n",
|
|
" hmaxgap = mainimage.shape[1]//40\n",
|
|
" hlinesP = cv2.HoughLinesP(canny, 1, np.pi / 180, 10, None, hminlength, hmaxgap)\n",
|
|
" # print(linesP)\n",
|
|
" \n",
|
|
" vmarginlines = mf.WithinXDegrees(vlinesP, 15)\n",
|
|
" hmarginlines = mf.WithinXDegrees(hlinesP, 15, baseangle=90)\n",
|
|
" \n",
|
|
" marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
|
" # marginlines = marginlines.astype(int)\n",
|
|
" # # print(marginlines)\n",
|
|
" # reshaped = np.reshape(marginlines, (-1,1, 2))\n",
|
|
" # # reshaped = cv2.convexHull(reshaped)\n",
|
|
" # # print(reshaped)\n",
|
|
" \n",
|
|
" \n",
|
|
" \n",
|
|
" colourdst = cv2.cvtColor(morphedl, cv2.COLOR_GRAY2BGR)\n",
|
|
" # out = cv2.drawContours(colourdst, [reshaped], -1, (0,255,0), thickness=3)\n",
|
|
" # return out\n",
|
|
" \n",
|
|
" \n",
|
|
" #### NEW IDEA: MERGE THE WHITEOUT BACKGROUND AND TEXT CLARIFICATION STEP BECAUSE DOING THE OTSU THRESHOLD SEEMS TO WORK PRETTY WELL AND IF I JUST WHITE OUT THE OUTER AREA (ACTUALLY WHITE)\n",
|
|
" # THEN I HAVE JUST THE TEXT\n",
|
|
" \n",
|
|
"\n",
|
|
" if marginlines is not None:\n",
|
|
" for l in marginlines:\n",
|
|
" cv2.line(colourdst, (int(l[0]), int(l[1])), (int(l[2]), int(l[3])), (0,0,255), 3, cv2.LINE_AA)\n",
|
|
" return colourdst\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
" ## IDEA:\n",
|
|
" # MASK OUT THE WORDS USING OUR MASKS MADE FROM THE STUFF BELOW. THEN WHEN CANNY IS DONE TO IT, IT SHOULDN'T HAVE A WHOLE BUNCH OF SHIT IN THE CENTER. STILL NEED TO FIGURE OUT HOW TO LINK THE HOUGH LINES AROUND THE RECEIPT\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
"\n",
|
|
" # morphedl = 255-cv2.morphologyEx(255-threshl, cv2.MORPH_OPEN, kernel, iterations=3)\n",
|
|
" morphedl = paddedl\n",
|
|
" morphedl = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
" morphedl = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernelell, iterations=1)\n",
|
|
"\n",
|
|
" # return morphedl\n",
|
|
" \n",
|
|
" contours, heirarchy = cv2.findContours(morphedl, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
|
" # print(contours[0].shape)\n",
|
|
" print(contours[0])\n",
|
|
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
|
" return canny\n",
|
|
" \n",
|
|
" \n",
|
|
" blank = np.full(labl.shape, 255, dtype=np.uint8)\n",
|
|
" mask1 = blank.copy()\n",
|
|
" mask1 = mf.padWithColour(mask1, sdim*2, sdim*2, fill=255)\n",
|
|
" mask1 = cv2.drawContours(mask1, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
|
" \n",
|
|
" \n",
|
|
" mask1 = mask1[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" \n",
|
|
" \n",
|
|
" # resizemask = cv2.resize(mask1, (ogshape[1], ogshape[0]))\n",
|
|
" # return resizemask\n",
|
|
" maskc = cv2.cvtColor(mask1, cv2.COLOR_GRAY2BGR)\n",
|
|
" # print(maskc.shape)\n",
|
|
" # print(image.shape)\n",
|
|
" whitedbackground = cv2.bitwise_or(mainimage, maskc)\n",
|
|
" # return whitedbackground\n",
|
|
" \n",
|
|
" \n",
|
|
" lab2 = cv2.cvtColor(whitedbackground, cv2.COLOR_BGR2LAB)\n",
|
|
" \n",
|
|
" lab2l = lab2[:,:,0]\n",
|
|
" \n",
|
|
" \n",
|
|
" otsu2 = cv2.threshold(lab2l, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
|
" \n",
|
|
" expandedmask1 = cv2.morphologyEx(mask1, cv2.MORPH_DILATE, kernel, iterations=1)\n",
|
|
" expandedmask1 = cv2.morphologyEx(expandedmask1, cv2.MORPH_DILATE, kernelell, iterations=1)\n",
|
|
" # return expandedmask1\n",
|
|
" \n",
|
|
" maskmerge = cv2.bitwise_and(otsu2, 255-expandedmask1)\n",
|
|
" return mask1\n",
|
|
" return maskmerge\n",
|
|
" \n",
|
|
" # return otsu2\n",
|
|
" \n",
|
|
" mpad = mf.padWithColour(maskmerge, sdim*2, sdim*2, fill=0)\n",
|
|
" return mpad\n",
|
|
" \n",
|
|
" #MORPHOLOGIES \n",
|
|
" morphed2 = cv2.morphologyEx(mpad, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
" morphed2 = cv2.morphologyEx(morphed2, cv2.MORPH_ERODE, kernelell, iterations=1)\n",
|
|
" return morphed2\n",
|
|
" \n",
|
|
" contours, heirarchy = cv2.findContours(morphed2, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
|
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
|
" \n",
|
|
" \n",
|
|
" mask2 = blank.copy()\n",
|
|
" mask2 = mf.padWithColour(mask2, sdim*2, sdim*2, fill=255)\n",
|
|
" mask2 = cv2.drawContours(mask2, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
|
" \n",
|
|
" \n",
|
|
" mask2 = mask2[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" \n",
|
|
" return mask2\n",
|
|
" \n",
|
|
" test = cv2.inpaint(whitedbackground, resizemask, 3, cv2.INPAINT_TELEA)\n",
|
|
" \n",
|
|
" return test\n",
|
|
" \n",
|
|
" contours, heirarchy = cv2.findContours(255-labl, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)\n",
|
|
" \n",
|
|
" imgout = cv2.drawContours(mainimage, contours, -1, (0,255,0), thickness=3)\n",
|
|
" return imgout\n",
|
|
" \n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 7,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def textleaver(image):\n",
|
|
" ogshape = image.shape\n",
|
|
" shrunkdim=1000\n",
|
|
" if (image.shape[1] > image.shape[0]):\n",
|
|
" shrunkimg, scaler = mf.ResizeWithAspectRatio(image, width=shrunkdim, retscale=True)\n",
|
|
" else:\n",
|
|
" shrunkimg, scaler = mf.ResizeWithAspectRatio(image, height=shrunkdim, retscale=True)\n",
|
|
" \n",
|
|
" mainimage = shrunkimg\n",
|
|
" \n",
|
|
" sdim = int(min(mainimage.shape[0], mainimage.shape[1])/5)\n",
|
|
" srkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (sdim, sdim))\n",
|
|
" skernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (sdim, sdim))\n",
|
|
" \n",
|
|
" oglab = cv2.cvtColor(image, cv2.COLOR_BGR2LAB)\n",
|
|
" lab = cv2.cvtColor(mainimage, cv2.COLOR_BGR2LAB)\n",
|
|
" \n",
|
|
" imglist = []\n",
|
|
" # imglist.append(mainimage)\n",
|
|
" \n",
|
|
" labl = lab[:,:,0]\n",
|
|
" oglabl = oglab[:,:,0]\n",
|
|
" # # imglist.append(labl)\n",
|
|
" # # imglist.append(cv2.cvtColor(image, cv2.COLOR_BGR2GRAY))\n",
|
|
" # laba = lab[:,:,1]\n",
|
|
" # # imglist.append(laba)\n",
|
|
" # labb = lab[:,:,2]\n",
|
|
" # # imglist.append(labb)\n",
|
|
" \n",
|
|
" divisor = 1.5\n",
|
|
" window = int(min(labl.shape)/divisor)\n",
|
|
" window = window if window%2 == 1 else window + 1\n",
|
|
" # canny = cv2.Canny(labl, 0, 500)\n",
|
|
" ethreshl = cv2.threshold(labl, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
|
" threshl = cv2.threshold(labl, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
|
" # threshl = cv2.adaptiveThreshold(labl, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 35)\n",
|
|
" \n",
|
|
" \n",
|
|
" ogwindow = int(min(oglabl.shape)/divisor)\n",
|
|
" ogwindow = window if window%2 == 1 else window + 1\n",
|
|
" print(ogwindow)\n",
|
|
" ogthreshl = cv2.threshold(oglabl, 0, 255, cv2.THRESH_TRIANGLE)[1]\n",
|
|
" return ogthreshl\n",
|
|
" # ogthreshl = cv2.adaptiveThreshold(oglabl, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, ogwindow, 35)\n",
|
|
" # return threshl\n",
|
|
" \n",
|
|
" colourthresh = cv2.cvtColor(threshl, cv2.COLOR_GRAY2BGR)\n",
|
|
" \n",
|
|
" dim = int(min(mainimage.shape[0], mainimage.shape[1])/100)\n",
|
|
" # dim = 2\n",
|
|
" # dim = dotsize\n",
|
|
" dim = max(3,dim)\n",
|
|
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (dim, dim))\n",
|
|
" kernelell = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dim, dim))\n",
|
|
" \n",
|
|
" # paddedl = mf.padWithColour(threshl, sdim*2, sdim*2, fill=0)\n",
|
|
" paddedl = threshl\n",
|
|
" # return paddedl\n",
|
|
" \n",
|
|
" \n",
|
|
" # morphedl = 255-cv2.morphologyEx(255-threshl, cv2.MORPH_OPEN, kernel, iterations=3)\n",
|
|
" morphedl = paddedl\n",
|
|
" morphed1l = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
" # morphed1l = cv2.morphologyEx(morphed1l, cv2.MORPH_OPEN, kernel, iterations=1)\n",
|
|
" # morphed1l = cv2.morphologyEx(morphed1l, cv2.MORPH_OPEN, kernel, iterations=1)\n",
|
|
" # morphed1l = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernelell, iterations=2)\n",
|
|
" \n",
|
|
" emorphed1l = cv2.morphologyEx(ethreshl, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
"\n",
|
|
" # return morphedl\n",
|
|
" \n",
|
|
" contours, heirarchy = cv2.findContours(morphed1l, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
|
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
|
" \n",
|
|
" # temp = cv2.drawContours(colourthresh, [biggestcontour], -1, (0,255,0), thickness=1)\n",
|
|
" # return temp\n",
|
|
" \n",
|
|
" \n",
|
|
" blank = np.full(labl.shape, 255, dtype=np.uint8)\n",
|
|
" mask1 = blank.copy()\n",
|
|
" # mask1 = mf.padWithColour(mask1, sdim*2, sdim*2, fill=255)\n",
|
|
" mask1 = cv2.drawContours(mask1, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
|
" ## need to change the erosion so that if the paper goes to the edge, it doesn't get eroded in (because that means the paper is right to the edge and writing may be close)\n",
|
|
" \n",
|
|
" contours, heirarchy = cv2.findContours(morphed1l, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
|
" biggestcontour = max(contours, key=cv2.contourArea)\n",
|
|
" \n",
|
|
" emask1 = blank.copy()\n",
|
|
" emask1 = cv2.drawContours(emask1, [biggestcontour], -1, 0, thickness=cv2.FILLED)\n",
|
|
" \n",
|
|
" mask1 = 255-cv2.morphologyEx(255-mask1, cv2.MORPH_ERODE, kernel, iterations=2)\n",
|
|
" \n",
|
|
" emask1 = 255-cv2.morphologyEx(255-emask1, cv2.MORPH_ERODE, kernel, iterations=2)\n",
|
|
" \n",
|
|
" \n",
|
|
" # mask1 = mask1[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" # return mask1\n",
|
|
" \n",
|
|
" # morphed2l = mf.padWithColour(morphedl, sdim*2, sdim*2, fill=255)\n",
|
|
" morphed2l = cv2.morphologyEx(morphedl, cv2.MORPH_OPEN, kernel, iterations=1)\n",
|
|
" morphed2l = cv2.morphologyEx(morphedl, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
" # morphed2l = morphed2l[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" \n",
|
|
" # return morphed2l\n",
|
|
" # print(mask1.shape)\n",
|
|
" # print(morphed2l.shape)\n",
|
|
" morphed2l = cv2.bitwise_or(morphed2l, 255-mask1)\n",
|
|
" # return morphed2l\n",
|
|
"\n",
|
|
" # paddedthreshl = mf.padWithColour(morphed2l, sdim*2, sdim*2, fill=255)\n",
|
|
" # temp = cv2.drawContours(colourthresh, [biggestcontour], -1, (0,255,0), thickness=1)\n",
|
|
" # return temp\n",
|
|
"\n",
|
|
"\n",
|
|
" morphed2l = cv2.morphologyEx(morphed2l, cv2.MORPH_ERODE, kernel, iterations=1)\n",
|
|
" morphed2l = cv2.morphologyEx(morphed2l, cv2.MORPH_ERODE, kernelell, iterations=1)\n",
|
|
" # return morphed2l\n",
|
|
" # morphed2l = cv2.bitwise_or(morphed2l, 255-emask1)\n",
|
|
" \n",
|
|
" # morphed2l = morphed2l[(sdim*2):-(sdim*2), (sdim*2):-(sdim*2)]\n",
|
|
" \n",
|
|
" resizedmask = cv2.resize(255-morphed2l, (ogshape[1], ogshape[0]))\n",
|
|
" temp_final = cv2.bitwise_or(ogthreshl, resizedmask)\n",
|
|
" \n",
|
|
" dim=3\n",
|
|
" kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dim, dim))\n",
|
|
" temp_final = cv2.morphologyEx(temp_final, cv2.MORPH_OPEN, kernel)\n",
|
|
" temp_final = cv2.morphologyEx(temp_final, cv2.MORPH_OPEN, kernel)\n",
|
|
" # temp_final = cv2.morphologyEx(temp_final, cv2.MORPH_CLOSE, kernel)\n",
|
|
" # temp_final = cv2.morphologyEx(temp_final, cv2.MORPH_OPEN, kernel)\n",
|
|
" return temp_final"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 8,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def cropclarifying(image):\n",
|
|
" # whitedbackground = whiteoutbackground(image)\n",
|
|
" # return whitedbackground\n",
|
|
"\n",
|
|
" # textrefined = mf.textClarifying(whitedbackground)\n",
|
|
" textrefined = textleaver(image)\n",
|
|
" return textrefined\n",
|
|
" #maybe now is when I put in the line removing function\n",
|
|
"\n",
|
|
" lineout = mf.removeLinesFromText(textrefined)\n",
|
|
"\n",
|
|
" return lineout\n",
|
|
" # implement a function that's called refine text"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 9,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"def houghlineprocessing(image):\n",
|
|
" croppedanddeskewed, angle = mf.houghlinedeskewandcrop(image)\n",
|
|
" # return croppedanddeskewed\n",
|
|
" \n",
|
|
" \n",
|
|
" # postprocessed = cropclarifying(croppedanddeskewed)\n",
|
|
" postprocessed = croppedanddeskewed\n",
|
|
" # return postprocessed\n",
|
|
" # postprocessed = mf.croptoblack(postprocessed)\n",
|
|
" \n",
|
|
" # postprocessed = cv2.cvtColor(postprocessed, cv2.COLOR_GRAY2BGR)\n",
|
|
" # return postprocessed\n",
|
|
" \n",
|
|
" # final = mf.externaldeskew(postprocessed, fill=(255,255,255))\n",
|
|
" # rotangle = mf.receipttextdeskew(postprocessed, fill=(255,255,255), returnangle=True)\n",
|
|
" final = postprocessed\n",
|
|
" \n",
|
|
" \n",
|
|
" # final = mf.croptoblack(final)\n",
|
|
" \n",
|
|
" # cv2.imshow(\"postprocessed\", mf.ResizeWithAspectRatio(postprocessed, 1000))\n",
|
|
" # cv2.imshow(\"final\", mf.ResizeWithAspectRatio(final, 1000))\n",
|
|
" # cv2.waitKey(0)\n",
|
|
" # cv2.destroyAllWindows()\n",
|
|
" \n",
|
|
" return final"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 10,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# print(img.shape)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 11,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0.0\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"# prepped, scaler, hp, vp = mf.squareandthenresize(img, fill=255, width=1000, returnscalerinfo=True)\n",
|
|
"outs = houghlineprocessing(img)\n",
|
|
"# outs = prepimageforhoughline(img, returnrect=True)\n",
|
|
"# print(img.shape)\n",
|
|
"# outs = houghlinedeskewandcrop(img)\n",
|
|
"# outs = outs[0]\n",
|
|
"# print(croprect)\n",
|
|
"#need to fix premorphCrop. it removes too much"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 12,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# shrunk, scaler, hp, vp = mf.squareandthenresize(img, fill=255, width=1000, returnscalerinfo=True)\n",
|
|
"# shrunk1, croprect = mf.premorphCrop(shrunk)\n",
|
|
"# print(croprect)\n",
|
|
"# print(int(30*4.032 - 0))\n",
|
|
"# # temp = img[100:, :, :]\n",
|
|
"# temp = shrunk[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2], :]\n"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 13,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# cv2.imshow(\"temp\", mf.ResizeWithAspectRatio(out, height=1000))\n",
|
|
"# # cv2.imshow(\"shrunk1\", mf.ResizeWithAspectRatio(shrunk1, height=1000))\n",
|
|
"# cv2.waitKey(0)\n",
|
|
"# cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 14,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"testall = True"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 15,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if not testall:\n",
|
|
" showimgs(outs)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 16,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# # for out in outs:\n",
|
|
"# # if (out.shape[0] > out.shape[1]):\n",
|
|
"# # cv2.imshow(\"test1\", mf.ResizeWithAspectRatio(out, height=1000))\n",
|
|
"# # else:\\\n",
|
|
"# # cv2.imshow(\"test1\", mf.ResizeWithAspectRatio(out, width=1000))\n",
|
|
"# # key = cv2.waitKey(0)\n",
|
|
"# # cv2.destroyAllWindows()\n",
|
|
"# # if (key == 107):\n",
|
|
"# # break\n",
|
|
"# if (isinstance(outs, np.ndarray)):\n",
|
|
"# if (outs.shape[0] > outs.shape[1]):\n",
|
|
"# cv2.imshow(\"test\", mf.ResizeWithAspectRatio(outs, height=1350))\n",
|
|
"# else:\n",
|
|
"# cv2.imshow(\"test\", mf.ResizeWithAspectRatio(outs, width=1000))\n",
|
|
"# else:\n",
|
|
"# for i, out in enumerate(outs):\n",
|
|
"# if (out.shape[0] > out.shape[1]):\n",
|
|
"# cv2.imshow(\"test\"+str(i), mf.ResizeWithAspectRatio(out, height=1350))\n",
|
|
"# else:\n",
|
|
"# cv2.imshow(\"test\"+str(i), mf.ResizeWithAspectRatio(out, width=1000))\n",
|
|
"# cv2.waitKey(0)\n",
|
|
"# cv2.destroyAllWindows()"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 17,
|
|
"metadata": {},
|
|
"outputs": [
|
|
{
|
|
"name": "stdout",
|
|
"output_type": "stream",
|
|
"text": [
|
|
"0.9740282517223996\n",
|
|
"-2.0053522829578814\n",
|
|
"-0.9740282517223996\n",
|
|
"0.0\n",
|
|
"0.9740282517223996\n",
|
|
"-0.9740282517223996\n",
|
|
"-0.011669615052326776\n",
|
|
"2.0053522829578814\n",
|
|
"0.0\n",
|
|
"0.0\n",
|
|
"0.0\n",
|
|
"-2.979380534680281\n",
|
|
"0.0\n",
|
|
"0.0\n",
|
|
"-2.0053522829578814\n",
|
|
"-11.000789666511807\n",
|
|
"average time: 0.19967518746852875(s)\n"
|
|
]
|
|
}
|
|
],
|
|
"source": [
|
|
"if testall:\n",
|
|
" results = testondataset(\"/mnt/dataset/baseimages/\", houghlineprocessing)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 18,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# if testall:\n",
|
|
"# showimgs(results)"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 19,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"# print(results[0])"
|
|
]
|
|
},
|
|
{
|
|
"cell_type": "code",
|
|
"execution_count": 20,
|
|
"metadata": {},
|
|
"outputs": [],
|
|
"source": [
|
|
"if testall:\n",
|
|
" writeimgs(\"/mnt/code/autocropper/result_images/\", results)"
|
|
]
|
|
}
|
|
],
|
|
"metadata": {
|
|
"kernelspec": {
|
|
"display_name": "Python 3",
|
|
"language": "python",
|
|
"name": "python3"
|
|
},
|
|
"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.10.12"
|
|
}
|
|
},
|
|
"nbformat": 4,
|
|
"nbformat_minor": 2
|
|
}
|