File size: 6,410 Bytes
144dfb5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "# import the necessary packages\n",
    "from imutils.perspective import four_point_transform\n",
    "import pytesseract\n",
    "import argparse\n",
    "import imutils\n",
    "import cv2\n",
    "import re"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "usage: ipykernel_launcher.py [-h] -i INPUT [-d DEBUG]\n",
      "ipykernel_launcher.py: error: the following arguments are required: -i/--input\n"
     ]
    },
    {
     "ename": "SystemExit",
     "evalue": "2",
     "output_type": "error",
     "traceback": [
      "An exception has occurred, use %tb to see the full traceback.\n",
      "\u001b[1;31mSystemExit\u001b[0m\u001b[1;31m:\u001b[0m 2\n"
     ]
    }
   ],
   "source": [
    "input= \"sample_711.jpg\"\n",
    "\n",
    "# Construct the argument parser\n",
    "ap = argparse.ArgumentParser()\n",
    "ap.add_argument(\"-i\", \"--input\", required=True,\n",
    "\thelp=\"path to input receipt image\")\n",
    "ap.add_argument(\"-d\", \"--debug\", type=int, default=-1,\n",
    "\thelp=\"whether or not we are visualizing each step of the pipeline\")\n",
    "\n",
    "# Parse the arguments\n",
    "args = vars(ap.parse_args())\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "ename": "NameError",
     "evalue": "name 'args' is not defined",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mNameError\u001b[0m                                 Traceback (most recent call last)",
      "Cell \u001b[1;32mIn[14], line 4\u001b[0m\n\u001b[0;32m      1\u001b[0m \u001b[38;5;66;03m# load the input image from disk, resize it, and compute the ratio\u001b[39;00m\n\u001b[0;32m      2\u001b[0m \u001b[38;5;66;03m# of the *new* width to the *old* width\u001b[39;00m\n\u001b[0;32m      3\u001b[0m image\u001b[38;5;241m=\u001b[39m \u001b[38;5;124m\"\u001b[39m\u001b[38;5;124msample_711.jpg\u001b[39m\u001b[38;5;124m\"\u001b[39m\n\u001b[1;32m----> 4\u001b[0m orig \u001b[38;5;241m=\u001b[39m cv2\u001b[38;5;241m.\u001b[39mimread(\u001b[43margs\u001b[49m[\u001b[38;5;124m\"\u001b[39m\u001b[38;5;124mimage\u001b[39m\u001b[38;5;124m\"\u001b[39m])\n\u001b[0;32m      5\u001b[0m image \u001b[38;5;241m=\u001b[39m orig\u001b[38;5;241m.\u001b[39mcopy()\n\u001b[0;32m      6\u001b[0m image \u001b[38;5;241m=\u001b[39m imutils\u001b[38;5;241m.\u001b[39mresize(image, width\u001b[38;5;241m=\u001b[39m\u001b[38;5;241m500\u001b[39m)\n",
      "\u001b[1;31mNameError\u001b[0m: name 'args' is not defined"
     ]
    }
   ],
   "source": [
    "# load the input image from disk, resize it, and compute the ratio\n",
    "# of the *new* width to the *old* width\n",
    "image= \"sample_711.jpg\"\n",
    "orig = cv2.imread(args[\"image\"])\n",
    "image = orig.copy()\n",
    "image = imutils.resize(image, width=500)\n",
    "ratio = orig.shape[1] / float(image.shape[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# convert the image to grayscale, blur it slightly, and then apply\n",
    "# edge detection\n",
    "gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)\n",
    "blurred = cv2.GaussianBlur(gray, (5, 5,), 0)\n",
    "edged = cv2.Canny(blurred, 75, 200)\n",
    "# check to see if we should show the output of our edge detection\n",
    "# procedure\n",
    "if args[\"debug\"] > 0:\n",
    "\tcv2.imshow(\"Input\", image)\n",
    "\tcv2.imshow(\"Edged\", edged)\n",
    "\tcv2.waitKey(0)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# find contours in the edge map and sort them by size in descending\n",
    "# order\n",
    "cnts = cv2.findContours(edged.copy(), cv2.RETR_EXTERNAL,\n",
    "\tcv2.CHAIN_APPROX_SIMPLE)\n",
    "cnts = imutils.grab_contours(cnts)\n",
    "cnts = sorted(cnts, key=cv2.contourArea, reverse=True)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize a contour that corresponds to the receipt outline\n",
    "receiptCnt = None\n",
    "# loop over the contours\n",
    "for c in cnts:\n",
    "\t# approximate the contour\n",
    "\tperi = cv2.arcLength(c, True)\n",
    "\tapprox = cv2.approxPolyDP(c, 0.02 * peri, True)\n",
    "\t# if our approximated contour has four points, then we can\n",
    "\t# assume we have found the outline of the receipt\n",
    "\tif len(approx) == 4:\n",
    "\t\treceiptCnt = approx\n",
    "\t\tbreak\n",
    "# if the receipt contour is empty then our script could not find the\n",
    "# outline and we should be notified\n",
    "if receiptCnt is None:\n",
    "\traise Exception((\"Could not find receipt outline. \"\n",
    "\t\t\"Try debugging your edge detection and contour steps.\"))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# check to see if we should draw the contour of the receipt on the\n",
    "# image and then display it to our screen\n",
    "if args[\"debug\"] > 0:\n",
    "\toutput = image.copy()\n",
    "\tcv2.drawContours(output, [receiptCnt], -1, (0, 255, 0), 2)\n",
    "\tcv2.imshow(\"Receipt Outline\", output)\n",
    "\tcv2.waitKey(0)\n",
    "# apply a four-point perspective transform to the *original* image to\n",
    "# obtain a top-down bird's-eye view of the receipt\n",
    "receipt = four_point_transform(orig, receiptCnt.reshape(4, 2) * ratio)\n",
    "# show transformed image\n",
    "cv2.imshow(\"Receipt Transform\", imutils.resize(receipt, width=500))\n",
    "cv2.waitKey(0)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mlenv",
   "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.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}