shreyansh1347 commited on
Commit
7b928b9
1 Parent(s): 98c0f89

Upload task1.ipynb

Browse files
Files changed (1) hide show
  1. task1.ipynb +330 -0
task1.ipynb ADDED
@@ -0,0 +1,330 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ {
2
+ "cells": [
3
+ {
4
+ "cell_type": "code",
5
+ "execution_count": 25,
6
+ "id": "536c48a1",
7
+ "metadata": {},
8
+ "outputs": [],
9
+ "source": [
10
+ "#!pip install pytesseract\n",
11
+ "from PIL import Image\n",
12
+ "import pandas as pd\n",
13
+ "import pytesseract\n",
14
+ "from pytesseract import Output\n",
15
+ "import pandas as pd"
16
+ ]
17
+ },
18
+ {
19
+ "cell_type": "code",
20
+ "execution_count": 26,
21
+ "id": "611fd576-62e6-406a-83ed-6d0a8497e34d",
22
+ "metadata": {},
23
+ "outputs": [],
24
+ "source": [
25
+ "#!pip install pyarrow"
26
+ ]
27
+ },
28
+ {
29
+ "cell_type": "code",
30
+ "execution_count": 27,
31
+ "id": "f97b4939",
32
+ "metadata": {},
33
+ "outputs": [],
34
+ "source": [
35
+ "df = pd.read_parquet('./testing.parquet')"
36
+ ]
37
+ },
38
+ {
39
+ "cell_type": "code",
40
+ "execution_count": 28,
41
+ "id": "afd89e19-9348-414e-a951-4e36dfa3fb60",
42
+ "metadata": {},
43
+ "outputs": [
44
+ {
45
+ "data": {
46
+ "text/html": [
47
+ "<div>\n",
48
+ "<style scoped>\n",
49
+ " .dataframe tbody tr th:only-of-type {\n",
50
+ " vertical-align: middle;\n",
51
+ " }\n",
52
+ "\n",
53
+ " .dataframe tbody tr th {\n",
54
+ " vertical-align: top;\n",
55
+ " }\n",
56
+ "\n",
57
+ " .dataframe thead th {\n",
58
+ " text-align: right;\n",
59
+ " }\n",
60
+ "</style>\n",
61
+ "<table border=\"1\" class=\"dataframe\">\n",
62
+ " <thead>\n",
63
+ " <tr style=\"text-align: right;\">\n",
64
+ " <th></th>\n",
65
+ " <th>image</th>\n",
66
+ " <th>ocr_annotation_texts</th>\n",
67
+ " <th>image_height</th>\n",
68
+ " <th>image_width</th>\n",
69
+ " </tr>\n",
70
+ " </thead>\n",
71
+ " <tbody>\n",
72
+ " <tr>\n",
73
+ " <th>0</th>\n",
74
+ " <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
75
+ " <td>71 2 84 11 \\n43 7 57 9 PROJECT BRIEF\\n14 11 19...</td>\n",
76
+ " <td>1000</td>\n",
77
+ " <td>762</td>\n",
78
+ " </tr>\n",
79
+ " <tr>\n",
80
+ " <th>1</th>\n",
81
+ " <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
82
+ " <td>3 3 11 10 B&amp;W\\n77 3 87 10 QUALITY\\n15 4 74 9 Q...</td>\n",
83
+ " <td>1000</td>\n",
84
+ " <td>762</td>\n",
85
+ " </tr>\n",
86
+ " <tr>\n",
87
+ " <th>2</th>\n",
88
+ " <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
89
+ " <td>12 11 15 13 TO:\\n24 11 34 13 R. B. SPELL\\n64 1...</td>\n",
90
+ " <td>1000</td>\n",
91
+ " <td>754</td>\n",
92
+ " </tr>\n",
93
+ " <tr>\n",
94
+ " <th>3</th>\n",
95
+ " <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
96
+ " <td>28 6 73 9 SPORTS MARKETING ENTERPRISES DOCUMEN...</td>\n",
97
+ " <td>1000</td>\n",
98
+ " <td>795</td>\n",
99
+ " </tr>\n",
100
+ " <tr>\n",
101
+ " <th>4</th>\n",
102
+ " <td>b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\...</td>\n",
103
+ " <td>18 8 25 9 S.P. Zolot\\n2 8 5 9 TO:\\n60 8 73 10 ...</td>\n",
104
+ " <td>1000</td>\n",
105
+ " <td>754</td>\n",
106
+ " </tr>\n",
107
+ " </tbody>\n",
108
+ "</table>\n",
109
+ "</div>"
110
+ ],
111
+ "text/plain": [
112
+ " image \\\n",
113
+ "0 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n",
114
+ "1 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n",
115
+ "2 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n",
116
+ "3 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n",
117
+ "4 b'\\x89PNG\\r\\n\\x1a\\n\\x00\\x00\\x00\\rIHDR\\x00\\x00\\... \n",
118
+ "\n",
119
+ " ocr_annotation_texts image_height \\\n",
120
+ "0 71 2 84 11 \\n43 7 57 9 PROJECT BRIEF\\n14 11 19... 1000 \n",
121
+ "1 3 3 11 10 B&W\\n77 3 87 10 QUALITY\\n15 4 74 9 Q... 1000 \n",
122
+ "2 12 11 15 13 TO:\\n24 11 34 13 R. B. SPELL\\n64 1... 1000 \n",
123
+ "3 28 6 73 9 SPORTS MARKETING ENTERPRISES DOCUMEN... 1000 \n",
124
+ "4 18 8 25 9 S.P. Zolot\\n2 8 5 9 TO:\\n60 8 73 10 ... 1000 \n",
125
+ "\n",
126
+ " image_width \n",
127
+ "0 762 \n",
128
+ "1 762 \n",
129
+ "2 754 \n",
130
+ "3 795 \n",
131
+ "4 754 "
132
+ ]
133
+ },
134
+ "execution_count": 28,
135
+ "metadata": {},
136
+ "output_type": "execute_result"
137
+ }
138
+ ],
139
+ "source": [
140
+ "df.head()"
141
+ ]
142
+ },
143
+ {
144
+ "cell_type": "code",
145
+ "execution_count": 29,
146
+ "id": "22dc4078-3524-4a32-9f72-1f9e8e3588ca",
147
+ "metadata": {},
148
+ "outputs": [],
149
+ "source": [
150
+ "import numpy as np\n",
151
+ "import io\n",
152
+ "import cv2"
153
+ ]
154
+ },
155
+ {
156
+ "cell_type": "code",
157
+ "execution_count": 30,
158
+ "id": "68630cb9",
159
+ "metadata": {},
160
+ "outputs": [],
161
+ "source": [
162
+ "pytesseract.pytesseract.tesseract_cmd = r'/opt/homebrew/bin/tesseract'\n",
163
+ "\n",
164
+ "\n",
165
+ "\n",
166
+ "class ocr:\n",
167
+ " def __init__(self, df):\n",
168
+ " self.df = df\n",
169
+ " def get_data(self, n):\n",
170
+ " image = Image.open(io.BytesIO(self.df.iloc(0)[n]['image']))\n",
171
+ " image_height = self.df.iloc(0)[n]['image_height']\n",
172
+ " image_width = self.df.iloc(0)[n]['image_width']\n",
173
+ " image_data = pytesseract.image_to_data(image, output_type=Output.DICT)\n",
174
+ " txt = ''\n",
175
+ " \n",
176
+ " for i in range(len(image_data['level'])):\n",
177
+ " if image_data['text'][i] == '':\n",
178
+ " continue\n",
179
+ " txt += image_data['text'][i]\n",
180
+ " txt += ' ' + str(image_data['left'][i]) + ' ' + str(image_data['top'][i]) + ' ' + str(image_data['width'][i]) + ' ' + str(image_data['height'][i]) + ' '\n",
181
+ " return image, image_height, image_width, image_data, txt\n",
182
+ " def sort_bounding_boxes(self, img_data, img_height, img_width):\n",
183
+ " n_boxes = len(img_data['text'])\n",
184
+ " if n_boxes == 0:\n",
185
+ " return []\n",
186
+ " #print(n_boxes)\n",
187
+ " boxes = []\n",
188
+ " for i in range(n_boxes):\n",
189
+ " box = {\n",
190
+ " 'index': i,\n",
191
+ " 'text': img_data['text'][i],\n",
192
+ " 'left': img_data['left'][i],\n",
193
+ " 'top': img_data['top'][i],\n",
194
+ " 'width': img_data['width'][i],\n",
195
+ " 'height': img_data['height'][i]\n",
196
+ " }\n",
197
+ " boxes.append(box)\n",
198
+ " \n",
199
+ " \n",
200
+ " sorted_boxes = sorted(boxes, key=lambda x: (x['top'], x['left']))\n",
201
+ " \n",
202
+ " final_boxes = []\n",
203
+ " for i in range(len(sorted_boxes)):\n",
204
+ " if (\n",
205
+ " sorted_boxes[i]['left'] >= img_width * 0.9\n",
206
+ " or sorted_boxes[i]['top'] >= img_height * 0.9\n",
207
+ " ):\n",
208
+ " continue\n",
209
+ " else:\n",
210
+ " final_boxes.append(sorted_boxes[i])\n",
211
+ " return final_boxes\n",
212
+ " def ocr_parse(self, img_data, img_height, img_width, width_threshold_percent=2, height_threshold_percent=1):\n",
213
+ " parsed_boxes = []\n",
214
+ "\n",
215
+ " if not img_data:\n",
216
+ " return parsed_boxes\n",
217
+ "\n",
218
+ " current_box = img_data[0]\n",
219
+ " img_width = max(current_box['left'] + current_box['width'], 1)\n",
220
+ " img_height = max(current_box['top'] + current_box['height'], 1)\n",
221
+ " current_text = current_box['text']\n",
222
+ "\n",
223
+ " for i in range(1, len(img_data)):\n",
224
+ " width_threshold = img_width * width_threshold_percent / 100\n",
225
+ " height_threshold = img_height * height_threshold_percent / 100\n",
226
+ "\n",
227
+ " if (\n",
228
+ " img_data[i]['left'] - (current_box['left'] + current_box['width']) <= width_threshold\n",
229
+ " and abs(img_data[i]['top'] - current_box['top']) <= height_threshold\n",
230
+ " ):\n",
231
+ " current_box['width'] = img_data[i]['left'] + img_data[i]['width'] - current_box['left']\n",
232
+ " current_box['height'] = max(current_box['height'], img_data[i]['top'] + img_data[i]['height'] - current_box['top'])\n",
233
+ " current_text += ' ' + img_data[i]['text']\n",
234
+ " else:\n",
235
+ " current_box['text'] = current_text\n",
236
+ " parsed_boxes.append(current_box)\n",
237
+ " current_box = img_data[i]\n",
238
+ " current_text = current_box['text']\n",
239
+ "\n",
240
+ " current_box['text'] = current_text\n",
241
+ " parsed_boxes.append(current_box)\n",
242
+ "\n",
243
+ " return parsed_boxes\n",
244
+ " def view(self, n):\n",
245
+ " image, img_height, img_width, img_data, text = self.get_data(n)\n",
246
+ " img_str = self.df.iloc(0)[n]['image']\n",
247
+ " nparr = np.fromstring(img_str, np.uint8)\n",
248
+ " img_np = cv2.imdecode(nparr, flags=1)\n",
249
+ " \n",
250
+ " n_boxes = len(img_data['level'])\n",
251
+ " for i in range(n_boxes):\n",
252
+ " (x, y, w, h) = (img_data['left'][i], img_data['top'][i], img_data['width'][i], img_data['height'][i])\n",
253
+ " \n",
254
+ " sorted_boxes = self.sort_bounding_boxes(img_data, img_height, img_width)\n",
255
+ " parsed_boxes = self.ocr_parse(sorted_boxes, img_width, img_height)\n",
256
+ " \n",
257
+ " for box in parsed_boxes:\n",
258
+ " (x, y, w, h) = (box['left'], box['top'], box['width'], box['height'])\n",
259
+ " cv2.rectangle(img_np, (x, y), (x + w, y + h), (0, 255, 0), 2)\n",
260
+ " cv2.imwrite('result.png', img_np)\n",
261
+ " txt = ''\n",
262
+ " for j in range(len(parsed_boxes)):\n",
263
+ " if parsed_boxes[j]['text'] == '':\n",
264
+ " continue\n",
265
+ " txt += parsed_boxes[j]['text'].strip(' ')\n",
266
+ " txt += ' ' + str(parsed_boxes[j]['left']) + ' ' + str(parsed_boxes[j]['top']) + ' ' + str(parsed_boxes[j]['width']) + ' ' + str(parsed_boxes[j]['height']) + ' '\n",
267
+ " print(txt)\n",
268
+ " return txt"
269
+ ]
270
+ },
271
+ {
272
+ "cell_type": "code",
273
+ "execution_count": 31,
274
+ "id": "1a66c36d-f835-467e-97d9-597c288280b5",
275
+ "metadata": {},
276
+ "outputs": [
277
+ {
278
+ "name": "stdout",
279
+ "output_type": "stream",
280
+ "text": [
281
+ " 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"
282
+ ]
283
+ },
284
+ {
285
+ "name": "stderr",
286
+ "output_type": "stream",
287
+ "text": [
288
+ "/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",
289
+ " nparr = np.fromstring(img_str, np.uint8)\n"
290
+ ]
291
+ }
292
+ ],
293
+ "source": [
294
+ "ocr_obj = ocr(df)\n",
295
+ "text = ocr_obj.view(0)"
296
+ ]
297
+ },
298
+ {
299
+ "cell_type": "code",
300
+ "execution_count": null,
301
+ "id": "0cb71096",
302
+ "metadata": {},
303
+ "outputs": [],
304
+ "source": [
305
+ "text"
306
+ ]
307
+ }
308
+ ],
309
+ "metadata": {
310
+ "kernelspec": {
311
+ "display_name": "Python 3 (ipykernel)",
312
+ "language": "python",
313
+ "name": "python3"
314
+ },
315
+ "language_info": {
316
+ "codemirror_mode": {
317
+ "name": "ipython",
318
+ "version": 3
319
+ },
320
+ "file_extension": ".py",
321
+ "mimetype": "text/x-python",
322
+ "name": "python",
323
+ "nbconvert_exporter": "python",
324
+ "pygments_lexer": "ipython3",
325
+ "version": "3.11.5"
326
+ }
327
+ },
328
+ "nbformat": 4,
329
+ "nbformat_minor": 5
330
+ }