Skip to content

Commit

Permalink
Created using Colaboratory
Browse files Browse the repository at this point in the history
  • Loading branch information
Christine-DK committed Jun 12, 2023
1 parent e55d3a6 commit 60e3167
Showing 1 changed file with 61 additions and 23 deletions.
84 changes: 61 additions & 23 deletions dewarp.ipynb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
"metadata": {
"colab": {
"provenance": [],
"authorship_tag": "ABX9TyM+4GH5UCSVKCFPOrG/vL+B",
"authorship_tag": "ABX9TyPHmJGNfeOycvd7PcpO881p",
"include_colab_link": true
},
"kernelspec": {
Expand Down Expand Up @@ -797,20 +797,20 @@
" ppts = project_keypoints(pvec, keypoint_index)\n",
" return np.sum((dstpoints - ppts)**2)\n",
"\n",
" print ' initial objective is', objective(params)\n",
" print(' initial objective is', objective(params))\n",
"\n",
" if DEBUG_LEVEL >= 1:\n",
" projpts = project_keypoints(params, keypoint_index)\n",
" display = draw_correspondences(small, dstpoints, projpts)\n",
" debug_show(name, 4, 'keypoints before', display)\n",
"\n",
" print ' optimizing', len(params), 'parameters...'\n",
" print( ' optimizing', len(params), 'parameters...')\n",
" start = datetime.datetime.now()\n",
" res = scipy.optimize.minimize(objective, params,\n",
" method='Powell')\n",
" end = datetime.datetime.now()\n",
" print ' optimization took', round((end-start).total_seconds(), 2), 'sec.'\n",
" print ' final objective is', res.fun\n",
" print( ' optimization took', round((end-start).total_seconds(), 2), 'sec.')\n",
" print( ' final objective is', res.fun)\n",
" params = res.x\n",
"\n",
" if DEBUG_LEVEL >= 1:\n",
Expand All @@ -834,7 +834,7 @@
" res = scipy.optimize.minimize(objective, dims, method='Powell')\n",
" dims = res.x\n",
"\n",
" print ' got page dims', dims[0], 'x', dims[1]\n",
" print(' got page dims', dims[0], 'x', dims[1])\n",
"\n",
" return dims\n",
"\n",
Expand All @@ -847,7 +847,7 @@
" width = round_nearest_multiple(height * page_dims[0] / page_dims[1],\n",
" REMAP_DECIMATE)\n",
"\n",
" print ' output will be {}x{}'.format(width, height)\n",
" print(' output will be {}x{}'.format(width, height))\n",
"\n",
" height_small = height / REMAP_DECIMATE\n",
" width_small = width / REMAP_DECIMATE\n",
Expand Down Expand Up @@ -902,7 +902,7 @@
"def main():\n",
"\n",
" if len(sys.argv) < 2:\n",
" print 'usage:', sys.argv[0], 'IMAGE1 [IMAGE2 ...]'\n",
" print('usage:', sys.argv[0], 'IMAGE1 [IMAGE2 ...]')\n",
" sys.exit(0)\n",
"\n",
" if DEBUG_LEVEL > 0 and DEBUG_OUTPUT != 'file':\n",
Expand All @@ -917,8 +917,8 @@
" basename = os.path.basename(imgfile)\n",
" name, _ = os.path.splitext(basename)\n",
"\n",
" print 'loaded', basename, 'with size', imgsize(img),\n",
" print 'and resized to', imgsize(small)\n",
" print('loaded', basename, 'with size', imgsize(img),)\n",
" print('and resized to', imgsize(small))\n",
"\n",
" if DEBUG_LEVEL >= 3:\n",
" debug_show(name, 0.0, 'original', small)\n",
Expand All @@ -929,20 +929,20 @@
" spans = assemble_spans(name, small, pagemask, cinfo_list)\n",
"\n",
" if len(spans) < 3:\n",
" print ' detecting lines because only', len(spans), 'text spans'\n",
" print(' detecting lines because only', len(spans), 'text spans')\n",
" cinfo_list = get_contours(name, small, pagemask, 'line')\n",
" spans2 = assemble_spans(name, small, pagemask, cinfo_list)\n",
" if len(spans2) > len(spans):\n",
" spans = spans2\n",
"\n",
" if len(spans) < 1:\n",
" print 'skipping', name, 'because only', len(spans), 'spans'\n",
" print( 'skipping', name, 'because only', len(spans), 'spans')\n",
" continue\n",
"\n",
" span_points = sample_spans(small.shape, spans)\n",
"\n",
" print ' got', len(spans), 'spans',\n",
" print 'with', sum([len(pts) for pts in span_points]), 'points.'\n",
" print (' got', len(spans), 'spans',)\n",
" print ('with', sum([len(pts) for pts in span_points]), 'points.')\n",
"\n",
" corners, ycoords, xcoords = keypoints_from_samples(name, small,\n",
" pagemask,\n",
Expand All @@ -965,34 +965,72 @@
"\n",
" outfiles.append(outfile)\n",
"\n",
" print ' wrote', outfile\n",
" print\n",
" print(' wrote', outfile)\n",
" # print\n",
"\n",
" print 'to convert to PDF (requires ImageMagick):'\n",
" print ' convert -compress Group4 ' + ' '.join(outfiles) + ' output.pdf'\n",
" print('to convert to PDF (requires ImageMagick):')\n",
" print(' convert -compress Group4 ' + ' '.join(outfiles) + ' output.pdf')\n",
"\n",
"\n",
"if __name__ == '__main__':\n",
" main()"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
"base_uri": "https://localhost:8080/",
"height": 357
},
"id": "L3mRXepLyNzB",
"outputId": "0d943730-c296-45f8-9053-5dd95269bc48"
"outputId": "47bf5a57-a2ad-4cc7-c3b5-20663a4d2032"
},
"execution_count": 2,
"execution_count": 7,
"outputs": [
{
"output_type": "error",
"ename": "SyntaxError",
"ename": "AttributeError",
"evalue": "ignored",
"traceback": [
"\u001b[0;36m File \u001b[0;32m\"<ipython-input-2-cff663ea1231>\"\u001b[0;36m, line \u001b[0;32m739\u001b[0m\n\u001b[0;31m print ' initial objective is', objective(params)\u001b[0m\n\u001b[0m ^\u001b[0m\n\u001b[0;31mSyntaxError\u001b[0m\u001b[0;31m:\u001b[0m Missing parentheses in call to 'print'. Did you mean print(...)?\n"
"\u001b[0;31m---------------------------------------------------------------------------\u001b[0m",
"\u001b[0;31mAttributeError\u001b[0m Traceback (most recent call last)",
"\u001b[0;32m<ipython-input-7-565fc5e5fad4>\u001b[0m in \u001b[0;36m<cell line: 914>\u001b[0;34m()\u001b[0m\n\u001b[1;32m 913\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 914\u001b[0m \u001b[0;32mif\u001b[0m \u001b[0m__name__\u001b[0m \u001b[0;34m==\u001b[0m \u001b[0;34m'__main__'\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 915\u001b[0;31m \u001b[0mmain\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m",
"\u001b[0;32m<ipython-input-7-565fc5e5fad4>\u001b[0m in \u001b[0;36mmain\u001b[0;34m()\u001b[0m\n\u001b[1;32m 853\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 854\u001b[0m \u001b[0mimg\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mcv2\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mimread\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimgfile\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 855\u001b[0;31m \u001b[0msmall\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mresize_to_screen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimg\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 856\u001b[0m \u001b[0mbasename\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mbasename\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mimgfile\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 857\u001b[0m \u001b[0mname\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0m_\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mos\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mpath\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0msplitext\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mbasename\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;32m<ipython-input-7-565fc5e5fad4>\u001b[0m in \u001b[0;36mresize_to_screen\u001b[0;34m(src, maxw, maxh, copy)\u001b[0m\n\u001b[1;32m 236\u001b[0m \u001b[0;32mdef\u001b[0m \u001b[0mresize_to_screen\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0msrc\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmaxw\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m1280\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mmaxh\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;36m700\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mcopy\u001b[0m\u001b[0;34m=\u001b[0m\u001b[0;32mFalse\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 237\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0;32m--> 238\u001b[0;31m \u001b[0mheight\u001b[0m\u001b[0;34m,\u001b[0m \u001b[0mwidth\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0msrc\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mshape\u001b[0m\u001b[0;34m[\u001b[0m\u001b[0;34m:\u001b[0m\u001b[0;36m2\u001b[0m\u001b[0;34m]\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 239\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 240\u001b[0m \u001b[0mscl_x\u001b[0m \u001b[0;34m=\u001b[0m \u001b[0mfloat\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0mwidth\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m/\u001b[0m\u001b[0mmaxw\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n",
"\u001b[0;31mAttributeError\u001b[0m: 'NoneType' object has no attribute 'shape'"
]
}
]
},
{
"cell_type": "code",
"source": [
"import os\n",
"os.path.exists('6.jpg') # Will likely give False\n",
"os.getcwd() # This will return the current working directory"
],
"metadata": {
"id": "CS7JGeJFeo7n",
"outputId": "cf1f354c-29c2-4aaf-d720-3e73c3849d7f",
"colab": {
"base_uri": "https://localhost:8080/",
"height": 38
}
},
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"data": {
"text/plain": [
"'/content'"
],
"application/vnd.google.colaboratory.intrinsic+json": {
"type": "string"
}
},
"metadata": {},
"execution_count": 5
}
]
}
]
}

0 comments on commit 60e3167

Please sign in to comment.