Compare commits
4 Commits
main
...
autocroppe
| Author | SHA1 | Date | |
|---|---|---|---|
| a564908fbc | |||
| 3aee49f236 | |||
| 8c5671d54e | |||
| 613e7780b8 |
@ -2,20 +2,9 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 1,
|
||||
"execution_count": 514,
|
||||
"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"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"import cv2\n",
|
||||
"import myfunctions as mf\n",
|
||||
@ -26,7 +15,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 2,
|
||||
"execution_count": 515,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -66,7 +55,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 3,
|
||||
"execution_count": 516,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -88,7 +77,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"execution_count": 517,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -102,18 +91,18 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 5,
|
||||
"execution_count": 518,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"img = cv2.imread('/mnt/dataset/baseimages/12.jpg')\n",
|
||||
"img = cv2.imread('/mnt/dataset/baseimages/21.jpg')\n",
|
||||
"# img = cv2.imread('/mnt/code/autocropper/test_images/IMG_7605.jpg')\n",
|
||||
"testall = False"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 6,
|
||||
"execution_count": 519,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -333,7 +322,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 7,
|
||||
"execution_count": 520,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -472,7 +461,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 8,
|
||||
"execution_count": 521,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -493,17 +482,534 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 9,
|
||||
"execution_count": null,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": []
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 522,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlinepcrop(baseimage, preppedimage, scalingmultiplier, returnrect=False):\n",
|
||||
" hminlength = int(preppedimage.shape[1]/30)\n",
|
||||
" hmaxgap = int(preppedimage.shape[1]/35)\n",
|
||||
" \n",
|
||||
" vminlength = int(preppedimage.shape[0]/25)\n",
|
||||
" vmaxgap = int(preppedimage.shape[0]/35)\n",
|
||||
" # print(dim)\n",
|
||||
" \n",
|
||||
" vrotatedlines = cv2.HoughLinesP(preppedimage, 1, np.pi / 180, 20, None, vminlength, vmaxgap)\n",
|
||||
" hrotatedlines = cv2.HoughLinesP(preppedimage, 1, np.pi / 180, 20, None, hminlength, hmaxgap)\n",
|
||||
" # if rotatedlines is None:\n",
|
||||
" # print(\"hi\")\n",
|
||||
" # return baseimage\n",
|
||||
" vmarginlines = mf.WithinXDegrees(vrotatedlines, 7)\n",
|
||||
" hmarginlines = mf.WithinXDegrees(hrotatedlines, 7, baseangle=90)\n",
|
||||
" if (len(vmarginlines) != 0):\n",
|
||||
" # print(\"hi\")\n",
|
||||
" vrect = mf.lineBoundingRect(vmarginlines,asRect=False, returnint=True)\n",
|
||||
" hmarginlines = mf.lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)\n",
|
||||
" # print(vmarginlines)\n",
|
||||
" # print(hmarginlines)\n",
|
||||
" marginlines = np.append(vmarginlines, hmarginlines, axis=0)\n",
|
||||
" \n",
|
||||
" colourdst = cv2.cvtColor(preppedimage, cv2.COLOR_GRAY2BGR)\n",
|
||||
" # print(len(marginlines))\n",
|
||||
" # up = False\n",
|
||||
" # if len(marginlines) != 0:\n",
|
||||
" # # print(\"hi\")\n",
|
||||
" # up = True\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",
|
||||
" # # if up:\n",
|
||||
" # # print(\"good\")\n",
|
||||
" # # else:\n",
|
||||
" # # print(\"bad\")\n",
|
||||
" \n",
|
||||
" # # print(\"bye\")\n",
|
||||
" # return colourdst\n",
|
||||
" rect = mf.lineBoundingRect(marginlines,asRect=False, returnint=True)\n",
|
||||
" scaledrect = (int(rect[0]*scalingmultiplier), int(rect[1]*scalingmultiplier), int(rect[2]*scalingmultiplier), int(rect[3]*scalingmultiplier))\n",
|
||||
" croppedbaseimage = baseimage[scaledrect[1]:scaledrect[3], scaledrect[0]:scaledrect[2], :]\n",
|
||||
" if returnrect:\n",
|
||||
" croprect = (scaledrect[0], scaledrect[1], scaledrect[2]-scaledrect[0], scaledrect[3]-scaledrect[1])\n",
|
||||
" return croppedbaseimage, croprect\n",
|
||||
" return croppedbaseimage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 523,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def contourcrop(baseimage):\n",
|
||||
" # return baseimage\n",
|
||||
" shrunkencbi, sizemultiplier = mf.ResizeWithAspectRatio(baseimage, width=1000, retscale=True)\n",
|
||||
" # gray = cv2.cvtColor(shrunkencbi, cv2.COLOR_BGR2GRAY)\n",
|
||||
" gray = cv2.cvtColor(shrunkencbi, cv2.COLOR_BGR2LAB)[:,:,0]\n",
|
||||
" # thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]\n",
|
||||
" thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_TRIANGLE)[1]\n",
|
||||
" # window = gray.shape[1]//7\n",
|
||||
" # if window % 2 == 0:\n",
|
||||
" # window += 1\n",
|
||||
" # thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 10)\n",
|
||||
" # return thresh\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))\n",
|
||||
" # thresh = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel, iterations=2)\n",
|
||||
" thresh = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel)\n",
|
||||
" # return thresh\n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(thresh,cv2.RETR_LIST, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
" \n",
|
||||
" # temp = cv2.drawContours(shrunkencbi, contours, -1, (0,255,0), thickness=3)\n",
|
||||
" # biggestcontour = max(contours, key=cv2.contourArea)\n",
|
||||
" # temp = cv2.drawContours(shrunkencbi, [biggestcontour], -1, (0,255,0), thickness=3)\n",
|
||||
" \n",
|
||||
" # return temp\n",
|
||||
" \n",
|
||||
" mx = (0,0,0,0)\n",
|
||||
" mx_area = 0\n",
|
||||
"\n",
|
||||
" for i, cont in enumerate(contours):\n",
|
||||
" rect = cv2.boundingRect(cont)\n",
|
||||
" area = mf.rectArea(rect)\n",
|
||||
" if (area > mx_area):\n",
|
||||
" mx = rect\n",
|
||||
" mx_area = area\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" scaledmx = (int(mx[0]*sizemultiplier), int(mx[1]*sizemultiplier), int(mx[2]*sizemultiplier), int(mx[3]*sizemultiplier))\n",
|
||||
" finalbaseimage = baseimage[scaledmx[1]:scaledmx[1]+scaledmx[3], scaledmx[0]:scaledmx[0]+scaledmx[2], :]\n",
|
||||
" return finalbaseimage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 524,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlinedeskewthencrop(baseimage, preppedimage, rotationangle, mask): \n",
|
||||
" rotatedbaseimage = mf.rotatewithexactpadding(baseimage, rotationangle, fill=(0,0,0))\n",
|
||||
" rotateddst1 = mf.rotatewithexactpadding(preppedimage, rotationangle, fill=(0,0,0))\n",
|
||||
" sizemultiplier = rotatedbaseimage.shape[0]/rotateddst1.shape[0]\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))\n",
|
||||
" shrunkmask = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)\n",
|
||||
" \n",
|
||||
" rotatedmask = mf.rotatewithexactpadding(shrunkmask, rotationangle, fill=0)\n",
|
||||
" \n",
|
||||
" # rotatedbaseimage = cv2.inpaint(rotatedbaseimage, rotatedmask, 3, cv2.INPAINT_TELEA)\n",
|
||||
" # return rotatedmask, 5\n",
|
||||
" return rotatedbaseimage, 5\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" croppedbaseimage, croprect = houghlinepcrop(rotatedbaseimage, rotateddst1, sizemultiplier, returnrect=True)\n",
|
||||
" # return croppedbaseimage, 5\n",
|
||||
" croppedmask = rotatedmask[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2]]\n",
|
||||
" \n",
|
||||
" # return croppedmask, 5\n",
|
||||
" # return croppedbaseimage, 5\n",
|
||||
" # t = time.time()\n",
|
||||
" adjustedimage = cv2.inpaint(croppedbaseimage, 255-croppedmask, 3, cv2.INPAINT_TELEA)\n",
|
||||
" # print(\"inpaint time:\", time.time()-t)\n",
|
||||
" \n",
|
||||
" return adjustedimage, 5\n",
|
||||
"\n",
|
||||
" # finalbaseimage = contourcrop(adjustedimage)\n",
|
||||
" # finalbaseimage = croppedbaseimage\n",
|
||||
" \n",
|
||||
" return finalbaseimage, rotationangle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 525,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def premorphCrop(image, mask):\n",
|
||||
" colourmask = cv2.cvtColor(255-mask, cv2.COLOR_GRAY2BGR)\n",
|
||||
" image = cv2.bitwise_or(image, colourmask)\n",
|
||||
" # croppedimage \n",
|
||||
" \n",
|
||||
" croprect = mf.croptoblack(255-mask, extraborder=0, returnrect=True)\n",
|
||||
" \n",
|
||||
" croppedimage = image[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2], :]\n",
|
||||
" # return croppedimage, (0,0,0,0)\n",
|
||||
" #IDEA, try cropping the image for triangle threshold. not too sure how triangle threshold actually works\n",
|
||||
" ogshape = croppedimage.shape\n",
|
||||
" miniimage = mf.ResizeWithAspectRatio(croppedimage, width=1000) if (ogshape[1] > ogshape[0]) else mf.ResizeWithAspectRatio(croppedimage, height=1000)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # hsvmini = cv2.cvtColor(miniimage, cv2.COLOR_BGR2HSV)\n",
|
||||
" # grayh = hsvmini[:,:,1]\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # grayg = cv2.cvtColor(miniimage,cv2.COLOR_BGR2GRAY)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # threshh = cv2.threshold(grayh, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
||||
" # threshh = 255-threshh\n",
|
||||
" \n",
|
||||
" # threshg = cv2.threshold(grayg, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
||||
" \n",
|
||||
" # window = grayh.shape[1]//10\n",
|
||||
" # if window % 2 == 0:\n",
|
||||
" # window += 1\n",
|
||||
" \n",
|
||||
" # grayh = 255-grayh\n",
|
||||
" # threshha = cv2.adaptiveThreshold(grayh, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, window, 4)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # miniimage = croppedimage\n",
|
||||
" \n",
|
||||
" hsvmini = cv2.cvtColor(miniimage, cv2.COLOR_BGR2HSV)\n",
|
||||
" # return hsvmini, (0,0,0,0)\n",
|
||||
" bing = hsvmini[:,:,1]\n",
|
||||
" # return bing, (0,0,0,0)\n",
|
||||
" gray = bing\n",
|
||||
" # gray = cv2.cvtColor(hsvmini, cv2.COLOR_BGR2GRAY)\n",
|
||||
" # return gray, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # labhsv = cv2.cvtColor(hsvmini, cv2.COLOR_BGR2LAB)\n",
|
||||
" # labhsvl = labhsv[:,:,1]\n",
|
||||
" # return labhsvl, (0,0,0,0)\n",
|
||||
" # gray = labhsvl\n",
|
||||
"\n",
|
||||
" # labmini = cv2.cvtColor(miniimage, cv2.COLOR_BGR2LAB)\n",
|
||||
" # return labmini, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" # whitesquare = (0, 0, 50)\n",
|
||||
" # graysquare = (180, 80, 255)\n",
|
||||
" \n",
|
||||
" # maskwhite = cv2.inRange(hsvmini, whitesquare, graysquare)\n",
|
||||
" \n",
|
||||
" # temp = cv2.bitwise_and(miniimage, miniimage, mask=maskwhite)\n",
|
||||
" # return temp, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # convert to grayscale\n",
|
||||
" # gray = cv2.cvtColor(miniimage,cv2.COLOR_BGR2GRAY)\n",
|
||||
" # miniimage = cv2.blur(miniimage, (4,4))\n",
|
||||
" # gray = cv2.cvtColor(miniimage, cv2.COLOR_BGR2LAB)[:,:,0]\n",
|
||||
" \n",
|
||||
" # blurredgray = cv2.blur(gray, (4,4))\n",
|
||||
" # canniedgray = cv2.Canny(gray, 0, 500)\n",
|
||||
" # kernel = np.ones((3,3), np.uint8)\n",
|
||||
" # morph = cv2.morphologyEx(canniedgray, cv2.MORPH_DILATE, kernel, iterations=1)\n",
|
||||
" \n",
|
||||
" # kernel = np.ones((7,7), np.uint8)\n",
|
||||
" # morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel, iterations=1)\n",
|
||||
" # # blurred = cv2.blur(canniedgray, (11,11))\n",
|
||||
" # return canniedgray, (0,0,0,0)\n",
|
||||
" # return morph, (0,0,0,0)\n",
|
||||
"\n",
|
||||
" window = gray.shape[1]//10\n",
|
||||
" if window % 2 == 0:\n",
|
||||
" window += 1\n",
|
||||
" # print(window)\n",
|
||||
" # gray = cv2.blur(gray, (11,11))\n",
|
||||
"\n",
|
||||
" # threshold\n",
|
||||
" # thresh = cv2.threshold(gray, 170, 255, cv2.THRESH_BINARY)[1]\n",
|
||||
" # thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)[1]\n",
|
||||
" # thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_TRIANGLE)[1]\n",
|
||||
"\n",
|
||||
" gray = 255-gray\n",
|
||||
" thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, window, 4)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # thresh = cv2.bitwise_and(thresh, thresh1)\n",
|
||||
" # thresh = cv2.bitwise_or(thresh, 255-mask)\n",
|
||||
" \n",
|
||||
" # temp = cv2.bitwise_and(thresh, 255-canniedgray)\n",
|
||||
" # return temp, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" # thresh = 255-thresh\n",
|
||||
"\n",
|
||||
" # return thresh, (0,0,0,0)\n",
|
||||
"\n",
|
||||
" # apply morphology\n",
|
||||
" kernel = np.ones((3,3), np.uint8)\n",
|
||||
" morph = cv2.morphologyEx(thresh, cv2.MORPH_OPEN, kernel, iterations=2)\n",
|
||||
" kernel = np.ones((5,5), np.uint8)\n",
|
||||
" # morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel, iterations=1)\n",
|
||||
" # morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)\n",
|
||||
" kernel = np.ones((9,9), np.uint8)\n",
|
||||
" # morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel, iterations=1)\n",
|
||||
" # kernel = np.ones((3,3), np.uint8)\n",
|
||||
" # morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)\n",
|
||||
" \n",
|
||||
" # return morph, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
" big_contour = max(contours, key=cv2.contourArea)\n",
|
||||
" \n",
|
||||
" miniblank = np.zeros(gray.shape, dtype=np.uint8)\n",
|
||||
" morphmask = miniblank.copy()\n",
|
||||
" morphmask = cv2.drawContours(morphmask, [big_contour], -1, 255, thickness=cv2.FILLED) \n",
|
||||
" \n",
|
||||
" return morphmask, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" dim = int(min(thresh.shape)/10)\n",
|
||||
" bkernel = cv2.getStructuringElement(cv2.MORPH_RECT, (dim, dim))\n",
|
||||
" brkernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (dim, dim))\n",
|
||||
" morphmask = cv2.morphologyEx(morphmask, cv2.MORPH_CLOSE, bkernel)\n",
|
||||
" morphmask = cv2.morphologyEx(morphmask, cv2.MORPH_OPEN, bkernel)\n",
|
||||
" # morphmask = cv2.morphologyEx(morphmask, cv2.MORPH_CLOSE, bkernel)\n",
|
||||
" # return morphmask, (0,0,0,0)\n",
|
||||
" \n",
|
||||
" contours, heirarchy = cv2.findContours(morphmask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)\n",
|
||||
" big_contour = max(contours, key=cv2.contourArea)\n",
|
||||
" \n",
|
||||
" # epsilon = 0.01 * cv2.arcLength(big_contour, True)\n",
|
||||
" # contour = cv2.approxPolyDP(big_contour, epsilon, True)\n",
|
||||
" # contour = cv2.convexHull(big_contour)\n",
|
||||
" contour = big_contour\n",
|
||||
" \n",
|
||||
" morphmask = cv2.drawContours(miniblank.copy(),[contour], -1, 255, thickness=cv2.FILLED) \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
"\n",
|
||||
"\n",
|
||||
" morph = cv2.resize(morphmask, (ogshape[1], ogshape[0]))\n",
|
||||
" # morph = cv2.bitwise_and(bigbinary, mask)\n",
|
||||
" # return morph, (0,0,0,0)\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" temp = cv2.bitwise_and(croppedimage, croppedimage, mask=morph)\n",
|
||||
" return temp, (0,0,0,0)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" # get largest contour\n",
|
||||
" contours, heirarchy = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)\n",
|
||||
" big_contour = max(contours, key=cv2.contourArea)\n",
|
||||
" \n",
|
||||
" # temp = cv2.drawContours(croppedimage, [big_contour], -1, (255,0,0), thickness=2)\n",
|
||||
" # return temp, (0,0,0,0)\n",
|
||||
"\n",
|
||||
"\n",
|
||||
" # get bounding box\n",
|
||||
" x,y,w,h = cv2.boundingRect(big_contour)\n",
|
||||
" x += croprect[0]\n",
|
||||
" y += croprect[1]\n",
|
||||
"\n",
|
||||
" # draw filled contour on black background\n",
|
||||
" mask = np.zeros((ogshape[0], ogshape[1]), dtype=np.uint8)\n",
|
||||
" # mask = cv2.merge([mask,mask,mask])\n",
|
||||
" # mask = cv2.blur(mask,(121,121))\n",
|
||||
" cv2.drawContours(mask, [big_contour], -1, 255, cv2.FILLED)\n",
|
||||
" mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)\n",
|
||||
" borderType = cv2.BORDER_CONSTANT\n",
|
||||
" mask = cv2.copyMakeBorder(mask, croprect[1], (image.shape[0]-(croprect[1]+croprect[3])), croprect[0], (image.shape[1]-(croprect[0]+croprect[2])), borderType, None, (0,0,0))\n",
|
||||
" # return mask, (0,0,0,0)\n",
|
||||
"\n",
|
||||
" # apply mask to input\n",
|
||||
" result1 = image.copy()\n",
|
||||
" mask = cv2.blur(mask,(3,3))\n",
|
||||
" result1 = cv2.bitwise_and(result1, mask)\n",
|
||||
"\n",
|
||||
" # crop result\n",
|
||||
" result2 = result1[y:y+h, x:x+w]\n",
|
||||
" return result2, (x,y,w,h)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 526,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def prepimageforhoughline(image, returnrect=True):\n",
|
||||
" prepped, scaler, hp, vp, mask = mf.squareandthenresize(image, fill=255, width=1000, returnscalerinfo=True, returnmask=True)\n",
|
||||
" ogpreppedshape = prepped.shape\n",
|
||||
" # t = time.time()\n",
|
||||
" prepped, croprect = premorphCrop(prepped, mask)\n",
|
||||
" # print(\"premorphCrop time:\", time.time()-t)\n",
|
||||
" return prepped\n",
|
||||
" if (prepped.shape[1] > prepped.shape[0]):\n",
|
||||
" prepped, preppedscaler = mf.ResizeWithAspectRatio(prepped, width=1000, retscale=True)\n",
|
||||
" else:\n",
|
||||
" prepped, preppedscaler = mf.ResizeWithAspectRatio(prepped, height=1000, retscale=True)\n",
|
||||
" finalcroprect = (int(croprect[0]*scaler - hp), int(croprect[1]*scaler - vp), int(croprect[2]*scaler), int(croprect[3]*scaler))\n",
|
||||
" gray1 = cv2.cvtColor(prepped, cv2.COLOR_BGR2GRAY)\n",
|
||||
" padding=30\n",
|
||||
" gray1 = mf.padWithColour(gray1, hpadding=padding, vpadding=padding, fill=0)\n",
|
||||
" return gray1\n",
|
||||
" \n",
|
||||
"\n",
|
||||
" dst1 = cv2.Canny(gray1, 0, 500, None, 3)\n",
|
||||
" return dst1\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" kernel = np.ones((5,5), np.uint8)\n",
|
||||
" out = cv2.morphologyEx(dst1, cv2.MORPH_DILATE, kernel)\n",
|
||||
" out = cv2.blur(out, (5,5))\n",
|
||||
" kernel = np.ones((6,6), np.uint8)\n",
|
||||
" dst1 = cv2.morphologyEx(out, cv2.MORPH_ERODE, kernel)\n",
|
||||
" # return dst1\n",
|
||||
"\n",
|
||||
" dst1 = cv2.Canny(dst1, 0, 500, None, 3)\n",
|
||||
" dst1 = dst1[padding:-padding, padding:-padding]\n",
|
||||
" return dst1\n",
|
||||
" \n",
|
||||
" accompaniedimage = image[finalcroprect[1]:finalcroprect[1]+finalcroprect[3], finalcroprect[0]:finalcroprect[0]+finalcroprect[2], :]\n",
|
||||
" if returnrect:\n",
|
||||
" borderType = cv2.BORDER_CONSTANT\n",
|
||||
" preppadding = [croprect[0], croprect[1], ogpreppedshape[1]-(croprect[0]+croprect[2]), ogpreppedshape[0]-(croprect[1]+croprect[3])]\n",
|
||||
" preppadding = [int(s/preppedscaler) for s in preppadding]\n",
|
||||
" # print(preppadding)\n",
|
||||
" paddedprepped = cv2.copyMakeBorder(dst1, preppadding[1], preppadding[3], preppadding[0], preppadding[2], borderType, 0)\n",
|
||||
" \n",
|
||||
" squaredimage = mf.squarepad(image, fill=0)\n",
|
||||
" mask = np.full((image.shape[0],image.shape[1]), fill_value=255, dtype=np.uint8)\n",
|
||||
" mask = mf.squarepad(mask, fill=0)\n",
|
||||
" \n",
|
||||
" # print(finalcroprect)\n",
|
||||
" \n",
|
||||
" return dst1, accompaniedimage, paddedprepped, squaredimage, mask, finalcroprect\n",
|
||||
" else:\n",
|
||||
" return dst1, accompaniedimage"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 527,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlinedeskewangle(image):\n",
|
||||
" imgcopy = cv2.cvtColor(image, cv2.COLOR_GRAY2BGR)\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" minlength = int(max(image.shape)/5)\n",
|
||||
" maxgap = int(max(image.shape)/20)\n",
|
||||
" lines = cv2.HoughLinesP(image, 1, np.pi / 180, 40, None, minlength, maxgap)\n",
|
||||
" angles = np.array([0])\n",
|
||||
" if lines is not None:\n",
|
||||
" angles = np.zeros(len(lines))\n",
|
||||
" for i in range(0, len(lines)):\n",
|
||||
" l = lines[i][0]\n",
|
||||
" angles[i] = mf.lineAngle(l)\n",
|
||||
" # imgcopy = cv2.line(imgcopy, (l[0], l[1]), (l[2], l[3]), (0,0,255), 3, cv2.LINE_AA)\n",
|
||||
" \n",
|
||||
" # mode = st.mode(np.around(angles, decimals=3))[0]\n",
|
||||
" # rotationangle = np.rad2deg(mode)\n",
|
||||
" # print(rotationangle)\n",
|
||||
" # return rotationangle, imgcopy\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" \n",
|
||||
" lines = cv2.HoughLines(image, 1, np.pi/180, int(max(image.shape[0], image.shape[1])/6), None, 0, 0)\n",
|
||||
" angles = np.array([0])\n",
|
||||
" if lines is not None:\n",
|
||||
" angles = np.zeros(len(lines))\n",
|
||||
" for i in range(0, len(lines)):\n",
|
||||
" rho = lines[i][0][0]\n",
|
||||
" theta = lines[i][0][1]\n",
|
||||
" a = math.cos(theta)\n",
|
||||
" b = math.sin(theta)\n",
|
||||
" x0 = a * rho\n",
|
||||
" y0 = b * rho\n",
|
||||
" unroundedpt1 = (x0 + 1000*(-b), y0 + 1000*(a))\n",
|
||||
" unroundedpt2 = (x0 - 1000*(-b), y0 - 1000*(a))\n",
|
||||
" pt1 = (int(unroundedpt1[0]), int(unroundedpt1[1]))\n",
|
||||
" pt2 = (int(unroundedpt2[0]), int(unroundedpt2[1]))\n",
|
||||
" v1_theta = math.atan2(pt1[1], pt1[0])\n",
|
||||
" v2_theta = math.atan2(pt2[1], pt2[0])\n",
|
||||
" # print(math.atan2(unroundedpt2[1] - unroundedpt1[1], unroundedpt2[0] - unroundedpt1[0]) % np.pi)\n",
|
||||
" # print(lineAngle((unroundedpt1[0], unroundedpt1[1], unroundedpt2[0], unroundedpt2[1])))\n",
|
||||
" # angles[i] = math.atan2(unroundedpt2[1] - unroundedpt1[1], unroundedpt2[0] - unroundedpt1[0]) % np.pi\n",
|
||||
" angles[i] = mf.lineAngle((unroundedpt1[0], unroundedpt1[1], unroundedpt2[0], unroundedpt2[1]))\n",
|
||||
" # imgcopy = cv2.line(imgcopy, pt1, pt2, (0,0,255), 3, cv2.LINE_AA)\n",
|
||||
"\n",
|
||||
" mode = st.mode(np.around(angles, decimals=3))[0]\n",
|
||||
" rotationangle = np.rad2deg(mode)\n",
|
||||
" return rotationangle, imgcopy\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 528,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlinedeskewandcrop(image):\n",
|
||||
" # croppedcanny, croppedimage, canny, squaredimage, ogimagemask, rect = prepimageforhoughline(image, returnrect=True) ## scaling and cropping occurs. need to also return the changes done\n",
|
||||
" return prepimageforhoughline(image, returnrect=True), 5\n",
|
||||
" # return canny, ogimage\n",
|
||||
" # print(canny.shape)\n",
|
||||
" # print(croppedogimage.shape)\n",
|
||||
" # return croppedimage, 5\n",
|
||||
"\n",
|
||||
" ## -----------------finding angle to deskew-----------------\n",
|
||||
" rotationangle, pic = houghlinedeskewangle(croppedcanny)\n",
|
||||
" return pic, rotationangle\n",
|
||||
" # print(croppedcanny.shape)\n",
|
||||
" # print(abs(rotationangle))\n",
|
||||
" if (croppedcanny.shape[0] > croppedcanny.shape[1]):\n",
|
||||
" if (rotationangle > 45):\n",
|
||||
" rotationangle -= 90\n",
|
||||
" elif rotationangle < -45:\n",
|
||||
" rotationangle += 90\n",
|
||||
" # print(rotationangle)\n",
|
||||
" # elif (croppedcanny.shape[1] > croppedcanny.shape[0]):\n",
|
||||
" # if (rotationangle > 45):\n",
|
||||
" # rotationangle -= 90\n",
|
||||
" # elif rotationangle < -45:\n",
|
||||
" # rotationangle += 90\n",
|
||||
" # print(rotationangle)\n",
|
||||
" \n",
|
||||
" # return croppedimage, 5\n",
|
||||
"\n",
|
||||
" \n",
|
||||
" # rotatorrect = findcroprectforangle(rect, angle)\n",
|
||||
"\n",
|
||||
" # -----------------end of finding angle to deskew-----------------\n",
|
||||
"\n",
|
||||
" ## -----------------deskewing and then cropping-----------------\n",
|
||||
" outimg, angle = houghlinedeskewthencrop(squaredimage, canny, rotationangle, ogimagemask)\n",
|
||||
" return outimg, angle"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 529,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"def houghlineprocessing(image):\n",
|
||||
" croppedanddeskewed, angle = mf.houghlinedeskewandcrop(image)\n",
|
||||
" croppedanddeskewed, angle = houghlinedeskewandcrop(image)\n",
|
||||
" # return croppedanddeskewed\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # postprocessed = cropclarifying(croppedanddeskewed)\n",
|
||||
" postprocessed = croppedanddeskewed\n",
|
||||
" # postprocessed = croppedanddeskewed\n",
|
||||
" # return postprocessed\n",
|
||||
" # postprocessed = mf.croptoblack(postprocessed)\n",
|
||||
" \n",
|
||||
@ -512,7 +1018,7 @@
|
||||
" \n",
|
||||
" # final = mf.externaldeskew(postprocessed, fill=(255,255,255))\n",
|
||||
" # rotangle = mf.receipttextdeskew(postprocessed, fill=(255,255,255), returnangle=True)\n",
|
||||
" final = postprocessed\n",
|
||||
" # final = postprocessed\n",
|
||||
" \n",
|
||||
" \n",
|
||||
" # final = mf.croptoblack(final)\n",
|
||||
@ -522,12 +1028,13 @@
|
||||
" # cv2.waitKey(0)\n",
|
||||
" # cv2.destroyAllWindows()\n",
|
||||
" \n",
|
||||
" return final"
|
||||
" # return final\n",
|
||||
" return croppedanddeskewed"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 10,
|
||||
"execution_count": 530,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -536,17 +1043,9 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 11,
|
||||
"execution_count": 531,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"0.0\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# prepped, scaler, hp, vp = mf.squareandthenresize(img, fill=255, width=1000, returnscalerinfo=True)\n",
|
||||
"outs = houghlineprocessing(img)\n",
|
||||
@ -560,7 +1059,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 12,
|
||||
"execution_count": 532,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -574,7 +1073,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 13,
|
||||
"execution_count": 533,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -586,7 +1085,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 14,
|
||||
"execution_count": 534,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -595,7 +1094,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 15,
|
||||
"execution_count": 535,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -605,7 +1104,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 16,
|
||||
"execution_count": 536,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -635,30 +1134,14 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 17,
|
||||
"execution_count": 537,
|
||||
"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"
|
||||
"average time: 0.10459864317481198(s)\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -669,7 +1152,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 18,
|
||||
"execution_count": 538,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -679,7 +1162,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 19,
|
||||
"execution_count": 539,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -688,7 +1171,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 20,
|
||||
"execution_count": 540,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -699,7 +1182,7 @@
|
||||
],
|
||||
"metadata": {
|
||||
"kernelspec": {
|
||||
"display_name": "Python 3",
|
||||
"display_name": "Python 3 (ipykernel)",
|
||||
"language": "python",
|
||||
"name": "python3"
|
||||
},
|
||||
|
||||
@ -28,7 +28,12 @@ def ResizeWithAspectRatio(image, width=None, height=None, inter=cv2.INTER_AREA,
|
||||
return cv2.resize(image, dim, interpolation=inter)
|
||||
|
||||
|
||||
def squareandthenresize(image, fill=0, width=None, height=None, inter=cv2.INTER_AREA, returnscalerinfo=False):
|
||||
def squareandthenresize(image, fill=0, width=None, height=None, inter=cv2.INTER_AREA, returnscalerinfo=False, returnmask=False):
|
||||
if (returnmask):
|
||||
h,w = image.shape[0], image.shape[1]
|
||||
mask = np.full((h,w), fill_value=255, dtype=np.uint8)
|
||||
mask = squarepad(mask, fill=0, returnoffset=False)
|
||||
mask = ResizeWithAspectRatio(mask, width=width, height=height, inter=inter, retscale=False)
|
||||
out = squarepad(image, fill=fill, returnoffset=returnscalerinfo)
|
||||
if (returnscalerinfo):
|
||||
squaredimage, hp, vp = out
|
||||
@ -37,9 +42,13 @@ def squareandthenresize(image, fill=0, width=None, height=None, inter=cv2.INTER_
|
||||
out = ResizeWithAspectRatio(squaredimage, width=width, height=height, inter=inter, retscale=returnscalerinfo)
|
||||
if (returnscalerinfo):
|
||||
finalimage, scaler = out
|
||||
if (returnmask):
|
||||
return finalimage, scaler, hp, vp, mask
|
||||
return finalimage, scaler, hp, vp
|
||||
else:
|
||||
finalimage = out
|
||||
if (returnmask):
|
||||
return finalimage, mask
|
||||
return finalimage
|
||||
|
||||
|
||||
@ -209,6 +218,8 @@ def WithinXDegrees(lines, margin, baseangle=0):
|
||||
# outlines = np.array([[]])
|
||||
outlines = np.empty((0, 4))
|
||||
# print(outlines.shape)
|
||||
if lines is None:
|
||||
return outlines
|
||||
for line in lines:
|
||||
# print(type(line))
|
||||
# print(abs(lineAngle(line[0])))
|
||||
@ -246,16 +257,32 @@ def lineswithinrange(lines, pt1, pt2, x=True, y=False):
|
||||
if (x):
|
||||
minx = min(pt1[0], pt2[0])
|
||||
maxx = max(pt1[0], pt2[0])
|
||||
out_lines = [line for line in out_lines if ((min(line[0],line[2]) >= minx) and (max(line[0],line[2]) <= maxx))]
|
||||
out_lines = [list(line) for line in out_lines if ((min(line[0],line[2]) >= minx) and (max(line[0],line[2]) <= maxx))]
|
||||
if (y):
|
||||
miny = min(pt1[1], pt2[1])
|
||||
maxy = max(pt1[1], pt2[1])
|
||||
out_lines = [line for line in out_lines if ((min(line[1],line[3]) >= minx) and (max(line[1],line[3]) <= maxx))]
|
||||
out_lines = [list(line) for line in out_lines if ((min(line[1],line[3]) >= minx) and (max(line[1],line[3]) <= maxx))]
|
||||
if len(out_lines) == 0:
|
||||
out_lines = np.empty((0, 4))
|
||||
return out_lines
|
||||
|
||||
def premorphCrop(image):
|
||||
def premorphCrop(image, mask):
|
||||
colourmask = cv2.cvtColor(255-mask, cv2.COLOR_GRAY2BGR)
|
||||
image = cv2.bitwise_or(image, colourmask)
|
||||
# croppedimage
|
||||
|
||||
croprect = croptoblack(255-mask, extraborder=0, returnrect=True)
|
||||
|
||||
croppedimage = image[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2], :]
|
||||
# return croppedimage, (0,0,0,0)
|
||||
#IDEA, try cropping the image for triangle threshold. not too sure how triangle threshold actually works
|
||||
ogshape = croppedimage.shape
|
||||
miniimage = ResizeWithAspectRatio(croppedimage, width=1000) if (ogshape[1] > ogshape[0]) else ResizeWithAspectRatio(croppedimage, height=1000)
|
||||
# miniimage = croppedimage
|
||||
|
||||
# convert to grayscale
|
||||
gray = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
|
||||
# gray = cv2.cvtColor(miniimage,cv2.COLOR_BGR2GRAY)
|
||||
gray = cv2.cvtColor(miniimage, cv2.COLOR_BGR2LAB)[:,:,0]
|
||||
|
||||
window = gray.shape[1]//8
|
||||
if window % 2 == 0:
|
||||
@ -265,43 +292,59 @@ def premorphCrop(image):
|
||||
|
||||
# threshold
|
||||
# thresh = cv2.threshold(gray, 170, 255, cv2.THRESH_BINARY)[1]
|
||||
# thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_OTSU)[1]
|
||||
# thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_TRIANGLE)[1]
|
||||
|
||||
thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_MEAN_C, cv2.THRESH_BINARY, window, 2)
|
||||
|
||||
|
||||
# thresh = cv2.bitwise_and(thresh, thresh1)
|
||||
# thresh = cv2.bitwise_or(thresh, 255-mask)
|
||||
|
||||
|
||||
|
||||
# return thresh
|
||||
# return thresh, (0,0,0,0)
|
||||
|
||||
# apply morphology
|
||||
kernel = np.ones((9,9), np.uint8)
|
||||
morph = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel)
|
||||
kernel = np.ones((3,3), np.uint8)
|
||||
morph = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel, iterations=2)
|
||||
# morph = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, kernel)
|
||||
kernel = np.ones((9,9), np.uint8)
|
||||
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
|
||||
kernel = np.ones((3,3), np.uint8)
|
||||
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
|
||||
morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel, iterations=1)
|
||||
# kernel = np.ones((3,3), np.uint8)
|
||||
# morph = cv2.morphologyEx(morph, cv2.MORPH_CLOSE, kernel)
|
||||
|
||||
# return morph
|
||||
|
||||
morph = cv2.resize(morph, (ogshape[1], ogshape[0]))
|
||||
# morph = cv2.bitwise_and(bigbinary, mask)
|
||||
# return morph, (0,0,0,0)
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
# get largest contour
|
||||
contours = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
contours = contours[0] if len(contours) == 2 else contours[1]
|
||||
area_thresh = 0
|
||||
for c in contours:
|
||||
area = cv2.contourArea(c)
|
||||
if area > area_thresh:
|
||||
area_thresh = area
|
||||
big_contour = c
|
||||
contours, heirarchy = cv2.findContours(morph, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE)
|
||||
big_contour = max(contours, key=cv2.contourArea)
|
||||
|
||||
# temp = cv2.drawContours(croppedimage, [big_contour], -1, (255,0,0), thickness=2)
|
||||
# return temp, (0,0,0,0)
|
||||
|
||||
|
||||
# get bounding box
|
||||
x,y,w,h = cv2.boundingRect(big_contour)
|
||||
x += croprect[0]
|
||||
y += croprect[1]
|
||||
|
||||
# draw filled contour on black background
|
||||
mask = np.zeros_like(gray)
|
||||
mask = cv2.merge([mask,mask,mask])
|
||||
mask = np.zeros((ogshape[0], ogshape[1]), dtype=np.uint8)
|
||||
# mask = cv2.merge([mask,mask,mask])
|
||||
# mask = cv2.blur(mask,(121,121))
|
||||
cv2.drawContours(mask, [big_contour], -1, (255,255,255), cv2.FILLED)
|
||||
cv2.drawContours(mask, [big_contour], -1, 255, cv2.FILLED)
|
||||
mask = cv2.cvtColor(mask, cv2.COLOR_GRAY2BGR)
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
mask = cv2.copyMakeBorder(mask, croprect[1], (image.shape[0]-(croprect[1]+croprect[3])), croprect[0], (image.shape[1]-(croprect[0]+croprect[2])), borderType, None, (0,0,0))
|
||||
# return mask, (0,0,0,0)
|
||||
|
||||
# apply mask to input
|
||||
result1 = image.copy()
|
||||
@ -343,17 +386,25 @@ def rotateLine(img, line, angle, returnint=True):
|
||||
return (pt1[0], pt1[1], pt2[0], pt2[1])
|
||||
|
||||
def prepimageforhoughline(image, returnrect=True):
|
||||
prepped, scaler, hp, vp = squareandthenresize(image, fill=255, width=1000, returnscalerinfo=True)
|
||||
prepped, scaler, hp, vp, mask = squareandthenresize(image, fill=255, width=1000, returnscalerinfo=True, returnmask=True)
|
||||
ogpreppedshape = prepped.shape
|
||||
prepped, croprect = premorphCrop(prepped)
|
||||
# t = time.time()
|
||||
prepped, croprect = premorphCrop(prepped, mask)
|
||||
# print("premorphCrop time:", time.time()-t)
|
||||
# return prepped
|
||||
if (prepped.shape[1] > prepped.shape[0]):
|
||||
prepped, preppedscaler = ResizeWithAspectRatio(prepped, width=1000, retscale=True)
|
||||
else:
|
||||
prepped, preppedscaler = ResizeWithAspectRatio(prepped, height=1000, retscale=True)
|
||||
finalcroprect = (int(croprect[0]*scaler - hp), int(croprect[1]*scaler - vp), int(croprect[2]*scaler), int(croprect[3]*scaler))
|
||||
gray1 = cv2.cvtColor(prepped, cv2.COLOR_BGR2GRAY)
|
||||
padding=30
|
||||
gray1 = padWithColour(gray1, hpadding=padding, vpadding=padding, fill=0)
|
||||
# return gray1
|
||||
|
||||
|
||||
dst1 = cv2.Canny(gray1, 0, 500, None, 3)
|
||||
# return dst1
|
||||
|
||||
|
||||
kernel = np.ones((5,5), np.uint8)
|
||||
@ -364,24 +415,32 @@ def prepimageforhoughline(image, returnrect=True):
|
||||
# return dst1
|
||||
|
||||
dst1 = cv2.Canny(dst1, 0, 500, None, 3)
|
||||
dst1 = dst1[padding:-padding, padding:-padding]
|
||||
# return dst1
|
||||
|
||||
accompaniedimage = image[finalcroprect[1]:finalcroprect[1]+finalcroprect[3], finalcroprect[0]:finalcroprect[0]+finalcroprect[2], :]
|
||||
if returnrect:
|
||||
borderType = cv2.BORDER_CONSTANT
|
||||
preppadding = [croprect[0], croprect[1], ogpreppedshape[1]-(croprect[0]+croprect[2]), ogpreppedshape[0]-(croprect[1]+croprect[3])]
|
||||
preppadding = [int(s/preppedscaler) for s in preppadding]
|
||||
# print(preppadding)
|
||||
paddedprepped = cv2.copyMakeBorder(dst1, preppadding[1], preppadding[3], preppadding[0], preppadding[2], borderType, 0)
|
||||
|
||||
squaredimage = squarepad(image, fill=0)
|
||||
mask = np.full((image.shape[0],image.shape[1]), fill_value=255, dtype=np.uint8)
|
||||
mask = squarepad(mask, fill=0)
|
||||
|
||||
return dst1, accompaniedimage, paddedprepped, squaredimage, finalcroprect
|
||||
# print(finalcroprect)
|
||||
|
||||
return dst1, accompaniedimage, paddedprepped, squaredimage, mask, finalcroprect
|
||||
else:
|
||||
return dst1, accompaniedimage
|
||||
|
||||
def houghlinedeskewangle(image):
|
||||
lines = cv2.HoughLines(image, 1, np.pi/180, int(max(image.shape[0], image.shape[1])/6), None, 0, 0)
|
||||
angles = np.zeros(len(lines))
|
||||
angles = np.array([0])
|
||||
if lines is not None:
|
||||
angles = np.zeros(len(lines))
|
||||
for i in range(0, len(lines)):
|
||||
rho = lines[i][0][0]
|
||||
theta = lines[i][0][1]
|
||||
@ -429,35 +488,65 @@ def rotatewithexactpadding(img, angle, fill=(0,0,0)):
|
||||
rotatedimg = rotate(baseimage, angle,fill=fill)
|
||||
return rotatedimg
|
||||
|
||||
def houghlinepcrop(baseimage, preppedimage, scalingmultiplier):
|
||||
rotatedlines = cv2.HoughLinesP(preppedimage, 1, np.pi / 180, 30, None, 90, 30)
|
||||
def houghlinepcrop(baseimage, preppedimage, scalingmultiplier, returnrect=False):
|
||||
hminlength = int(preppedimage.shape[1]/30)
|
||||
hmaxgap = int(preppedimage.shape[1]/35)
|
||||
|
||||
vmarginlines = WithinXDegrees(rotatedlines, 7)
|
||||
hmarginlines = WithinXDegrees(rotatedlines, 7, baseangle=90)
|
||||
# vrect = lineBoundingRect(vmarginlines,asRect=False, returnint=True)
|
||||
# hmarginlines = lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)
|
||||
vminlength = int(preppedimage.shape[0]/25)
|
||||
vmaxgap = int(preppedimage.shape[0]/35)
|
||||
# print(dim)
|
||||
|
||||
vrotatedlines = cv2.HoughLinesP(preppedimage, 1, np.pi / 180, 20, None, vminlength, vmaxgap)
|
||||
hrotatedlines = cv2.HoughLinesP(preppedimage, 1, np.pi / 180, 20, None, hminlength, hmaxgap)
|
||||
# if rotatedlines is None:
|
||||
# print("hi")
|
||||
# return baseimage
|
||||
vmarginlines = WithinXDegrees(vrotatedlines, 7)
|
||||
hmarginlines = WithinXDegrees(hrotatedlines, 7, baseangle=90)
|
||||
if (len(vmarginlines) != 0):
|
||||
# print("hi")
|
||||
vrect = lineBoundingRect(vmarginlines,asRect=False, returnint=True)
|
||||
hmarginlines = lineswithinrange(hmarginlines, (vrect[0], vrect[1]), (vrect[2],vrect[3]), x=True, y=False)
|
||||
# print(vmarginlines)
|
||||
# print(hmarginlines)
|
||||
marginlines = np.append(vmarginlines, hmarginlines, axis=0)
|
||||
|
||||
# colourdst = cv2.cvtColor(preppedimage, cv2.COLOR_GRAY2BGR)
|
||||
# if marginlines is not None:
|
||||
colourdst = cv2.cvtColor(preppedimage, cv2.COLOR_GRAY2BGR)
|
||||
# print(len(marginlines))
|
||||
# up = False
|
||||
# if len(marginlines) != 0:
|
||||
# # print("hi")
|
||||
# up = True
|
||||
# for l in marginlines:
|
||||
# cv2.line(colourdst, (int(l[0]), int(l[1])), (int(l[2]), int(l[3])), (0,0,255), 3, cv2.LINE_AA)
|
||||
# # if up:
|
||||
# # print("good")
|
||||
# # else:
|
||||
# # print("bad")
|
||||
|
||||
# # print("bye")
|
||||
# return colourdst
|
||||
|
||||
rect = lineBoundingRect(marginlines,asRect=False, returnint=True)
|
||||
scaledrect = (int(rect[0]*scalingmultiplier), int(rect[1]*scalingmultiplier), int(rect[2]*scalingmultiplier), int(rect[3]*scalingmultiplier))
|
||||
croppedbaseimage = baseimage[scaledrect[1]:scaledrect[3], scaledrect[0]:scaledrect[2], :]
|
||||
if returnrect:
|
||||
croprect = (scaledrect[0], scaledrect[1], scaledrect[2]-scaledrect[0], scaledrect[3]-scaledrect[1])
|
||||
return croppedbaseimage, croprect
|
||||
return croppedbaseimage
|
||||
|
||||
def contourcrop(baseimage):
|
||||
# return baseimage
|
||||
shrunkencbi, sizemultiplier = ResizeWithAspectRatio(baseimage, width=1000, retscale=True)
|
||||
gray = cv2.cvtColor(shrunkencbi, cv2.COLOR_BGR2GRAY)
|
||||
# gray = cv2.cvtColor(shrunkencbi, cv2.COLOR_BGR2GRAY)
|
||||
gray = cv2.cvtColor(shrunkencbi, cv2.COLOR_BGR2LAB)[:,:,0]
|
||||
# thresh = cv2.threshold(gray, 200, 255, cv2.THRESH_BINARY)[1]
|
||||
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_TRIANGLE)[1]
|
||||
# window = gray.shape[1]//7
|
||||
# if window % 2 == 0:
|
||||
# window += 1
|
||||
# thresh = cv2.adaptiveThreshold(gray, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, window, 10)
|
||||
# return thresh
|
||||
|
||||
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (5,5))
|
||||
# thresh = cv2.morphologyEx(thresh, cv2.MORPH_ERODE, kernel, iterations=2)
|
||||
@ -487,23 +576,46 @@ def contourcrop(baseimage):
|
||||
finalbaseimage = baseimage[scaledmx[1]:scaledmx[1]+scaledmx[3], scaledmx[0]:scaledmx[0]+scaledmx[2], :]
|
||||
return finalbaseimage
|
||||
|
||||
def houghlinedeskewthencrop(baseimage, preppedimage, rotationangle, croprect):
|
||||
def houghlinedeskewthencrop(baseimage, preppedimage, rotationangle, mask):
|
||||
rotatedbaseimage = rotatewithexactpadding(baseimage, rotationangle, fill=(0,0,0))
|
||||
rotateddst1 = rotatewithexactpadding(preppedimage, rotationangle, fill=(0,0,0))
|
||||
sizemultiplier = rotatedbaseimage.shape[0]/rotateddst1.shape[0]
|
||||
|
||||
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3,3))
|
||||
shrunkmask = cv2.morphologyEx(mask, cv2.MORPH_ERODE, kernel)
|
||||
|
||||
rotatedmask = rotatewithexactpadding(shrunkmask, rotationangle, fill=0)
|
||||
|
||||
# rotatedbaseimage = cv2.inpaint(rotatedbaseimage, rotatedmask, 3, cv2.INPAINT_TELEA)
|
||||
# return rotatedmask, 5
|
||||
# return rotatedbaseimage, 5
|
||||
|
||||
|
||||
croppedbaseimage = houghlinepcrop(rotatedbaseimage, rotateddst1, sizemultiplier)
|
||||
croppedbaseimage, croprect = houghlinepcrop(rotatedbaseimage, rotateddst1, sizemultiplier, returnrect=True)
|
||||
# return croppedbaseimage, 5
|
||||
croppedmask = rotatedmask[croprect[1]:croprect[1]+croprect[3], croprect[0]:croprect[0]+croprect[2]]
|
||||
|
||||
# return croppedmask, 5
|
||||
# return croppedbaseimage, 5
|
||||
# t = time.time()
|
||||
adjustedimage = cv2.inpaint(croppedbaseimage, 255-croppedmask, 3, cv2.INPAINT_TELEA)
|
||||
# print("inpaint time:", time.time()-t)
|
||||
|
||||
# return adjustedimage, 5
|
||||
|
||||
finalbaseimage = contourcrop(croppedbaseimage)
|
||||
finalbaseimage = contourcrop(adjustedimage)
|
||||
# finalbaseimage = croppedbaseimage
|
||||
|
||||
return finalbaseimage, rotationangle
|
||||
|
||||
def houghlinedeskewandcrop(image):
|
||||
croppedcanny, croppedimage, canny, ogimage, rect = prepimageforhoughline(image, returnrect=True) ## scaling and cropping occurs. need to also return the changes done
|
||||
croppedcanny, croppedimage, canny, squaredimage, ogimagemask, rect = prepimageforhoughline(image, returnrect=True) ## scaling and cropping occurs. need to also return the changes done
|
||||
# return prepimageforhoughline(image, returnrect=True), 5
|
||||
# return canny, ogimage
|
||||
# print(canny.shape)
|
||||
# print(croppedogimage.shape)
|
||||
# return croppedimage, 5
|
||||
|
||||
## -----------------finding angle to deskew-----------------
|
||||
rotationangle = houghlinedeskewangle(croppedcanny)
|
||||
@ -521,6 +633,8 @@ def houghlinedeskewandcrop(image):
|
||||
# elif rotationangle < -45:
|
||||
# rotationangle += 90
|
||||
# print(rotationangle)
|
||||
|
||||
# return croppedimage, 5
|
||||
|
||||
|
||||
# rotatorrect = findcroprectforangle(rect, angle)
|
||||
@ -528,7 +642,7 @@ def houghlinedeskewandcrop(image):
|
||||
# -----------------end of finding angle to deskew-----------------
|
||||
|
||||
## -----------------deskewing and then cropping-----------------
|
||||
outimg, angle = houghlinedeskewthencrop(ogimage, canny, rotationangle, rect)
|
||||
outimg, angle = houghlinedeskewthencrop(squaredimage, canny, rotationangle, ogimagemask)
|
||||
return outimg, angle
|
||||
|
||||
def bruteforceprocessrects(greaterrects, lesserrects):
|
||||
@ -1154,7 +1268,7 @@ def houghlineprocessing(image):
|
||||
|
||||
|
||||
# postprocessed = cropclarifying(croppedanddeskewed)
|
||||
postprocessed = croppedanddeskewed
|
||||
# postprocessed = croppedanddeskewed
|
||||
# return postprocessed
|
||||
# postprocessed = mf.croptoblack(postprocessed)
|
||||
|
||||
@ -1163,7 +1277,7 @@ def houghlineprocessing(image):
|
||||
|
||||
# final = mf.externaldeskew(postprocessed, fill=(255,255,255))
|
||||
# rotangle = mf.receipttextdeskew(postprocessed, fill=(255,255,255), returnangle=True)
|
||||
final = postprocessed
|
||||
# final = postprocessed
|
||||
|
||||
|
||||
# final = mf.croptoblack(final)
|
||||
@ -1173,6 +1287,7 @@ def houghlineprocessing(image):
|
||||
# cv2.waitKey(0)
|
||||
# cv2.destroyAllWindows()
|
||||
|
||||
return final
|
||||
# return final
|
||||
return croppedanddeskewed
|
||||
|
||||
###### DESIRE: CONVERT STUFF RELATED TO THE HOUGHLINE PROCESSING INTO C SINCE IT ONLY REALLY USES OPENCV
|
||||
@ -2,7 +2,7 @@
|
||||
"cells": [
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 26,
|
||||
"execution_count": 1,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -10,14 +10,26 @@
|
||||
"import pathlib\n",
|
||||
"import shutil\n",
|
||||
"import cv2\n",
|
||||
"import numpy as np"
|
||||
"import numpy as np\n",
|
||||
"import time"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 27,
|
||||
"execution_count": 2,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"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 sys\n",
|
||||
"sys.path.insert(0, '/mnt/code/autocropper')\n",
|
||||
@ -26,7 +38,16 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 28,
|
||||
"execution_count": 3,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"# print(sys.path)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 4,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -35,7 +56,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 29,
|
||||
"execution_count": 5,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -46,7 +67,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 30,
|
||||
"execution_count": 6,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -148,7 +169,7 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 31,
|
||||
"execution_count": 7,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -159,6 +180,8 @@
|
||||
" \n",
|
||||
" filenames = next(os.walk(imagespath), (None, None, []))[2]\n",
|
||||
" \n",
|
||||
" tdiffs = []\n",
|
||||
" \n",
|
||||
" for filename in filenames:\n",
|
||||
" suffix = pathlib.Path(filename).suffix\n",
|
||||
" if (suffix not in imagefileextensions):\n",
|
||||
@ -170,15 +193,19 @@
|
||||
" continue\n",
|
||||
" img = cv2.imread(imagespath+filename)\n",
|
||||
" # print(img)\n",
|
||||
" t1 = time.time()\n",
|
||||
" autocropped = mf.houghlineprocessing(img)\n",
|
||||
" tdiffs.append(time.time() - t1)\n",
|
||||
" cv2.imwrite(datasetpath+subpathtoaugmentedfiles+filename, autocropped)\n",
|
||||
" tdiffs = np.array(tdiffs)\n",
|
||||
" print(\"average time: \" + str(np.mean(tdiffs))+\"(s)\")\n",
|
||||
" \n",
|
||||
" "
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 32,
|
||||
"execution_count": 8,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
@ -200,16 +227,22 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 33,
|
||||
"execution_count": 9,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'IMG_7736.jpg': '0.jpg', 'IMG_7737.jpg': '1.jpg', 'IMG_7738.jpg': '2.jpg', 'IMG_7739.jpg': '3.jpg', 'IMG_7740.jpg': '4.jpg', 'IMG_7741.jpg': '5.jpg', 'IMG_7742.jpg': '6.jpg', 'IMG_7743.jpg': '7.jpg', 'IMG_7744.jpg': '8.jpg', 'IMG_7745.jpg': '9.jpg', 'IMG_7747.jpg': '10.jpg', 'IMG_7748.jpg': '11.jpg'}\n",
|
||||
"[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11]\n",
|
||||
"{}\n"
|
||||
"{}\n",
|
||||
"[]\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"{'IMG_7594.jpg': '0.jpg', 'IMG_7604.jpg': '1.jpg', 'IMG_7605.jpg': '2.jpg', 'IMG_7640.jpg': '3.jpg', 'IMG_7736.jpg': '4.jpg', 'IMG_7737.jpg': '5.jpg', 'IMG_7738.jpg': '6.jpg', 'IMG_7739.jpg': '7.jpg', 'IMG_7740.jpg': '8.jpg', 'IMG_7741.jpg': '9.jpg', 'IMG_7742.jpg': '10.jpg', 'IMG_7743.jpg': '11.jpg', 'IMG_7744.jpg': '12.jpg', 'IMG_7745.jpg': '13.jpg', 'IMG_7747.jpg': '14.jpg', 'IMG_7748.jpg': '15.jpg', 'IMG_7753.jpg': '16.jpg', 'IMG_7754.jpg': '17.jpg', 'IMG_7776.jpg': '18.jpg', 'IMG_7777.jpg': '19.jpg', 'IMG_7778.jpg': '20.jpg', 'IMG_7779.jpg': '21.jpg', 'IMG_7780.jpg': '22.jpg', 'IMG_7781.jpg': '23.jpg', 'IMG_7782.jpg': '24.jpg', 'IMG_7783.jpg': '25.jpg', 'IMG_7784.jpg': '26.jpg', 'IMG_7785.jpg': '27.jpg', 'IMG_7786.jpg': '28.jpg', 'IMG_7788.jpg': '29.jpg', 'IMG_7790.jpg': '30.jpg', 'IMG_7791.jpg': '31.jpg', 'IMG_7792.jpg': '32.jpg', 'IMG_7794.jpg': '33.jpg', 'IMG_7795.jpg': '34.jpg', 'IMG_7796.jpg': '35.jpg', 'IMG_7797.jpg': '36.jpg', 'IMG_7798.jpg': '37.jpg', 'IMG_7799.jpg': '38.jpg', 'IMG_7800.jpg': '39.jpg', 'IMG_7801.jpg': '40.jpg', 'IMG_7802.jpg': '41.jpg', 'IMG_7803.jpg': '42.jpg', 'IMG_7804.jpg': '43.jpg', 'IMG_7805.jpg': '44.jpg', 'IMG_7807.jpg': '45.jpg', 'IMG_7809.jpg': '46.jpg', 'IMG_7810.jpg': '47.jpg', 'IMG_7811.jpg': '48.jpg', 'IMG_7812.jpg': '49.jpg', 'IMG_7813.jpg': '50.jpg', 'IMG_7814.jpg': '51.jpg', 'IMG_7815.jpg': '52.jpg', 'IMG_7816.jpg': '53.jpg', 'IMG_7817.jpg': '54.jpg', 'IMG_7818.jpg': '55.jpg', 'IMG_7819.jpg': '56.jpg', 'IMG_7820.jpg': '57.jpg', 'IMG_7821.jpg': '58.jpg', 'IMG_7822.jpg': '59.jpg', 'IMG_7823.jpg': '60.jpg', 'IMG_7824.jpg': '61.jpg', 'IMG_7825.jpg': '62.jpg', 'IMG_7826.jpg': '63.jpg', 'IMG_7827.jpg': '64.jpg', 'IMG_7828.jpg': '65.jpg', 'IMG_7829.jpg': '66.jpg'}\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
@ -219,47 +252,76 @@
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": 34,
|
||||
"execution_count": 10,
|
||||
"metadata": {},
|
||||
"outputs": [
|
||||
{
|
||||
"ename": "error",
|
||||
"evalue": "OpenCV(4.5.4) ./modules/imgproc/src/resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'resize'\n",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31merror\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[1;32m/mnt/code/libraries/testprocessing.ipynb Cell 9\u001b[0m line \u001b[0;36m2\n\u001b[1;32m <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a>\u001b[0m img \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mimread(\u001b[39m'\u001b[39m\u001b[39m/mnt/dataset/baseimages/1.jpg\u001b[39m\u001b[39m'\u001b[39m)\n\u001b[0;32m----> <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X11sdnNjb2RlLXJlbW90ZQ%3D%3D?line=1'>2</a>\u001b[0m out \u001b[39m=\u001b[39m mf\u001b[39m.\u001b[39;49mhoughlineprocessing(img)\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:1042\u001b[0m, in \u001b[0;36mhoughlineprocessing\u001b[0;34m(image)\u001b[0m\n\u001b[1;32m 1041\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mhoughlineprocessing\u001b[39m(image):\n\u001b[0;32m-> 1042\u001b[0m croppedanddeskewed, _ \u001b[39m=\u001b[39m houghlinedeskewandcrop(image)\n\u001b[1;32m 1043\u001b[0m \u001b[39m##IF IT DOESN'T CHANGE THE IMAGE (CHANGE THE _ TO SOMETHING USEFUL), THEN CROPCLARIFYING SHOULD JUST DO THE TEXT ISOLATION SECTION AND NOT TRY AND WHITE OUT ANY BACKGROUND.\u001b[39;00m\n\u001b[1;32m 1044\u001b[0m \u001b[39m## IF THERE'S NO CROPPING, MAYBE EVEN JUMP RIGHT TO USING THE EXTERNAL DESKEW FIRST BEFORE TOSSING IT INTO CROPCLARIFYING\u001b[39;00m\n\u001b[1;32m 1046\u001b[0m postprocessed \u001b[39m=\u001b[39m cropclarifying(croppedanddeskewed)\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:452\u001b[0m, in \u001b[0;36mhoughlinedeskewandcrop\u001b[0;34m(image)\u001b[0m\n\u001b[1;32m 446\u001b[0m rotationangle \u001b[39m=\u001b[39m houghlinedeskewangle(dst1)\n\u001b[1;32m 448\u001b[0m \u001b[39m# -----------------end of finding angle to deskew-----------------\u001b[39;00m\n\u001b[1;32m 449\u001b[0m \n\u001b[1;32m 450\u001b[0m \u001b[39m## -----------------deskewing and then cropping-----------------\u001b[39;00m\n\u001b[0;32m--> 452\u001b[0m \u001b[39mreturn\u001b[39;00m houghlinedeskewthencrop(croppedogimage, dst1, rotationangle)\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:420\u001b[0m, in \u001b[0;36mhoughlinedeskewthencrop\u001b[0;34m(baseimage, preppedimage, rotationangle)\u001b[0m\n\u001b[1;32m 414\u001b[0m scaledrect \u001b[39m=\u001b[39m (\u001b[39mint\u001b[39m(rect[\u001b[39m0\u001b[39m]\u001b[39m*\u001b[39msizemultiplier), \u001b[39mint\u001b[39m(rect[\u001b[39m1\u001b[39m]\u001b[39m*\u001b[39msizemultiplier), \u001b[39mint\u001b[39m(rect[\u001b[39m2\u001b[39m]\u001b[39m*\u001b[39msizemultiplier), \u001b[39mint\u001b[39m(rect[\u001b[39m3\u001b[39m]\u001b[39m*\u001b[39msizemultiplier))\n\u001b[1;32m 416\u001b[0m croppedbaseimage \u001b[39m=\u001b[39m rotatedbaseimage[scaledrect[\u001b[39m1\u001b[39m]:scaledrect[\u001b[39m3\u001b[39m], scaledrect[\u001b[39m0\u001b[39m]:scaledrect[\u001b[39m2\u001b[39m], :]\n\u001b[0;32m--> 420\u001b[0m shrunkencbi, sizemultiplier \u001b[39m=\u001b[39m ResizeWithAspectRatio(croppedbaseimage, width\u001b[39m=\u001b[39;49m\u001b[39m1000\u001b[39;49m, retscale\u001b[39m=\u001b[39;49m\u001b[39mTrue\u001b[39;49;00m)\n\u001b[1;32m 421\u001b[0m gray \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mcvtColor(shrunkencbi, cv2\u001b[39m.\u001b[39mCOLOR_BGR2GRAY)\n\u001b[1;32m 422\u001b[0m thresh \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mthreshold(gray, \u001b[39m200\u001b[39m, \u001b[39m255\u001b[39m, cv2\u001b[39m.\u001b[39mTHRESH_BINARY)[\u001b[39m1\u001b[39m]\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:27\u001b[0m, in \u001b[0;36mResizeWithAspectRatio\u001b[0;34m(image, width, height, inter, retscale)\u001b[0m\n\u001b[1;32m 23\u001b[0m dim \u001b[39m=\u001b[39m (width, \u001b[39mint\u001b[39m(h \u001b[39m*\u001b[39m r))\n\u001b[1;32m 25\u001b[0m \u001b[39mif\u001b[39;00m (retscale \u001b[39m==\u001b[39m \u001b[39mTrue\u001b[39;00m):\n\u001b[1;32m 26\u001b[0m \u001b[39m# print(\"hi\")\u001b[39;00m\n\u001b[0;32m---> 27\u001b[0m \u001b[39mreturn\u001b[39;00m (cv2\u001b[39m.\u001b[39;49mresize(image, dim, interpolation\u001b[39m=\u001b[39;49minter), \u001b[39m1\u001b[39m\u001b[39m/\u001b[39mr)\n\u001b[1;32m 28\u001b[0m \u001b[39mreturn\u001b[39;00m cv2\u001b[39m.\u001b[39mresize(image, dim, interpolation\u001b[39m=\u001b[39minter)\n",
|
||||
"\u001b[0;31merror\u001b[0m: OpenCV(4.5.4) ./modules/imgproc/src/resize.cpp:4051: error: (-215:Assertion failed) !ssize.empty() in function 'resize'\n"
|
||||
]
|
||||
}
|
||||
],
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"img = cv2.imread('/mnt/dataset/baseimages/1.jpg')\n",
|
||||
"out = mf.houghlineprocessing(img)\n",
|
||||
"# img = cv2.imread('/mnt/dataset/baseimages/1.jpg')\n",
|
||||
"# out = mf.houghlineprocessing(img)\n",
|
||||
"\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 11,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"source": [
|
||||
"showimgs(out)"
|
||||
"# showimgs(out)"
|
||||
]
|
||||
},
|
||||
{
|
||||
"cell_type": "code",
|
||||
"execution_count": null,
|
||||
"execution_count": 12,
|
||||
"metadata": {},
|
||||
"outputs": [],
|
||||
"outputs": [
|
||||
{
|
||||
"name": "stdout",
|
||||
"output_type": "stream",
|
||||
"text": [
|
||||
"/mnt/dataset/baseimages/0.jpg\n",
|
||||
"/mnt/dataset/baseimages/1.jpg\n",
|
||||
"/mnt/dataset/baseimages/10.jpg\n",
|
||||
"/mnt/dataset/baseimages/11.jpg\n",
|
||||
"/mnt/dataset/baseimages/12.jpg\n",
|
||||
"/mnt/dataset/baseimages/13.jpg\n",
|
||||
"/mnt/dataset/baseimages/14.jpg\n",
|
||||
"/mnt/dataset/baseimages/15.jpg\n",
|
||||
"/mnt/dataset/baseimages/16.jpg\n",
|
||||
"/mnt/dataset/baseimages/17.jpg\n",
|
||||
"/mnt/dataset/baseimages/18.jpg\n",
|
||||
"/mnt/dataset/baseimages/19.jpg\n",
|
||||
"/mnt/dataset/baseimages/2.jpg\n",
|
||||
"/mnt/dataset/baseimages/20.jpg\n",
|
||||
"/mnt/dataset/baseimages/21.jpg\n",
|
||||
"/mnt/dataset/baseimages/22.jpg\n",
|
||||
"/mnt/dataset/baseimages/23.jpg\n",
|
||||
"/mnt/dataset/baseimages/24.jpg\n",
|
||||
"/mnt/dataset/baseimages/25.jpg\n",
|
||||
"/mnt/dataset/baseimages/26.jpg\n",
|
||||
"/mnt/dataset/baseimages/27.jpg\n",
|
||||
"/mnt/dataset/baseimages/28.jpg\n",
|
||||
"/mnt/dataset/baseimages/29.jpg\n"
|
||||
]
|
||||
},
|
||||
{
|
||||
"ename": "TypeError",
|
||||
"evalue": "object of type 'NoneType' has no len()",
|
||||
"output_type": "error",
|
||||
"traceback": [
|
||||
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
|
||||
"\u001b[0;31mTypeError\u001b[0m Traceback (most recent call last)",
|
||||
"\u001b[1;32m/mnt/code/libraries/testprocessing.ipynb Cell 12\u001b[0m line \u001b[0;36m1\n\u001b[0;32m----> <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X14sdnNjb2RlLXJlbW90ZQ%3D%3D?line=0'>1</a>\u001b[0m autocrop(\u001b[39m\"\u001b[39;49m\u001b[39m/mnt/dataset/\u001b[39;49m\u001b[39m\"\u001b[39;49m)\n",
|
||||
"\u001b[1;32m/mnt/code/libraries/testprocessing.ipynb Cell 12\u001b[0m line \u001b[0;36m2\n\u001b[1;32m <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X14sdnNjb2RlLXJlbW90ZQ%3D%3D?line=19'>20</a>\u001b[0m \u001b[39m# print(img)\u001b[39;00m\n\u001b[1;32m <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X14sdnNjb2RlLXJlbW90ZQ%3D%3D?line=20'>21</a>\u001b[0m t1 \u001b[39m=\u001b[39m time\u001b[39m.\u001b[39mtime()\n\u001b[0;32m---> <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X14sdnNjb2RlLXJlbW90ZQ%3D%3D?line=21'>22</a>\u001b[0m autocropped \u001b[39m=\u001b[39m mf\u001b[39m.\u001b[39;49mhoughlineprocessing(img)\n\u001b[1;32m <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X14sdnNjb2RlLXJlbW90ZQ%3D%3D?line=22'>23</a>\u001b[0m tdiffs\u001b[39m.\u001b[39mappend(time\u001b[39m.\u001b[39mtime() \u001b[39m-\u001b[39m t1)\n\u001b[1;32m <a href='vscode-notebook-cell://attached-container%2B7b22636f6e7461696e65724e616d65223a222f72696c6962726172696573646576656e76227d/mnt/code/libraries/testprocessing.ipynb#X14sdnNjb2RlLXJlbW90ZQ%3D%3D?line=23'>24</a>\u001b[0m cv2\u001b[39m.\u001b[39mimwrite(datasetpath\u001b[39m+\u001b[39msubpathtoaugmentedfiles\u001b[39m+\u001b[39mfilename, autocropped)\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:1152\u001b[0m, in \u001b[0;36mhoughlineprocessing\u001b[0;34m(image)\u001b[0m\n\u001b[1;32m 1151\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mhoughlineprocessing\u001b[39m(image):\n\u001b[0;32m-> 1152\u001b[0m croppedanddeskewed, angle \u001b[39m=\u001b[39m houghlinedeskewandcrop(image)\n\u001b[1;32m 1153\u001b[0m \u001b[39m# return croppedanddeskewed\u001b[39;00m\n\u001b[1;32m 1154\u001b[0m \n\u001b[1;32m 1155\u001b[0m \n\u001b[1;32m 1156\u001b[0m \u001b[39m# postprocessed = cropclarifying(croppedanddeskewed)\u001b[39;00m\n\u001b[1;32m 1157\u001b[0m postprocessed \u001b[39m=\u001b[39m croppedanddeskewed\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:509\u001b[0m, in \u001b[0;36mhoughlinedeskewandcrop\u001b[0;34m(image)\u001b[0m\n\u001b[1;32m 503\u001b[0m croppedcanny, croppedimage, canny, ogimage, rect \u001b[39m=\u001b[39m prepimageforhoughline(image, returnrect\u001b[39m=\u001b[39m\u001b[39mTrue\u001b[39;00m) \u001b[39m## scaling and cropping occurs. need to also return the changes done\u001b[39;00m\n\u001b[1;32m 504\u001b[0m \u001b[39m# return canny, ogimage\u001b[39;00m\n\u001b[1;32m 505\u001b[0m \u001b[39m# print(canny.shape)\u001b[39;00m\n\u001b[1;32m 506\u001b[0m \u001b[39m# print(croppedogimage.shape)\u001b[39;00m\n\u001b[1;32m 507\u001b[0m \n\u001b[1;32m 508\u001b[0m \u001b[39m## -----------------finding angle to deskew-----------------\u001b[39;00m\n\u001b[0;32m--> 509\u001b[0m rotationangle \u001b[39m=\u001b[39m houghlinedeskewangle(croppedcanny)\n\u001b[1;32m 510\u001b[0m \u001b[39m# print(croppedcanny.shape)\u001b[39;00m\n\u001b[1;32m 511\u001b[0m \u001b[39m# print(abs(rotationangle))\u001b[39;00m\n\u001b[1;32m 512\u001b[0m \u001b[39mif\u001b[39;00m (croppedcanny\u001b[39m.\u001b[39mshape[\u001b[39m0\u001b[39m] \u001b[39m>\u001b[39m croppedcanny\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m]):\n",
|
||||
"File \u001b[0;32m/mnt/code/autocropper/myfunctions.py:383\u001b[0m, in \u001b[0;36mhoughlinedeskewangle\u001b[0;34m(image)\u001b[0m\n\u001b[1;32m 381\u001b[0m \u001b[39mdef\u001b[39;00m \u001b[39mhoughlinedeskewangle\u001b[39m(image):\n\u001b[1;32m 382\u001b[0m lines \u001b[39m=\u001b[39m cv2\u001b[39m.\u001b[39mHoughLines(image, \u001b[39m1\u001b[39m, np\u001b[39m.\u001b[39mpi\u001b[39m/\u001b[39m\u001b[39m180\u001b[39m, \u001b[39mint\u001b[39m(\u001b[39mmax\u001b[39m(image\u001b[39m.\u001b[39mshape[\u001b[39m0\u001b[39m], image\u001b[39m.\u001b[39mshape[\u001b[39m1\u001b[39m])\u001b[39m/\u001b[39m\u001b[39m6\u001b[39m), \u001b[39mNone\u001b[39;00m, \u001b[39m0\u001b[39m, \u001b[39m0\u001b[39m)\n\u001b[0;32m--> 383\u001b[0m angles \u001b[39m=\u001b[39m np\u001b[39m.\u001b[39mzeros(\u001b[39mlen\u001b[39;49m(lines))\n\u001b[1;32m 384\u001b[0m \u001b[39mif\u001b[39;00m lines \u001b[39mis\u001b[39;00m \u001b[39mnot\u001b[39;00m \u001b[39mNone\u001b[39;00m:\n\u001b[1;32m 385\u001b[0m \u001b[39mfor\u001b[39;00m i \u001b[39min\u001b[39;00m \u001b[39mrange\u001b[39m(\u001b[39m0\u001b[39m, \u001b[39mlen\u001b[39m(lines)):\n",
|
||||
"\u001b[0;31mTypeError\u001b[0m: object of type 'NoneType' has no len()"
|
||||
]
|
||||
}
|
||||
],
|
||||
"source": [
|
||||
"# autocrop(\"/mnt/dataset/\")"
|
||||
"autocrop(\"/mnt/dataset/\")"
|
||||
]
|
||||
}
|
||||
],
|
||||
|
||||
Loading…
Reference in New Issue
Block a user