{ "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 }