{ "cells": [ { "cell_type": "code", "execution_count": 25, "id": "536c48a1", "metadata": {}, "outputs": [], "source": [ "#!pip install pytesseract\n", "from PIL import Image\n", "import pandas as pd\n", "import pytesseract\n", "from pytesseract import Output\n", "import pandas as pd" ] }, { "cell_type": "code", "execution_count": 26, "id": "611fd576-62e6-406a-83ed-6d0a8497e34d", "metadata": {}, "outputs": [], "source": [ "#!pip install pyarrow" ] }, { "cell_type": "code", "execution_count": 27, "id": "f97b4939", "metadata": {}, "outputs": [], "source": [ "df = pd.read_parquet('./testing.parquet')" ] }, { "cell_type": "code", "execution_count": 28, "id": "afd89e19-9348-414e-a951-4e36dfa3fb60", "metadata": {}, "outputs": [ { "data": { "text/html": [ "
\n", "\n", "\n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", " \n", "
imageocr_annotation_textsimage_heightimage_width
0b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...71 2 84 11 \\n43 7 57 9 PROJECT BRIEF\\n14 11 19...1000762
1b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...3 3 11 10 B&W\\n77 3 87 10 QUALITY\\n15 4 74 9 Q...1000762
2b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...12 11 15 13 TO:\\n24 11 34 13 R. B. SPELL\\n64 1...1000754
3b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...28 6 73 9 SPORTS MARKETING ENTERPRISES DOCUMEN...1000795
4b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...18 8 25 9 S.P. Zolot\\n2 8 5 9 TO:\\n60 8 73 10 ...1000754
\n", "
" ], "text/plain": [ " image \\\n", "0 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n", "1 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n", "2 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n", "3 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n", "4 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n", "\n", " ocr_annotation_texts image_height \\\n", "0 71 2 84 11 \\n43 7 57 9 PROJECT BRIEF\\n14 11 19... 1000 \n", "1 3 3 11 10 B&W\\n77 3 87 10 QUALITY\\n15 4 74 9 Q... 1000 \n", "2 12 11 15 13 TO:\\n24 11 34 13 R. B. SPELL\\n64 1... 1000 \n", "3 28 6 73 9 SPORTS MARKETING ENTERPRISES DOCUMEN... 1000 \n", "4 18 8 25 9 S.P. Zolot\\n2 8 5 9 TO:\\n60 8 73 10 ... 1000 \n", "\n", " image_width \n", "0 762 \n", "1 762 \n", "2 754 \n", "3 795 \n", "4 754 " ] }, "execution_count": 28, "metadata": {}, "output_type": "execute_result" } ], "source": [ "df.head()" ] }, { "cell_type": "code", "execution_count": 29, "id": "22dc4078-3524-4a32-9f72-1f9e8e3588ca", "metadata": {}, "outputs": [], "source": [ "import numpy as np\n", "import io\n", "import cv2" ] }, { "cell_type": "code", "execution_count": 30, "id": "68630cb9", "metadata": {}, "outputs": [], "source": [ "pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'\n", "\n", "\n", "\n", "class ocr:\n", " def __init__(self, df):\n", " self.df = df\n", " def get_data(self, n):\n", " image = Image.open(io.BytesIO(self.df.iloc(0)[n]['image']))\n", " image_height = self.df.iloc(0)[n]['image_height']\n", " image_width = self.df.iloc(0)[n]['image_width']\n", " image_data = pytesseract.image_to_data(image, output_type=Output.DICT)\n", " txt = ''\n", " \n", " for i in range(len(image_data['level'])):\n", " if image_data['text'][i] == '':\n", " continue\n", " txt += image_data['text'][i]\n", " txt += ' ' + str(image_data['left'][i]) + ' ' + str(image_data['top'][i]) + ' ' + str(image_data['width'][i]) + ' ' + str(image_data['height'][i]) + ' '\n", " return image, image_height, image_width, image_data, txt\n", " def sort_bounding_boxes(self, img_data, img_height, img_width):\n", " n_boxes = len(img_data['text'])\n", " if n_boxes == 0:\n", " return []\n", " #print(n_boxes)\n", " boxes = []\n", " for i in range(n_boxes):\n", " box = {\n", " 'index': i,\n", " 'text': img_data['text'][i],\n", " 'left': img_data['left'][i],\n", " 'top': img_data['top'][i],\n", " 'width': img_data['width'][i],\n", " 'height': img_data['height'][i]\n", " }\n", " boxes.append(box)\n", " \n", " \n", " sorted_boxes = sorted(boxes, key=lambda x: (x['top'], x['left']))\n", " \n", " final_boxes = []\n", " for i in range(len(sorted_boxes)):\n", " if (\n", " sorted_boxes[i]['left'] >= img_width * 0.9\n", " or sorted_boxes[i]['top'] >= img_height * 0.9\n", " ):\n", " continue\n", " else:\n", " final_boxes.append(sorted_boxes[i])\n", " return final_boxes\n", " def ocr_parse(self, img_data, img_height, img_width, width_threshold_percent=2, height_threshold_percent=1):\n", " parsed_boxes = []\n", "\n", " if not img_data:\n", " return parsed_boxes\n", "\n", " current_box = img_data[0]\n", " img_width = max(current_box['left'] + current_box['width'], 1)\n", " img_height = max(current_box['top'] + current_box['height'], 1)\n", " current_text = current_box['text']\n", "\n", " for i in range(1, len(img_data)):\n", " width_threshold = img_width * width_threshold_percent / 100\n", " height_threshold = img_height * height_threshold_percent / 100\n", "\n", " if (\n", " img_data[i]['left'] - (current_box['left'] + current_box['width']) <= width_threshold\n", " and abs(img_data[i]['top'] - current_box['top']) <= height_threshold\n", " ):\n", " current_box['width'] = img_data[i]['left'] + img_data[i]['width'] - current_box['left']\n", " current_box['height'] = max(current_box['height'], img_data[i]['top'] + img_data[i]['height'] - current_box['top'])\n", " current_text += ' ' + img_data[i]['text']\n", " else:\n", " current_box['text'] = current_text\n", " parsed_boxes.append(current_box)\n", " current_box = img_data[i]\n", " current_text = current_box['text']\n", "\n", " current_box['text'] = current_text\n", " parsed_boxes.append(current_box)\n", "\n", " return parsed_boxes\n", " def view(self, n):\n", " image, img_height, img_width, img_data, text = self.get_data(n)\n", " img_str = self.df.iloc(0)[n]['image']\n", " nparr = np.fromstring(img_str, np.uint8)\n", " img_np = cv2.imdecode(nparr, flags=1)\n", " \n", " n_boxes = len(img_data['level'])\n", " for i in range(n_boxes):\n", " (x, y, w, h) = (img_data['left'][i], img_data['top'][i], img_data['width'][i], img_data['height'][i])\n", " \n", " sorted_boxes = self.sort_bounding_boxes(img_data, img_height, img_width)\n", " parsed_boxes = self.ocr_parse(sorted_boxes, img_width, img_height)\n", " \n", " for box in parsed_boxes:\n", " (x, y, w, h) = (box['left'], box['top'], box['width'], box['height'])\n", " cv2.rectangle(img_np, (x, y), (x + w, y + h), (0, 255, 0), 2)\n", " cv2.imwrite('result.png', img_np)\n", " txt = ''\n", " for j in range(len(parsed_boxes)):\n", " if parsed_boxes[j]['text'] == '':\n", " continue\n", " txt += parsed_boxes[j]['text'].strip(' ')\n", " txt += ' ' + str(parsed_boxes[j]['left']) + ' ' + str(parsed_boxes[j]['top']) + ' ' + str(parsed_boxes[j]['width']) + ' ' + str(parsed_boxes[j]['height']) + ' '\n", " print(txt)\n", " return txt" ] }, { "cell_type": "code", "execution_count": 31, "id": "1a66c36d-f835-467e-97d9-597c288280b5", "metadata": {}, "outputs": [ { "name": "stdout", "output_type": "stream", "text": [ " 0 0 718 1000 PROJECT BRIEF 329 76 107 11 DATE: 107 110 49 28 June 2, 1990 DATE: 179 110 -28 28 June 1, 195 116 49 10 1990 263 117 107 15 BRAND: 112 143 46 31 General Merchandising 178 144 174 13 ITEM: 394 147 33 10 Package Fixture Circle-K Nonspecific 452 148 -184 26 Convenient Storés 451 162 140 12 SUMMARY OF PROJECT: 110 184 155 14 See Attached 285 187 99 11 SUPPLIERS BEING CONSIDERED: 109 254 222 12 chicago show 107 280 101 71 Display 108 301 58 21 Equation 374 301 -133 21 Chicago Display 107 336 125 15 Robert Nielson & Associates 107 364 224 13 FUNDING: 106 406 64 44 1990 Customized Merchandising 107 434 240 16 Services 106 461 65 11 SPNS - 198 463 -10 9 SIGNATURE: 104 560 92 14 REQUESTING MANAGER 102 599 170 14 MERCHANDISING MANAGER 101 641 189 57 GROUP PRODUCT DIRECTOR 101 686 184 12 — DEPARTMENT PURCHASING 282 721 -99 28 — 353 729 14 2 RETURN TO: 98 771 82 14 REQUESTING 199 772 83 13 | MANAGER 376 773 -27 10 4514cbta 98 827 66 10 \n" ] }, { "name": "stderr", "output_type": "stream", "text": [ "/var/folders/fv/pf_pqm6s2ds43tstk6b5q3wm0000gn/T/ipykernel_72369/3881488818.py:86: DeprecationWarning: The binary mode of fromstring is deprecated, as it behaves surprisingly on unicode inputs. Use frombuffer instead\n", " nparr = np.fromstring(img_str, np.uint8)\n" ] } ], "source": [ "ocr_obj = ocr(df)\n", "text = ocr_obj.view(0)" ] }, { "cell_type": "code", "execution_count": null, "id": "0cb71096", "metadata": {}, "outputs": [], "source": [ "text" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3 (ipykernel)", "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.5" } }, "nbformat": 4, "nbformat_minor": 5 }