SelimGilon commited on
Commit
d4c60fd
·
1 Parent(s): 672ed4a

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +390 -0
app.py ADDED
@@ -0,0 +1,390 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import urllib
2
+
3
+ import pandas as pd
4
+ import numpy as np
5
+ from pdf2image import convert_from_path
6
+ import cv2
7
+ import pytesseract
8
+ import requests
9
+ import os
10
+ import json
11
+ from PIL import Image
12
+ import re
13
+ from roboflow import Roboflow
14
+ import gradio as gr
15
+
16
+ rf = Roboflow(api_key="TN586SQsdwjUUvMN3H8e")
17
+ project = rf.workspace().project("smartflyers")
18
+ model = project.version(9).model
19
+
20
+ # Define shops with their respective search URL and reference number regex
21
+ shops = {
22
+ 'Latulippe': {
23
+ 'url': 'https://latulippe.com/fr/recherche/?keywords=',
24
+ 'regex': r'N° (\w+)'
25
+ },
26
+ 'Decathlon': {
27
+ 'url': 'https://www.decathlon.fr/search?Ntt=',
28
+ 'regex': r'Réf. : (\d+)'
29
+ },
30
+ 'Metro' : {
31
+ 'url': 'https://www.metro.ca/en/online-grocery/search?filter=',
32
+ 'regex': r'(\d{6})'
33
+ },
34
+ 'Walmart' : {
35
+ 'url': 'https://www.walmart.ca/search?q=',
36
+ 'regex': r'(\d{6})'
37
+ },
38
+ 'Canadian Tire' : {
39
+ 'url': 'https://www.canadiantire.ca/en/search-results.html?q=',
40
+ 'regex': r'(\d{6})'
41
+ },
42
+ 'Rona' : {
43
+ 'url': 'https://www.rona.ca/en/search?searchType=product&q=',
44
+ 'regex': r'(\d{6})'
45
+ },
46
+ 'Home Depot' : {
47
+ 'url': 'https://www.homedepot.ca/en/home/search.html?q=',
48
+ 'regex': r'(\d{6})'
49
+ },
50
+ 'BMR' : {
51
+ 'url': 'https://www.bmr.co/fr/recherche.html?q=',
52
+ 'regex': r'(\d{6})'
53
+ },
54
+ 'Reno Depot' : {
55
+ 'url': 'https://www.renodepot.com/en/search#?q=',
56
+ 'regex': r'(\d{6})'
57
+ },
58
+ 'Maxi' : {
59
+ 'url': 'https://www.maxi.ca/search?search-bar=',
60
+ 'regex': r'(\d{6})'
61
+ },
62
+ 'Super C' : {
63
+ 'url': 'https://www.superc.ca/search?search-bar=',
64
+ 'regex': r'(\d{6})'
65
+ },
66
+ 'Provigo' : {
67
+ 'url': 'https://www.provigo.ca/search?search-bar=',
68
+ 'regex': r'(\d{6})'
69
+ },
70
+ 'IGA' : {
71
+ 'url': 'https://www.iga.net/en/search?t={14A91276-40BA-49D5-940D-3692DE3DC381}&k=',
72
+ 'regex': r'(\d{6})'
73
+ },
74
+ 'Jean Coutu' : {
75
+ 'url': 'https://www.jeancoutu.com/recherche?searchTerm=',
76
+ 'regex': r'(\d{6})'
77
+ },
78
+ 'Pharmaprix' : {
79
+ 'url': 'https://www1.pharmaprix.ca/search?search-bar=',
80
+ 'regex': r'(\d{6})'
81
+ },
82
+ 'Uniprix' : {
83
+ 'url': 'https://www.uniprix.com/en/search?search-bar=',
84
+ 'regex': r'(\d{6})'
85
+ },
86
+ 'Familiprix' : {
87
+ 'url': 'https://www.familiprix.com/en/search?search-bar=',
88
+ 'regex': r'(\d{6})'
89
+ },
90
+ 'SAQ' : {
91
+ 'url': 'https://www.saq.com/en/search?search-bar=',
92
+ 'regex': r'(\d{6})'
93
+ },
94
+ 'Dollarama' : {
95
+ 'url': 'https://www.dollarama.com/en-CA/search?text=',
96
+ 'regex': r'(\d{6})'
97
+ },
98
+ 'Canac' : {
99
+ 'url': 'https://www.canac.ca/en/search?search-bar=',
100
+ 'regex': r'(\d{6})'
101
+ },
102
+ 'Patrick Morin' : {
103
+ 'url': 'https://www.patrickmorin.com/en/search?search-bar=',
104
+ 'regex': r'(\d{6})'
105
+ },
106
+ 'Gagnon La Grande Quincaillerie' : {
107
+ 'url': 'https://www.gagnonfreres.com/en/search?search-bar=',
108
+ 'regex': r'(\d{6})'
109
+ },
110
+ 'Tanguay' : {
111
+ 'url': 'https://www.tanguay.ca/en/search?search-bar=',
112
+ 'regex': r'(\d{6})'
113
+ },
114
+ 'The Source' : {
115
+ 'url': 'https://www.thesource.ca/en-ca/search?text=',
116
+ 'regex': r'(\d{6})'
117
+ },
118
+ 'Best Buy' : {
119
+ 'url': 'https://www.bestbuy.ca/en-ca/search?search=',
120
+ 'regex': r'(\d{6})'
121
+ },
122
+ 'Staples' : {
123
+ 'url': 'https://www.staples.ca/search?query=',
124
+ 'regex': r'(\d{6})'
125
+ },
126
+ 'Bureau en Gros' : {
127
+ 'url': 'https://www.staples.ca/search?query=',
128
+ 'regex': r'(\d{6})'
129
+ },
130
+ 'Centre Hi-Fi' : {
131
+ 'url': 'https://www.centrehifi.com/en/search?search-bar=',
132
+ 'regex': r'(\d{6})'
133
+ },
134
+ 'Stokes' : {
135
+ 'url': 'https://www.stokesstores.com/en/search?search-bar=',
136
+ 'regex': r'(\d{6})'
137
+ },
138
+ 'RD Furniture' : {
139
+ 'url': 'https://www.raymond.ca/en/search?search-bar=',
140
+ 'regex': r'(\d{6})'
141
+ },
142
+ 'Go Sport' : {
143
+ 'url': 'https://www.gosport.ca/en/search?search-bar=',
144
+ 'regex': r'(\d{6})'
145
+ },
146
+ 'Sports Experts' : {
147
+ 'url': 'https://www.sportsexperts.ca/en-CA/search?search-bar=',
148
+ 'regex': r'(\d{6})'
149
+ },
150
+ 'Atmosphere' : {
151
+ 'url': 'https://www.atmosphere.ca/search?search-bar=',
152
+ 'regex': r'(\d{6})'
153
+ },
154
+ 'SAIL' : {
155
+ 'url': 'https://www.sail.ca/en/search?search-bar=',
156
+ 'regex': r'(\d{6})'
157
+ },
158
+ 'MEC' : {
159
+ 'url': 'https://www.mec.ca/en/search?text=',
160
+ 'regex': r'(\d{6})'
161
+ },
162
+ 'Toys R Us' : {
163
+ 'url': 'https://www.toysrus.ca/en/search?search-bar=',
164
+ 'regex': r'(\d{6})'
165
+ },
166
+ # Add more shops and their respective information here
167
+ }
168
+
169
+ def pdf_to_images(pdf_path):
170
+ images = convert_from_path(pdf_path)
171
+ return [cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR) for img in images]
172
+
173
+
174
+ def get_yolo_predictions(image):
175
+ # Convert image to suitable format if required
176
+ # Use Roboflow API to get predictions
177
+ predictions = model.predict(image, confidence=0.20, overlap=50).json()
178
+ return predictions
179
+
180
+ def display_bbox_and_extract_text(image, predictions, page_number):
181
+ texts = [] # Store OCR text for each bbox
182
+ labeled_image = image.copy() # Copy the image to draw labels on it
183
+ font = cv2.FONT_HERSHEY_SIMPLEX
184
+ font_scale = 1 # Adjust the scale according to your image
185
+ color = (255, 255, 255) # White color for text
186
+ bg_color = (0, 0, 0) # Black background for text for visibility
187
+ thickness = 10 # Thickness of the bbox outline
188
+ text_thickness = 3 # Thickness of the font
189
+
190
+ for idx, pred in enumerate(predictions["predictions"]):
191
+ x_center = int(pred["x"])
192
+ y_center = int(pred["y"])
193
+ x = int(x_center - (pred["width"] / 2))
194
+ y = int(y_center - (pred["height"] / 2))
195
+ w = int(pred["width"])
196
+ h = int(pred["height"])
197
+
198
+ # Cropping the image to the bounding box
199
+ sub_image = image[y:y + h, x:x + w]
200
+
201
+ # Extract text using OCR
202
+ text = ocr(sub_image)
203
+ texts.append(text)
204
+
205
+ # Draw the bounding box on the image
206
+ start_point = (x, y)
207
+ end_point = (x + w, y + h)
208
+ bbox_color = (0, 255, 0) # Green color in BGR for bbox
209
+ labeled_image = cv2.rectangle(labeled_image, start_point, end_point, bbox_color, thickness)
210
+
211
+ # Put the label number near the bounding box with background for visibility
212
+ label_text = str(idx + 1)
213
+ label_size = cv2.getTextSize(label_text, font, font_scale, text_thickness)[0]
214
+ label_position = (x, y + label_size[1] + 10) if y > label_size[1] else (x, y + label_size[1] + 20)
215
+
216
+ # Draw background rectangle for text
217
+ label_bg_start = (label_position[0], label_position[1] - label_size[1] - 10)
218
+ label_bg_end = (label_position[0] + label_size[0], label_position[1] + 10)
219
+ labeled_image = cv2.rectangle(labeled_image, label_bg_start, label_bg_end, bg_color, -1)
220
+
221
+ # Put the label number on the image
222
+ labeled_image = cv2.putText(labeled_image, label_text, label_position, font, font_scale, color, text_thickness)
223
+
224
+ # Draw the page number on the image
225
+ page_text = f"Page {page_number}"
226
+ page_text_size = cv2.getTextSize(page_text, font, font_scale, text_thickness)[0]
227
+ page_text_position = (image.shape[1] - page_text_size[0] - 20, page_text_size[1] + 20) # top-right corner
228
+ col = (0, 0, 0) # Black color in BGR for page number
229
+ labeled_image = cv2.putText(labeled_image, page_text, page_text_position, font, font_scale, col, text_thickness)
230
+
231
+ # Convert the labeled image from BGR to RGB for display
232
+ labeled_image = cv2.cvtColor(labeled_image, cv2.COLOR_BGR2RGB)
233
+ return labeled_image, texts
234
+
235
+
236
+ def extract_reference_number(text):
237
+ # Look for "N°" followed by the reference number pattern
238
+ match = re.search(r'N°\s*([A-Za-z0-9]+)', text)
239
+ if match:
240
+ # Return the found reference number
241
+ return match.group(1)
242
+ else:
243
+ # Return None if no reference number is found
244
+ return None
245
+
246
+ def ocr(sub_image):
247
+ # Use OCR to get text from image
248
+ text = pytesseract.image_to_string(sub_image, lang='eng')
249
+ return text
250
+
251
+
252
+ # Set up your Google API key and Custom Search Engine ID
253
+ GOOGLE_API_KEY = 'AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0'
254
+ GOOGLE_CSE_ID = '32784ecc8a6bd4fad'
255
+
256
+
257
+ def search_google(query):
258
+ search_url = "https://www.googleapis.com/customsearch/v1"
259
+ params = {
260
+ 'q': query,
261
+ 'key': GOOGLE_API_KEY,
262
+ 'cx': GOOGLE_CSE_ID,
263
+ 'num': 1 # Number of search results to return
264
+ }
265
+
266
+ response = requests.get(search_url, params=params)
267
+ search_results = response.json()
268
+ print(json.dumps(search_results, indent=2))
269
+
270
+ # Extract the first search result URL
271
+ url = search_results['items'][0]['link'] if 'items' in search_results else None
272
+ return url
273
+
274
+ def search_latulippe(query):
275
+ # URLEncode the query to ensure it is HTTP-compliant.
276
+ encoded_query = urllib.parse.quote(query)
277
+ # Construct the search URL.
278
+ search_url = f"https://latulippe.com/fr/recherche/?keywords={encoded_query}"
279
+ return search_url
280
+
281
+
282
+ def create_search_url(shop_url, query):
283
+ # URLEncode the query to ensure it is HTTP-compliant.
284
+ encoded_query = urllib.parse.quote(query)
285
+ # Construct the search URL.
286
+ if '{}' in shop_url:
287
+ # The user provided a format string with a placeholder for the query
288
+ search_url = shop_url.format(encoded_query)
289
+ else:
290
+ # Append the query to the base URL
291
+ search_url = f"{shop_url}{encoded_query}"
292
+ return search_url
293
+
294
+
295
+ def clean_search_query(text):
296
+ # Add "Latulippe" to the beginning
297
+ text = "Latulippe " + text
298
+ # Keep only the first sentence by finding the first period followed by a space or the end of the text
299
+ first_sentence = re.split(r'(?<=[.!?]) +', text, maxsplit=1)[0]
300
+ # Convert to lowercase
301
+ first_sentence = first_sentence.lower()
302
+ # Replace newline characters with spaces
303
+ first_sentence = first_sentence.replace("\n", " ")
304
+ # Remove special characters except for basic punctuation
305
+ first_sentence = re.sub(r'[^a-z0-9\s,.]', '', first_sentence)
306
+ # Optionally, remove multiple consecutive spaces
307
+ first_sentence = re.sub(r'\s+', ' ', first_sentence).strip()
308
+ return first_sentence
309
+
310
+ # Function to create a table from the extracted data
311
+ def create_table(data):
312
+ # Assuming data is a list of tuples or lists with the following format:
313
+ # [(page_number, object_number, reference_code, url), (...), ...]
314
+ headers = ["Page", "Object", "Reference Code", "URL"]
315
+ # Convert URL to a clickable link
316
+ processed_data = []
317
+ for row in data:
318
+ clickable_link = f'<a href="{row[3]}" target="_blank">{row[3]}</a>'
319
+ processed_data.concat((row[0], row[1], row[2], clickable_link))
320
+ return gr.Dataframe(headers=headers, values=processed_data)
321
+
322
+ def app(pdf_path, selected_shop):
323
+ if pdf_path is None:
324
+ raise gr.Error("No file uploaded. Please upload a PDF file.")
325
+
326
+ # Check if the uploaded file is a PDF
327
+ if not pdf_path.name.lower().endswith('.pdf'):
328
+ raise gr.Error("Invalid file format. Please upload a PDF file.")
329
+
330
+ if selected_shop is None:
331
+ raise gr.Error("Please select a shop.")
332
+
333
+ # Retrieve the selected shop's information
334
+ search_url = shops[selected_shop]['url']
335
+ ref_number_regex = shops[selected_shop]['regex']
336
+ table = pd.DataFrame(columns=["Page", "Object", "Reference", "URL"])
337
+ images = pdf_to_images(pdf_path)
338
+ output_images = []
339
+ all_urls = []
340
+
341
+ # Process the PDF and generate outputs if no errors are found
342
+ try:
343
+ images = pdf_to_images(pdf_path)
344
+ for page_number, img in enumerate(images, start=1):
345
+ predictions = get_yolo_predictions(img)
346
+ img_with_bbox, texts = display_bbox_and_extract_text(img, predictions, page_number)
347
+ output_images.append(img_with_bbox)
348
+
349
+ # Extract reference numbers using regex and generate search URLs
350
+ #for text in texts:
351
+ for object_number, text in enumerate(texts, start=1):
352
+ ref_numbers = re.findall(ref_number_regex, text)
353
+ for ref in ref_numbers:
354
+ search_query = search_url + urllib.parse.quote(ref)
355
+ # all_urls.append({'page': page_number, 'object':object_number, 'reference': ref, 'url': search_query})
356
+ new_row = pd.Series({
357
+ "Page": page_number,
358
+ "Object": object_number,
359
+ "Reference": ref,
360
+ "URL": search_query
361
+ })
362
+ table = pd.concat([table, pd.DataFrame([new_row])], ignore_index=True)
363
+
364
+ table_html = table.to_html(escape=False, index=False)
365
+ pass
366
+ except Exception as e:
367
+ return gr.Error(f"An error occurred: {str(e)}")
368
+
369
+ return output_images, table_html
370
+
371
+
372
+
373
+ # Gradio interface definition
374
+ iface = gr.Interface(
375
+ fn=app,
376
+ inputs=[
377
+ gr.File(label="Upload PDF"), # Specify that you expect a file
378
+ gr.Dropdown(choices=list(shops.keys()), label="Select Shop")
379
+ ],
380
+ outputs=[
381
+ gr.Gallery(label="Processed Images"), # Output for gallery
382
+ gr.HTML(label="Extracted Information") # Output for table
383
+ ],
384
+ title="Automated Hotspotting",
385
+ description="Here is a demo of an automated hotspotting tool. Upload a PDF file and select the correponding shop to get started.",
386
+ #allow_flagging="never"
387
+ )
388
+
389
+ iface.launch(share=False)
390
+