File size: 12,689 Bytes
7b928b9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
{
 "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": [
       "<div>\n",
       "<style scoped>\n",
       "    .dataframe tbody tr th:only-of-type {\n",
       "        vertical-align: middle;\n",
       "    }\n",
       "\n",
       "    .dataframe tbody tr th {\n",
       "        vertical-align: top;\n",
       "    }\n",
       "\n",
       "    .dataframe thead th {\n",
       "        text-align: right;\n",
       "    }\n",
       "</style>\n",
       "<table border=\"1\" class=\"dataframe\">\n",
       "  <thead>\n",
       "    <tr style=\"text-align: right;\">\n",
       "      <th></th>\n",
       "      <th>image</th>\n",
       "      <th>ocr_annotation_texts</th>\n",
       "      <th>image_height</th>\n",
       "      <th>image_width</th>\n",
       "    </tr>\n",
       "  </thead>\n",
       "  <tbody>\n",
       "    <tr>\n",
       "      <th>0</th>\n",
       "      <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
       "      <td>71 2 84 11 \\n43 7 57 9 PROJECT BRIEF\\n14 11 19...</td>\n",
       "      <td>1000</td>\n",
       "      <td>762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>1</th>\n",
       "      <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
       "      <td>3 3 11 10 B&amp;W\\n77 3 87 10 QUALITY\\n15 4 74 9 Q...</td>\n",
       "      <td>1000</td>\n",
       "      <td>762</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>2</th>\n",
       "      <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
       "      <td>12 11 15 13 TO:\\n24 11 34 13 R. B. SPELL\\n64 1...</td>\n",
       "      <td>1000</td>\n",
       "      <td>754</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>3</th>\n",
       "      <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
       "      <td>28 6 73 9 SPORTS MARKETING ENTERPRISES DOCUMEN...</td>\n",
       "      <td>1000</td>\n",
       "      <td>795</td>\n",
       "    </tr>\n",
       "    <tr>\n",
       "      <th>4</th>\n",
       "      <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
       "      <td>18 8 25 9 S.P. Zolot\\n2 8 5 9 TO:\\n60 8 73 10 ...</td>\n",
       "      <td>1000</td>\n",
       "      <td>754</td>\n",
       "    </tr>\n",
       "  </tbody>\n",
       "</table>\n",
       "</div>"
      ],
      "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
}