SelimGilon commited on
Commit
097ed17
·
1 Parent(s): 86ffd0e
Files changed (3) hide show
  1. app.py +543 -242
  2. app_file.py +401 -0
  3. app_file_colruyt.py +294 -0
app.py CHANGED
@@ -10,112 +10,114 @@ import re
10
  from roboflow import Roboflow
11
  import gradio as gr
12
  from ultralytics import YOLO
13
-
14
-
15
-
16
-
17
- with gr.Blocks() as demo:
18
- search_google = False
19
- GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
20
- GOOGLE_CSE_ID = "b536303d151b94476"
21
- # GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
22
- # GOOGLE_CSE_ID = "f3446c60c52cd4dc2"
23
- # GOOGLE_API_KEY = "AIzaSyCoBYZK4khX_W5eW-AqEXKOclUIer6CiC8"
24
- # GOOGLE_CSE_ID = "c3ba01991c1a14c7d"
25
- mode = 'local'
26
- # mode = 'rbf'
27
-
28
- if mode == 'local':
29
- model = YOLO('best-flyers-yolov8-1000img-v10.pt')
30
- else:
31
- rf = Roboflow(api_key="TN586SQsdwjUUvMN3H8e")
32
- project = rf.workspace().project("smartflyers")
33
- model = project.version(9).model
34
-
35
- # def process(selected_shop):
36
- # print(selected_shop)
37
- # return selected_shop
38
- hotspots_sku_links = []
39
- shop = None
40
- shops = {
41
- 'Latulippe': {
42
- 'url': 'https://latulippe.com/fr/recherche/?keywords=',
43
- 'regex': r'N° (M\d+)',
44
- 'multi_items': False,
45
- },
46
- 'Decathlon': {
47
- 'url': 'https://www.decathlon.fr/search?Ntt=',
48
- 'regex': r'Réf\. ?: ?([\d, ]+)',
49
- 'multi_items': True,
50
- 'brands': [
51
- "Allsix", "Aptonia", "Artengo", "Atorka", "B'Twin", "Caperlan", "Domyos", "Dreamscape",
52
- "FLX", "Forclaz", "Fouganza", "Geologic", "Geonaute", "Inesis", "Imviso", "Inovik",
53
- "Itiwit", "KOODZA", "KOROK", "Kalenji", "Kimjaly", "Kipsta", "Kuikma", "Nabaiji",
54
- "Newfeel", "Offload", "Olaian", "Opfeel", "Orao", "Oroks", "Outshock", "Oxelo",
55
- "Perfly", "Pongori", "Quechua", "Rockrider", "Sandever", "Simond", "Solognac",
56
- "Subea", "Tarmak", "Triban", "Tribord", "Urball", "Van Rysel", "Wed'Ze", "Decathlon"
57
- ]
58
- },
59
- 'Canadian Tire': {
60
- 'url': 'https://www.canadiantire.ca/en/search-results.html?q=',
61
- 'regex': r'(\d{2,3}-\d{4}-\d(?:/\d{4}-\d)?)',
62
- 'multi_items': True,
63
- },
64
- 'Walmart': {
65
- # 'url': 'https://www.walmart.ca/en/search?q=',
66
- 'url': '',
67
- 'regex': r'N°(\d+)',
68
- 'multi_items': False,
69
- },
70
- # Add more shops and their respective information here
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
71
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
72
 
73
- regex_price = r'(\d+\.?\d*) ?([€$])|([€$]) ?(\d+\.?\d*)'
74
-
75
-
76
- # def app(img, selected_shop):
77
- # if img is None:
78
- # raise gr.Error("No file uploaded. Please upload an image.")
79
- #
80
- # # Check if the uploaded file is a PDF
81
- # if selected_shop is None:
82
- # raise gr.Error("Please select a shop.")
83
- #
84
- # try:
85
- # image_annotated = section(img)
86
- # pass
87
- # except Exception as e:
88
- # return gr.Error(f"An error occurred: {str(e)}")
89
- #
90
- # return image_annotated, hotspots_sku_links[selected_shop]
91
-
92
- def build_request(query, start=1, num=10, date_restrict=None, **params):
93
- request = {
94
- 'key': GOOGLE_API_KEY,
95
- 'q': query,
96
- 'cx': GOOGLE_CSE_ID,
97
- # 'start': start,
98
- # 'num': num,
99
- # 'dateRestrict': date_restrict,
100
- }
101
- request.update(params)
102
- return request
103
-
104
-
105
- def make_request(request):
106
- response = requests.get('https://www.googleapis.com/customsearch/v1', params=request)
107
- # print(f"Status Code: {response.status_code}") # Print status code for debugging
108
- if response.status_code != 200:
109
- raise Exception("Google Custom Search request failed with status code {}".format(response.status_code))
110
-
111
- search_results = response.json()
112
- # print(f"Response JSON: {search_results}") # Print raw response for debugging
113
- if 'items' in search_results and len(search_results['items']) > 0:
114
- first_result_url = search_results['items'][0]['link']
115
- return first_result_url
116
- else:
117
- return None
118
-
119
 
120
  with gr.Row():
121
  selected_shop = gr.Dropdown(shops.keys(), label="Shop")
@@ -124,7 +126,7 @@ with gr.Blocks() as demo:
124
  confidence_threshold = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Confidence Threshold", value=0.5)
125
 
126
  with gr.Row():
127
- img_input = gr.Image(label="Input page (image)", sources=["upload", "webcam"])
128
  img_output = gr.AnnotatedImage(label="Annotated page (image)")
129
 
130
  section_btn = gr.Button("Identify Hotspots")
@@ -132,7 +134,6 @@ with gr.Blocks() as demo:
132
  selected_section = gr.Markdown(label="Selected Hotspot")
133
  product_dataframe = gr.Dataframe(label="Products Found")
134
 
135
-
136
  def get_yolo_predictions(image, confidence_threshold):
137
  # Use Roboflow API to get predictions
138
  predictions = model.predict(image, confidence=confidence_threshold, overlap=10).json()
@@ -151,156 +152,117 @@ with gr.Blocks() as demo:
151
  text = pytesseract.image_to_string(grey, lang='eng')
152
  return text
153
 
154
- def section(img, selected_shop, confidence_threshold):
155
  hostpots = []
 
156
  sku = []
157
  product_table_data = []
158
 
159
- global hotspots_sku_links
160
- hotspots_sku_links = []
 
 
 
 
 
 
 
161
 
162
  #print("selected_shop:", selected_shop)
163
  search_url = shops[selected_shop]['url']
164
  ref_number_regex = shops[selected_shop]['regex']
165
  # print("search_url:", search_url)
166
  brands = shops[selected_shop]['brands'] if 'brands' in shops[selected_shop] else None
167
-
168
  if brands:
169
  brands = [brand.upper() for brand in brands]
170
 
171
  if mode == 'rbf':
172
- predictions = get_yolo_predictions(img)
173
  else:
174
  # local inference
175
- results = get_yolo_predictions_local(img, confidence_threshold)
176
  for result in results:
177
- coordinates = result.boxes.xyxy
178
  coordinates = coordinates.to("cpu")
179
  coordinates = coordinates.numpy()
180
  print("FOUND {} objects".format(len(coordinates)))
 
181
  for idx, coord in enumerate(coordinates):
 
182
  # crop the image to the bounding box
183
- x = int(coord[0])
184
- y = int(coord[1])
185
- w = int(coord[2] - coord[0])
186
- h = int(coord[3] - coord[1])
187
- sub_image = img[y:y + h, x:x + w]
188
- if shops[selected_shop]['url'] is None or shops[selected_shop]['url'] == "":
189
- # just append the hotspot to hostpots
190
- hostpots.append(((x, y, x + w, y + h), str(idx)))
191
- continue
192
-
193
- # Extract text using OCR
194
- text = ocr(sub_image)
195
-
196
- if selected_shop == "Latulippe":
197
- # if there is a 'i' between a "M" and a number, it is a sku. Replace the 'i' with a '1'
198
- text = re.sub(r'Mi(\d+)', r'M1\1', text)
199
-
200
- # for object_number, text in enumerate(texts, start=1):
201
- #print("object", object_number)
202
- print("text -----", text)
203
- ref_numbers = []
204
- if selected_shop == "Decathlon":
205
- full_match = re.search(ref_number_regex, text)
206
- if full_match:
207
- print("FULL MATCH: ", full_match)
208
- # Extracting all numbers from the captured sequence
209
- numbers_string = full_match.group(1)
210
- ref_numbers = re.findall(r'\d+', numbers_string)
211
- print("REF NUMBERS: ", ref_numbers)
212
- else:
213
- ref_numbers = re.findall(ref_number_regex, text)
214
- # print("REF NUMBERS: ", ref_numbers)
215
-
216
- if selected_shop == "Canadian Tire":
217
- temp_ref_numbers = ref_numbers
218
- ref_numbers = []
219
- split = False
220
- for ref_sku in temp_ref_numbers:
221
- if '/' in ref_sku:
222
- split = True
223
- sku_split = ref_sku.split('/')
224
- sku_last = sku_split[-1]
225
- length = len(sku_last)
226
- base_number = sku_split[0][:-length]
227
- ref_numbers.append(sku_split[0])
228
- # base number + sku for each sku in sku_split except the first one
229
- for i in range(1, len(sku_split)):
230
- ref_numbers.append(base_number + sku_split[i])
231
-
232
- if split == False:
233
- ref_numbers = temp_ref_numbers
234
-
235
- matches = re.findall(regex_price, text)
236
- prices = []
237
- for match in matches:
238
- if match[0]: # If the first group is not empty, the format is "price currency"
239
- prices.append(match[0] + match[1])
240
- else: # Otherwise, the format is "currency price"
241
- prices.append(match[2] + match[3])
242
- # price = prices[0] if len(prices) > 0 else ""
243
- #print("PRICES: ", prices)
244
-
245
- if brands:
246
- # print("BRANDS: yes")
247
- # Extracting the product name
248
  lines = text.split('\n')
249
- product_name = ""
250
- found_brand = False
251
- name_started = False
252
- for line in lines:
253
- if any(brand in line for brand in brands):
254
- found_brand = True
255
- #print("FOUND BRAND", line)
256
- continue
257
- if found_brand:
258
- #print('current line:', line)
259
- if line.strip() == "": # Skip blank lines
260
- continue
261
- if name_started and (line[0].isupper() or any(char.isdigit() for char in line)):
262
- break
263
- product_name += line.strip() + " "
264
- name_started = True
265
- product_name = product_name.strip()
266
-
267
- else:
268
- # Regex to match sequences of uppercase letters
269
- uppercase_words = re.findall(r'\b[A-Z]+\b', text)
270
-
271
- # Joining the extracted words with a space
272
- product_name = ' '.join(uppercase_words)
273
-
274
- #print("PRODUCT NAME: ", product_name)
275
-
276
- for ref in ref_numbers:
277
- print("ref", ref)
278
- if search_google:
279
- # Use Google Search API to find items with the SKU
280
- to_seach = str(selected_shop) + ' ' + str(ref)
281
- # print("to_search: ", to_seach)
282
- request = build_request(to_seach)
283
- search_query = make_request(request)
284
- # print(f"First Result URL: {search_query}")
285
- if search_query is None:
286
- search_query = search_url + urllib.parse.quote(ref)
287
- else:
288
- search_query = search_url + urllib.parse.quote(ref)
289
- if ref not in sku:
290
- sku.append(ref)
291
- # if search_query not in hotspots_sku_links:
292
- hotspots_sku_links.append([{'sku': ref, 'url': search_query, 'prices': prices, 'product_name': product_name}])
293
- hostpots.append(((int(coord[0]), int(coord[1]), int(coord[2]), int(coord[3])), str(ref)))
294
 
295
  for data in hotspots_sku_links:
296
  for item in data:
297
  product_table_data.append({
298
- 'Reference Number': item['sku'],
299
  'Product Name': item['product_name'] if item['product_name'] else "Not Found",
300
- # 'Price': ", ".join(item['prices']) if item['prices'] else " ",
301
  'URL': item['url']
302
  })
303
 
 
304
  # Create a DataFrame
305
  product_dataframe_data = pd.DataFrame(product_table_data)
306
 
@@ -308,37 +270,376 @@ with gr.Blocks() as demo:
308
  # hotspots_sku_links = [item for sublist in hotspots_sku_links for item in sublist]
309
  print("hotspots_sku_links: ", hotspots_sku_links)
310
  print('length of hotspots_sku_links: ', len(hotspots_sku_links))
311
- print('shape of hotspots_sku_links: ', np.shape(hotspots_sku_links))
312
- print("skus: ", sku)
313
- print('length of skus: ', len(sku))
314
  assert len(hotspots_sku_links) == len(sku)
315
- return (img, hostpots), product_dataframe_data
316
-
317
-
318
- section_btn.click(section, [img_input, selected_shop, confidence_threshold], [img_output, product_dataframe])
319
 
320
 
 
321
 
322
  def select_section(evt: gr.SelectData):
323
  hotspot_data = hotspots_sku_links[evt.index]
324
  markdown_output = ""
325
  for data in hotspot_data:
326
  url = data['url']
327
- price_str = ", ".join(data['prices']) if data['prices'] else "No price found"
328
  product_name = data['product_name'] if data['product_name'] else "No product name found"
329
  if product_name != "No product name found":
330
- if price_str != "No price found":
331
- markdown_output += f"[{product_name}]({url}): {price_str}\n\n"
332
- else:
333
- markdown_output += f"[{product_name}]({url})\n\n"
334
  else:
335
- if price_str != "No price found":
336
- markdown_output += f"[SKU: {data['sku']}]({url}): {price_str}\n\n"
337
- else:
338
- markdown_output += f"[SKU: {data['sku']}]({url})\n\n"
339
  return markdown_output
340
 
341
  img_output.select(select_section, None, selected_section)
342
 
343
- # demo.launch(share=False)
344
  demo.queue(max_size=99).launch(debug=True)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
10
  from roboflow import Roboflow
11
  import gradio as gr
12
  from ultralytics import YOLO
13
+ import pypdfium2 as pdfium
14
+
15
+ search_google = True
16
+ GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
17
+ # GOOGLE_CSE_ID = "b536303d151b94476"
18
+ # GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
19
+ # GOOGLE_CSE_ID = "f3446c60c52cd4dc2"
20
+ # GOOGLE_API_KEY = "AIzaSyCoBYZK4khX_W5eW-AqEXKOclUIer6CiC8"
21
+ # GOOGLE_CSE_ID = "c3ba01991c1a14c7d"
22
+ GOOGLE_CSE_ID = "b536303d151b94476"
23
+
24
+ mode = 'local'
25
+ # mode = 'rbf'
26
+
27
+ if mode == 'local':
28
+ model = YOLO('best-flyers-yolov8-1000img-v10.pt')
29
+ else:
30
+ rf = Roboflow(api_key="TN586SQsdwjUUvMN3H8e")
31
+ project = rf.workspace().project("smartflyers")
32
+ model = project.version(9).model
33
+
34
+ # def process(selected_shop):
35
+ # print(selected_shop)
36
+ # return selected_shop
37
+ hotspots_sku_links = []
38
+ shop = None
39
+ shops = {
40
+ 'Colruyt': {
41
+ 'url': 'https://www.colruyt.be/fr/producten?searchTerm=e',
42
+ 'regex': r'',
43
+ 'multi_items': False,
44
+ },
45
+ # Add more shops and their respective information here
46
+ }
47
+
48
+ regex_price = r'(\d+\.?\d*) ?([€$])|([€$]) ?(\d+\.?\d*)'
49
+
50
+ def pdf_to_image(pdf):
51
+ pdf = pdfium.PdfDocument(pdf)
52
+ n_pages = len(pdf) # get the number of pages in the document
53
+ print(f"Number of pages: {n_pages}")
54
+ assert n_pages > 0, "The PDF document is empty"
55
+ assert n_pages == 1, "The PDF document has more than one page"
56
+ page = pdf.get_page(0)
57
+ bitmap = page.render(
58
+ scale=5, # 72dpi resolution
59
+ rotation=0, # no additional rotation
60
+ # ... further rendering options
61
+ )
62
+ pil_image = bitmap.to_pil()
63
+ pil_image.save('test.png')
64
+ width_img, height_img = pil_image.size
65
+ print("width image", width_img, "height image", height_img)
66
+ width_pdf, height_pdf = page.get_size()
67
+ print("width pdf ", width_pdf, "height pdf ", height_pdf)
68
+ #assert int(width_pdf) == width_img, "The width of the image is not the same as the width of the page"
69
+ #assert int(height_pdf) == height_img, "The height of the image is not the same as the height of the page"
70
+ return pil_image, width_pdf, height_pdf, page
71
+
72
+ def app(pdf, selected_shop):
73
+ if pdf is None:
74
+ raise gr.Error("No file uploaded. Please upload PDF.")
75
+
76
+ # Check if the uploaded file is a PDF
77
+ if selected_shop is None:
78
+ raise gr.Error("Please select a shop.")
79
+
80
+ try:
81
+ image_annotated = section(pdf)
82
+ pass
83
+ except Exception as e:
84
+ return gr.Error(f"An error occurred: {str(e)}")
85
+
86
+ return image_annotated, hotspots_sku_links[selected_shop]
87
+ def build_request(query, start=1, num=10, date_restrict=None, **params):
88
+ request = {
89
+ 'key': GOOGLE_API_KEY,
90
+ 'q': query,
91
+ 'cx': GOOGLE_CSE_ID,
92
+ # 'start': start,
93
+ # 'num': num,
94
+ # 'dateRestrict': date_restrict,
95
  }
96
+ request.update(params)
97
+ return request
98
+
99
+
100
+ def make_request(request):
101
+ response = requests.get('https://www.googleapis.com/customsearch/v1', params=request)
102
+ # print(f"Status Code: {response.status_code}") # Print status code for debugging
103
+ if response.status_code != 200:
104
+ raise Exception("Google Custom Search request failed with status code {}".format(response.status_code))
105
+
106
+ search_results = response.json()
107
+ # print(f"Response JSON: {search_results}") # Print raw response for debugging
108
+ if 'items' in search_results and len(search_results['items']) > 0:
109
+ # find the first result with a 'linl' containing 'www.colruyt' and return it
110
+ first_result_url = None
111
+ for item in search_results['items']:
112
+ if 'link' in item and 'www.colruyt' in item['link']:
113
+ first_result_url = item['link']
114
+ break
115
+ #first_result_url = search_results['items'][0]['link']
116
+ return first_result_url
117
+ else:
118
+ return None
119
 
120
+ with gr.Blocks() as demo:
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
121
 
122
  with gr.Row():
123
  selected_shop = gr.Dropdown(shops.keys(), label="Shop")
 
126
  confidence_threshold = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Confidence Threshold", value=0.5)
127
 
128
  with gr.Row():
129
+ pdf_input = gr.File(label="Input page (PDF)", file_types=["pdf"])
130
  img_output = gr.AnnotatedImage(label="Annotated page (image)")
131
 
132
  section_btn = gr.Button("Identify Hotspots")
 
134
  selected_section = gr.Markdown(label="Selected Hotspot")
135
  product_dataframe = gr.Dataframe(label="Products Found")
136
 
 
137
  def get_yolo_predictions(image, confidence_threshold):
138
  # Use Roboflow API to get predictions
139
  predictions = model.predict(image, confidence=confidence_threshold, overlap=10).json()
 
152
  text = pytesseract.image_to_string(grey, lang='eng')
153
  return text
154
 
155
+ def section(pdf, selected_shop, confidence_threshold):
156
  hostpots = []
157
+ # texts = []
158
  sku = []
159
  product_table_data = []
160
 
161
+ image, width_pdf, height_pdf, first_page = pdf_to_image(pdf)
162
+ image_name ="test.jpg"
163
+ image.save(image_name)
164
+
165
+ # # Load a text page helper
166
+ textpage = first_page.get_textpage()
167
+
168
+ text_all = textpage.get_text_range()
169
+ print("text_all", text_all)
170
 
171
  #print("selected_shop:", selected_shop)
172
  search_url = shops[selected_shop]['url']
173
  ref_number_regex = shops[selected_shop]['regex']
174
  # print("search_url:", search_url)
175
  brands = shops[selected_shop]['brands'] if 'brands' in shops[selected_shop] else None
 
176
  if brands:
177
  brands = [brand.upper() for brand in brands]
178
 
179
  if mode == 'rbf':
180
+ predictions = get_yolo_predictions(image)
181
  else:
182
  # local inference
183
+ results = get_yolo_predictions_local(image, confidence_threshold)
184
  for result in results:
185
+ coordinates = result.boxes.xyxyn
186
  coordinates = coordinates.to("cpu")
187
  coordinates = coordinates.numpy()
188
  print("FOUND {} objects".format(len(coordinates)))
189
+ # print("coordinates", coordinates)
190
  for idx, coord in enumerate(coordinates):
191
+ print("==============")
192
  # crop the image to the bounding box
193
+ # expansion_factor = 0.01
194
+ x = coord[0]
195
+ y = coord[1]
196
+ w = coord[2] - coord[0]
197
+ h = coord[3] - coord[1]
198
+ # sub_image = image[y:y + h, x:x + w]
199
+ print("x, y, w, h", x, y, w, h)
200
+
201
+ print('width_pdf', width_pdf)
202
+ print('height_pdf', height_pdf)
203
+
204
+ enlarge_factor = 0.0
205
+ pdf_x = x * width_pdf
206
+ pdf_y = y * height_pdf + enlarge_factor * height_pdf
207
+ pdf_w = w * width_pdf + enlarge_factor * width_pdf
208
+ pdf_h = h * height_pdf
209
+ print("PDF", pdf_x, pdf_y, pdf_w, pdf_h)
210
+
211
+ img_x = int(x * image.width)
212
+ img_y = int(y * image.height)
213
+ img_w = int(w * image.width)
214
+ img_h = int(h * image.height)
215
+ print("IMG", img_x, img_y, img_w, img_h)
216
+
217
+ text = textpage.get_text_bounded(left=pdf_x,
218
+ bottom=height_pdf - pdf_y - pdf_h,
219
+ right=pdf_x + pdf_w,
220
+ top=height_pdf - pdf_y)
221
+
222
+
223
+ print("TEXT", text)
224
+
225
+
226
+ if search_google:
227
+ # Use Google Search API to find items with the SKU
228
+ # keep only the first 2 lines of text to search
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
229
  lines = text.split('\n')
230
+ # delete the \r in all lines
231
+ lines = [line.replace('\r', '') for line in lines]
232
+ # remove the blank spaces at the beginning and end of each line
233
+ lines = [line.strip() for line in lines]
234
+ text = '\n'.join(lines[:2])
235
+ # if the text is less than 3 words, add the next line
236
+ if len(text.split(' ')) < 2:
237
+ text = '\n'.join(lines[:3])
238
+ product_name = text
239
+
240
+ to_seach = str(selected_shop) + ' ' + text + str(selected_shop)
241
+ print("TO SEARCH: ", to_seach)
242
+ request = build_request(to_seach)
243
+ search_query = make_request(request)
244
+ print(f"First Result URL: {search_query}")
245
+ # search_query = "https://www.google.com/search?q="
246
+
247
+ sku.append(product_name)
248
+ # if search_query not in hotspots_sku_links:
249
+ hotspots_sku_links.append([{'sku': product_name, 'url': search_query, 'product_name': product_name}])
250
+ hostpots.append(((img_x, img_y, (img_x + img_w), (img_h+img_y)), str(product_name)))
251
+ print("----- ADDED ------")
252
+
253
+
254
+
255
+ print("hotspots_sku_links: ", hotspots_sku_links)
256
+ print("skus: ", sku)
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
257
 
258
  for data in hotspots_sku_links:
259
  for item in data:
260
  product_table_data.append({
 
261
  'Product Name': item['product_name'] if item['product_name'] else "Not Found",
 
262
  'URL': item['url']
263
  })
264
 
265
+
266
  # Create a DataFrame
267
  product_dataframe_data = pd.DataFrame(product_table_data)
268
 
 
270
  # hotspots_sku_links = [item for sublist in hotspots_sku_links for item in sublist]
271
  print("hotspots_sku_links: ", hotspots_sku_links)
272
  print('length of hotspots_sku_links: ', len(hotspots_sku_links))
 
 
 
273
  assert len(hotspots_sku_links) == len(sku)
274
+ return (image, hostpots), product_dataframe_data
 
 
 
275
 
276
 
277
+ section_btn.click(section, [pdf_input, selected_shop, confidence_threshold], [img_output, product_dataframe])
278
 
279
  def select_section(evt: gr.SelectData):
280
  hotspot_data = hotspots_sku_links[evt.index]
281
  markdown_output = ""
282
  for data in hotspot_data:
283
  url = data['url']
 
284
  product_name = data['product_name'] if data['product_name'] else "No product name found"
285
  if product_name != "No product name found":
286
+ markdown_output += f"[{product_name}]({url})\n\n"
 
 
 
287
  else:
288
+ markdown_output += f"[SKU: {data['sku']}]({url})\n\n"
 
 
 
289
  return markdown_output
290
 
291
  img_output.select(select_section, None, selected_section)
292
 
 
293
  demo.queue(max_size=99).launch(debug=True)
294
+
295
+ # import urllib
296
+ # import pandas as pd
297
+ # import numpy as np
298
+ # from pdf2image import convert_from_path
299
+ # import cv2
300
+ # import pytesseract
301
+ # import requests
302
+ # import json
303
+ # import re
304
+ # from roboflow import Roboflow
305
+ # import gradio as gr
306
+ # from ultralytics import YOLO
307
+ #
308
+ #
309
+ #
310
+ #
311
+ # with gr.Blocks() as demo:
312
+ # search_google = False
313
+ # GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
314
+ # GOOGLE_CSE_ID = "b536303d151b94476"
315
+ # # GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
316
+ # # GOOGLE_CSE_ID = "f3446c60c52cd4dc2"
317
+ # # GOOGLE_API_KEY = "AIzaSyCoBYZK4khX_W5eW-AqEXKOclUIer6CiC8"
318
+ # # GOOGLE_CSE_ID = "c3ba01991c1a14c7d"
319
+ # mode = 'local'
320
+ # # mode = 'rbf'
321
+ #
322
+ # if mode == 'local':
323
+ # model = YOLO('best-flyers-yolov8-1000img-v10.pt')
324
+ # else:
325
+ # rf = Roboflow(api_key="TN586SQsdwjUUvMN3H8e")
326
+ # project = rf.workspace().project("smartflyers")
327
+ # model = project.version(9).model
328
+ #
329
+ # # def process(selected_shop):
330
+ # # print(selected_shop)
331
+ # # return selected_shop
332
+ # hotspots_sku_links = []
333
+ # shop = None
334
+ # shops = {
335
+ # 'Latulippe': {
336
+ # 'url': 'https://latulippe.com/fr/recherche/?keywords=',
337
+ # 'regex': r'N° (M\d+)',
338
+ # 'multi_items': False,
339
+ # },
340
+ # 'Decathlon': {
341
+ # 'url': 'https://www.decathlon.fr/search?Ntt=',
342
+ # 'regex': r'Réf\. ?: ?([\d, ]+)',
343
+ # 'multi_items': True,
344
+ # 'brands': [
345
+ # "Allsix", "Aptonia", "Artengo", "Atorka", "B'Twin", "Caperlan", "Domyos", "Dreamscape",
346
+ # "FLX", "Forclaz", "Fouganza", "Geologic", "Geonaute", "Inesis", "Imviso", "Inovik",
347
+ # "Itiwit", "KOODZA", "KOROK", "Kalenji", "Kimjaly", "Kipsta", "Kuikma", "Nabaiji",
348
+ # "Newfeel", "Offload", "Olaian", "Opfeel", "Orao", "Oroks", "Outshock", "Oxelo",
349
+ # "Perfly", "Pongori", "Quechua", "Rockrider", "Sandever", "Simond", "Solognac",
350
+ # "Subea", "Tarmak", "Triban", "Tribord", "Urball", "Van Rysel", "Wed'Ze", "Decathlon"
351
+ # ]
352
+ # },
353
+ # 'Canadian Tire': {
354
+ # 'url': 'https://www.canadiantire.ca/en/search-results.html?q=',
355
+ # 'regex': r'(\d{2,3}-\d{4}-\d(?:/\d{4}-\d)?)',
356
+ # 'multi_items': True,
357
+ # },
358
+ # 'Walmart': {
359
+ # # 'url': 'https://www.walmart.ca/en/search?q=',
360
+ # 'url': '',
361
+ # 'regex': r'N°(\d+)',
362
+ # 'multi_items': False,
363
+ # },
364
+ # 'Colruyt': {
365
+ # 'url': 'https://www.colruyt.be/fr/producten?searchTerm=e',
366
+ # 'regex': r'',
367
+ # 'multi_items': False,
368
+ # },
369
+ # # Add more shops and their respective information here
370
+ # }
371
+ #
372
+ # regex_price = r'(\d+\.?\d*) ?([€$])|([€$]) ?(\d+\.?\d*)'
373
+ #
374
+ #
375
+ # # def app(img, selected_shop):
376
+ # # if img is None:
377
+ # # raise gr.Error("No file uploaded. Please upload an image.")
378
+ # #
379
+ # # # Check if the uploaded file is a PDF
380
+ # # if selected_shop is None:
381
+ # # raise gr.Error("Please select a shop.")
382
+ # #
383
+ # # try:
384
+ # # image_annotated = section(img)
385
+ # # pass
386
+ # # except Exception as e:
387
+ # # return gr.Error(f"An error occurred: {str(e)}")
388
+ # #
389
+ # # return image_annotated, hotspots_sku_links[selected_shop]
390
+ #
391
+ # def build_request(query, start=1, num=10, date_restrict=None, **params):
392
+ # request = {
393
+ # 'key': GOOGLE_API_KEY,
394
+ # 'q': query,
395
+ # 'cx': GOOGLE_CSE_ID,
396
+ # # 'start': start,
397
+ # # 'num': num,
398
+ # # 'dateRestrict': date_restrict,
399
+ # }
400
+ # request.update(params)
401
+ # return request
402
+ #
403
+ #
404
+ # def make_request(request):
405
+ # response = requests.get('https://www.googleapis.com/customsearch/v1', params=request)
406
+ # # print(f"Status Code: {response.status_code}") # Print status code for debugging
407
+ # if response.status_code != 200:
408
+ # raise Exception("Google Custom Search request failed with status code {}".format(response.status_code))
409
+ #
410
+ # search_results = response.json()
411
+ # # print(f"Response JSON: {search_results}") # Print raw response for debugging
412
+ # if 'items' in search_results and len(search_results['items']) > 0:
413
+ # first_result_url = search_results['items'][0]['link']
414
+ # return first_result_url
415
+ # else:
416
+ # return None
417
+ #
418
+ #
419
+ # with gr.Row():
420
+ # selected_shop = gr.Dropdown(shops.keys(), label="Shop")
421
+ # # with gr.Row():
422
+ # # input_type = gr.Radio(["PDF", "Image"], label="Input Type", value="PDF")
423
+ # confidence_threshold = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Confidence Threshold", value=0.5)
424
+ #
425
+ # with gr.Row():
426
+ # img_input = gr.Image(label="Input page (image)", sources=["upload", "webcam"])
427
+ # img_output = gr.AnnotatedImage(label="Annotated page (image)")
428
+ #
429
+ # section_btn = gr.Button("Identify Hotspots")
430
+ # # selected_section = gr.Textbox(label="Selected Hotspot")
431
+ # selected_section = gr.Markdown(label="Selected Hotspot")
432
+ # product_dataframe = gr.Dataframe(label="Products Found")
433
+ #
434
+ #
435
+ # def get_yolo_predictions(image, confidence_threshold):
436
+ # # Use Roboflow API to get predictions
437
+ # predictions = model.predict(image, confidence=confidence_threshold, overlap=10).json()
438
+ # # print("predictions", predictions)
439
+ # return predictions
440
+ #
441
+ # def get_yolo_predictions_local(image, confidence_threshold):
442
+ # # Perform object detection on an image using the model
443
+ # predictions = model(image, conf=confidence_threshold)
444
+ # # print("predictions", predictions)
445
+ # return predictions
446
+ #
447
+ # def ocr(sub_image):
448
+ # # Use OCR to get text from image
449
+ # grey = cv2.cvtColor(sub_image, cv2.COLOR_BGR2GRAY)
450
+ # text = pytesseract.image_to_string(grey, lang='eng')
451
+ # return text
452
+ #
453
+ # def section(img, selected_shop, confidence_threshold):
454
+ # hostpots = []
455
+ # sku = []
456
+ # product_table_data = []
457
+ #
458
+ # global hotspots_sku_links
459
+ # hotspots_sku_links = []
460
+ #
461
+ # #print("selected_shop:", selected_shop)
462
+ # search_url = shops[selected_shop]['url']
463
+ # ref_number_regex = shops[selected_shop]['regex']
464
+ # # print("search_url:", search_url)
465
+ # brands = shops[selected_shop]['brands'] if 'brands' in shops[selected_shop] else None
466
+ #
467
+ # if brands:
468
+ # brands = [brand.upper() for brand in brands]
469
+ #
470
+ # if mode == 'rbf':
471
+ # predictions = get_yolo_predictions(img)
472
+ # else:
473
+ # # local inference
474
+ # results = get_yolo_predictions_local(img, confidence_threshold)
475
+ # for result in results:
476
+ # coordinates = result.boxes.xyxy
477
+ # coordinates = coordinates.to("cpu")
478
+ # coordinates = coordinates.numpy()
479
+ # print("FOUND {} objects".format(len(coordinates)))
480
+ # for idx, coord in enumerate(coordinates):
481
+ # # crop the image to the bounding box
482
+ # x = int(coord[0])
483
+ # y = int(coord[1])
484
+ # w = int(coord[2] - coord[0])
485
+ # h = int(coord[3] - coord[1])
486
+ # sub_image = img[y:y + h, x:x + w]
487
+ # if shops[selected_shop]['url'] is None or shops[selected_shop]['url'] == "":
488
+ # print("No search url for this shop")
489
+ # # just append the hotspot to hostpots
490
+ # hostpots.append(((x, y, x + w, y + h), str(idx)))
491
+ # continue
492
+ #
493
+ # # Extract text using OCR
494
+ # text = ocr(sub_image)
495
+ # print("text: ", text)
496
+ #
497
+ # if selected_shop == "Latulippe":
498
+ # # if there is a 'i' between a "M" and a number, it is a sku. Replace the 'i' with a '1'
499
+ # text = re.sub(r'Mi(\d+)', r'M1\1', text)
500
+ #
501
+ # # for object_number, text in enumerate(texts, start=1):
502
+ # #print("object", object_number)
503
+ # print("text -----", text)
504
+ # ref_numbers = []
505
+ # if selected_shop == "Decathlon":
506
+ # full_match = re.search(ref_number_regex, text)
507
+ # if full_match:
508
+ # print("FULL MATCH: ", full_match)
509
+ # # Extracting all numbers from the captured sequence
510
+ # numbers_string = full_match.group(1)
511
+ # ref_numbers = re.findall(r'\d+', numbers_string)
512
+ # print("REF NUMBERS: ", ref_numbers)
513
+ # else:
514
+ # ref_numbers = re.findall(ref_number_regex, text)
515
+ # # print("REF NUMBERS: ", ref_numbers)
516
+ #
517
+ # if selected_shop == "Canadian Tire":
518
+ # temp_ref_numbers = ref_numbers
519
+ # ref_numbers = []
520
+ # split = False
521
+ # for ref_sku in temp_ref_numbers:
522
+ # if '/' in ref_sku:
523
+ # split = True
524
+ # sku_split = ref_sku.split('/')
525
+ # sku_last = sku_split[-1]
526
+ # length = len(sku_last)
527
+ # base_number = sku_split[0][:-length]
528
+ # ref_numbers.append(sku_split[0])
529
+ # # base number + sku for each sku in sku_split except the first one
530
+ # for i in range(1, len(sku_split)):
531
+ # ref_numbers.append(base_number + sku_split[i])
532
+ #
533
+ # if split == False:
534
+ # ref_numbers = temp_ref_numbers
535
+ #
536
+ # matches = re.findall(regex_price, text)
537
+ # prices = []
538
+ # for match in matches:
539
+ # if match[0]: # If the first group is not empty, the format is "price currency"
540
+ # prices.append(match[0] + match[1])
541
+ # else: # Otherwise, the format is "currency price"
542
+ # prices.append(match[2] + match[3])
543
+ # # price = prices[0] if len(prices) > 0 else ""
544
+ # #print("PRICES: ", prices)
545
+ #
546
+ # if brands:
547
+ # # print("BRANDS: yes")
548
+ # # Extracting the product name
549
+ # lines = text.split('\n')
550
+ # product_name = ""
551
+ # found_brand = False
552
+ # name_started = False
553
+ # for line in lines:
554
+ # if any(brand in line for brand in brands):
555
+ # found_brand = True
556
+ # #print("FOUND BRAND", line)
557
+ # continue
558
+ # if found_brand:
559
+ # #print('current line:', line)
560
+ # if line.strip() == "": # Skip blank lines
561
+ # continue
562
+ # if name_started and (line[0].isupper() or any(char.isdigit() for char in line)):
563
+ # break
564
+ # product_name += line.strip() + " "
565
+ # name_started = True
566
+ # product_name = product_name.strip()
567
+ #
568
+ # else:
569
+ # # Regex to match sequences of uppercase letters
570
+ # uppercase_words = re.findall(r'\b[A-Z]+\b', text)
571
+ #
572
+ # # Joining the extracted words with a space
573
+ # product_name = ' '.join(uppercase_words)
574
+ #
575
+ # #print("PRODUCT NAME: ", product_name)
576
+ #
577
+ # for ref in ref_numbers:
578
+ # print("ref", ref)
579
+ # if search_google:
580
+ # # Use Google Search API to find items with the SKU
581
+ # to_seach = str(selected_shop) + ' ' + str(ref)
582
+ # # print("to_search: ", to_seach)
583
+ # request = build_request(to_seach)
584
+ # search_query = make_request(request)
585
+ # # print(f"First Result URL: {search_query}")
586
+ # if search_query is None:
587
+ # search_query = search_url + urllib.parse.quote(ref)
588
+ # else:
589
+ # search_query = search_url + urllib.parse.quote(ref)
590
+ # if ref not in sku:
591
+ # sku.append(ref)
592
+ # # if search_query not in hotspots_sku_links:
593
+ # hotspots_sku_links.append([{'sku': ref, 'url': search_query, 'prices': prices, 'product_name': product_name}])
594
+ # hostpots.append(((int(coord[0]), int(coord[1]), int(coord[2]), int(coord[3])), str(ref)))
595
+ #
596
+ # for data in hotspots_sku_links:
597
+ # for item in data:
598
+ # product_table_data.append({
599
+ # 'Reference Number': item['sku'],
600
+ # 'Product Name': item['product_name'] if item['product_name'] else "Not Found",
601
+ # # 'Price': ", ".join(item['prices']) if item['prices'] else " ",
602
+ # 'URL': item['url']
603
+ # })
604
+ #
605
+ # # Create a DataFrame
606
+ # product_dataframe_data = pd.DataFrame(product_table_data)
607
+ #
608
+ # # flattern the list hotspots_sku_links
609
+ # # hotspots_sku_links = [item for sublist in hotspots_sku_links for item in sublist]
610
+ # print("hotspots_sku_links: ", hotspots_sku_links)
611
+ # print('length of hotspots_sku_links: ', len(hotspots_sku_links))
612
+ # print('shape of hotspots_sku_links: ', np.shape(hotspots_sku_links))
613
+ # print("skus: ", sku)
614
+ # print('length of skus: ', len(sku))
615
+ # assert len(hotspots_sku_links) == len(sku)
616
+ # return (img, hostpots), product_dataframe_data
617
+ #
618
+ #
619
+ # section_btn.click(section, [img_input, selected_shop, confidence_threshold], [img_output, product_dataframe])
620
+ #
621
+ #
622
+ #
623
+ # def select_section(evt: gr.SelectData):
624
+ # hotspot_data = hotspots_sku_links[evt.index]
625
+ # markdown_output = ""
626
+ # for data in hotspot_data:
627
+ # url = data['url']
628
+ # price_str = ", ".join(data['prices']) if data['prices'] else "No price found"
629
+ # product_name = data['product_name'] if data['product_name'] else "No product name found"
630
+ # if product_name != "No product name found":
631
+ # if price_str != "No price found":
632
+ # markdown_output += f"[{product_name}]({url}): {price_str}\n\n"
633
+ # else:
634
+ # markdown_output += f"[{product_name}]({url})\n\n"
635
+ # else:
636
+ # if price_str != "No price found":
637
+ # markdown_output += f"[SKU: {data['sku']}]({url}): {price_str}\n\n"
638
+ # else:
639
+ # markdown_output += f"[SKU: {data['sku']}]({url})\n\n"
640
+ # return markdown_output
641
+ #
642
+ # img_output.select(select_section, None, selected_section)
643
+ #
644
+ # # demo.launch(share=False)
645
+ # demo.queue(max_size=99).launch(debug=True)
app_file.py ADDED
@@ -0,0 +1,401 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import urllib
2
+ import pandas as pd
3
+ import numpy as np
4
+ from pdf2image import convert_from_path
5
+ import cv2
6
+ import pytesseract
7
+ import requests
8
+ import json
9
+ import re
10
+ from roboflow import Roboflow
11
+ import gradio as gr
12
+ from ultralytics import YOLO
13
+ import pypdfium2 as pdfium
14
+
15
+ search_google = True
16
+ GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
17
+ # GOOGLE_CSE_ID = "b536303d151b94476"
18
+ # GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
19
+ # GOOGLE_CSE_ID = "f3446c60c52cd4dc2"
20
+ # GOOGLE_API_KEY = "AIzaSyCoBYZK4khX_W5eW-AqEXKOclUIer6CiC8"
21
+ # GOOGLE_CSE_ID = "c3ba01991c1a14c7d"
22
+ GOOGLE_CSE_ID = "b536303d151b94476"
23
+
24
+ mode = 'local'
25
+ # mode = 'rbf'
26
+
27
+ if mode == 'local':
28
+ model = YOLO('best-flyers-yolov8-1000img-v10.pt')
29
+ else:
30
+ rf = Roboflow(api_key="TN586SQsdwjUUvMN3H8e")
31
+ project = rf.workspace().project("smartflyers")
32
+ model = project.version(9).model
33
+
34
+ # def process(selected_shop):
35
+ # print(selected_shop)
36
+ # return selected_shop
37
+ hotspots_sku_links = []
38
+ shop = None
39
+ shops = {
40
+ 'Latulippe': {
41
+ 'url': 'https://latulippe.com/fr/recherche/?keywords=',
42
+ 'regex': r'N° (M\d+)',
43
+ 'multi_items': False,
44
+ },
45
+ 'Decathlon': {
46
+ 'url': 'https://www.decathlon.fr/search?Ntt=',
47
+ 'regex': r'Réf\. ?: ?([\d, ]+)',
48
+ 'multi_items': True,
49
+ 'brands': [
50
+ "Allsix", "Aptonia", "Artengo", "Atorka", "B'Twin", "Caperlan", "Domyos", "Dreamscape",
51
+ "FLX", "Forclaz", "Fouganza", "Geologic", "Geonaute", "Inesis", "Imviso", "Inovik",
52
+ "Itiwit", "KOODZA", "KOROK", "Kalenji", "Kimjaly", "Kipsta", "Kuikma", "Nabaiji",
53
+ "Newfeel", "Offload", "Olaian", "Opfeel", "Orao", "Oroks", "Outshock", "Oxelo",
54
+ "Perfly", "Pongori", "Quechua", "Rockrider", "Sandever", "Simond", "Solognac",
55
+ "Subea", "Tarmak", "Triban", "Tribord", "Urball", "Van Rysel", "Wed'Ze", "Decathlon"
56
+ ]
57
+ },
58
+ 'Canadian Tire': {
59
+ 'url': 'https://www.canadiantire.ca/en/search-results.html?q=',
60
+ 'regex': r'(\d{2,3}-\d{4}-\d(?:/\d{4}-\d)?)',
61
+ 'multi_items': True,
62
+ },
63
+ 'Walmart': {
64
+ # 'url': 'https://www.walmart.ca/en/search?q=',
65
+ 'url': '',
66
+ 'regex': r'N°(\d+)',
67
+ 'multi_items': False,
68
+ },
69
+ 'Colruyt': {
70
+ 'url': 'https://www.colruyt.be/fr/producten?searchTerm=e',
71
+ 'regex': r'',
72
+ 'multi_items': False,
73
+ },
74
+ # Add more shops and their respective information here
75
+ }
76
+
77
+ regex_price = r'(\d+\.?\d*) ?([€$])|([€$]) ?(\d+\.?\d*)'
78
+
79
+ def pdf_to_image(pdf):
80
+ pdf = pdfium.PdfDocument(pdf)
81
+ n_pages = len(pdf) # get the number of pages in the document
82
+ print(f"Number of pages: {n_pages}")
83
+ assert n_pages > 0, "The PDF document is empty"
84
+ assert n_pages == 1, "The PDF document has more than one page"
85
+ page = pdf.get_page(0)
86
+ bitmap = page.render(
87
+ scale=5, # 72dpi resolution
88
+ rotation=0, # no additional rotation
89
+ # ... further rendering options
90
+ )
91
+ pil_image = bitmap.to_pil()
92
+ pil_image.save('test.png')
93
+ width_img, height_img = pil_image.size
94
+ print("width image", width_img, "height image", height_img)
95
+ width_pdf, height_pdf = page.get_size()
96
+ print("width pdf ", width_pdf, "height pdf ", height_pdf)
97
+ #assert int(width_pdf) == width_img, "The width of the image is not the same as the width of the page"
98
+ #assert int(height_pdf) == height_img, "The height of the image is not the same as the height of the page"
99
+ return pil_image, width_pdf, height_pdf, page
100
+
101
+ def app(pdf, selected_shop):
102
+ if pdf is None:
103
+ raise gr.Error("No file uploaded. Please upload PDF.")
104
+
105
+ # Check if the uploaded file is a PDF
106
+ if selected_shop is None:
107
+ raise gr.Error("Please select a shop.")
108
+
109
+ try:
110
+ image_annotated = section(pdf)
111
+ pass
112
+ except Exception as e:
113
+ return gr.Error(f"An error occurred: {str(e)}")
114
+
115
+ return image_annotated, hotspots_sku_links[selected_shop]
116
+ def build_request(query, start=1, num=10, date_restrict=None, **params):
117
+ request = {
118
+ 'key': GOOGLE_API_KEY,
119
+ 'q': query,
120
+ 'cx': GOOGLE_CSE_ID,
121
+ # 'start': start,
122
+ # 'num': num,
123
+ # 'dateRestrict': date_restrict,
124
+ }
125
+ request.update(params)
126
+ return request
127
+
128
+
129
+ def make_request(request):
130
+ response = requests.get('https://www.googleapis.com/customsearch/v1', params=request)
131
+ # print(f"Status Code: {response.status_code}") # Print status code for debugging
132
+ if response.status_code != 200:
133
+ raise Exception("Google Custom Search request failed with status code {}".format(response.status_code))
134
+
135
+ search_results = response.json()
136
+ # print(f"Response JSON: {search_results}") # Print raw response for debugging
137
+ if 'items' in search_results and len(search_results['items']) > 0:
138
+ first_result_url = search_results['items'][0]['link']
139
+ return first_result_url
140
+ else:
141
+ return None
142
+
143
+ with gr.Blocks() as demo:
144
+
145
+ with gr.Row():
146
+ selected_shop = gr.Dropdown(shops.keys(), label="Shop")
147
+ # with gr.Row():
148
+ # input_type = gr.Radio(["PDF", "Image"], label="Input Type", value="PDF")
149
+ confidence_threshold = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Confidence Threshold", value=0.5)
150
+
151
+ with gr.Row():
152
+ pdf_input = gr.File(label="Input page (PDF)", file_types=["pdf"])
153
+ img_output = gr.AnnotatedImage(label="Annotated page (image)")
154
+
155
+ section_btn = gr.Button("Identify Hotspots")
156
+ # selected_section = gr.Textbox(label="Selected Hotspot")
157
+ selected_section = gr.Markdown(label="Selected Hotspot")
158
+ def get_yolo_predictions(image, confidence_threshold):
159
+ # Use Roboflow API to get predictions
160
+ predictions = model.predict(image, confidence=confidence_threshold, overlap=10).json()
161
+ # print("predictions", predictions)
162
+ return predictions
163
+
164
+ def get_yolo_predictions_local(image, confidence_threshold):
165
+ # Perform object detection on an image using the model
166
+ predictions = model(image, conf=confidence_threshold)
167
+ # print("predictions", predictions)
168
+ return predictions
169
+
170
+ def ocr(sub_image):
171
+ # Use OCR to get text from image
172
+ grey = cv2.cvtColor(sub_image, cv2.COLOR_BGR2GRAY)
173
+ text = pytesseract.image_to_string(grey, lang='eng')
174
+ return text
175
+
176
+ def section(pdf, selected_shop, confidence_threshold):
177
+ hostpots = []
178
+ # texts = []
179
+ sku = []
180
+
181
+ image, width_pdf, height_pdf, first_page = pdf_to_image(pdf)
182
+ image_name ="test.jpg"
183
+ image.save(image_name)
184
+
185
+ # # Load a text page helper
186
+ textpage = first_page.get_textpage()
187
+
188
+ text_all = textpage.get_text_range()
189
+ print("text_all", text_all)
190
+
191
+ #print("selected_shop:", selected_shop)
192
+ search_url = shops[selected_shop]['url']
193
+ ref_number_regex = shops[selected_shop]['regex']
194
+ # print("search_url:", search_url)
195
+ brands = shops[selected_shop]['brands'] if 'brands' in shops[selected_shop] else None
196
+ if brands:
197
+ brands = [brand.upper() for brand in brands]
198
+
199
+ if mode == 'rbf':
200
+ predictions = get_yolo_predictions(image)
201
+ else:
202
+ # local inference
203
+ results = get_yolo_predictions_local(image, confidence_threshold)
204
+ for result in results:
205
+ coordinates = result.boxes.xyxyn
206
+ coordinates = coordinates.to("cpu")
207
+ coordinates = coordinates.numpy()
208
+ print("FOUND {} objects".format(len(coordinates)))
209
+ # print("coordinates", coordinates)
210
+ for idx, coord in enumerate(coordinates):
211
+ # crop the image to the bounding box
212
+ # expansion_factor = 0.01
213
+ x = coord[0]
214
+ y = coord[1]
215
+ w = coord[2] - coord[0]
216
+ h = coord[3] - coord[1]
217
+ # sub_image = image[y:y + h, x:x + w]
218
+ print("x, y, w, h", x, y, w, h)
219
+
220
+ print('width_pdf', width_pdf)
221
+ print('height_pdf', height_pdf)
222
+
223
+ enlarge_factor = 0.01
224
+ pdf_x = x * width_pdf
225
+ pdf_y = y * height_pdf + enlarge_factor * height_pdf
226
+ pdf_w = w * width_pdf + enlarge_factor * width_pdf
227
+ pdf_h = h * height_pdf
228
+ print("PDF", pdf_x, pdf_y, pdf_w, pdf_h)
229
+
230
+ img_x = int(x * image.width)
231
+ img_y = int(y * image.height)
232
+ img_w = int(w * image.width)
233
+ img_h = int(h * image.height)
234
+ print("IMG", img_x, img_y, img_w, img_h)
235
+
236
+
237
+ if shops[selected_shop]['url'] is None or shops[selected_shop]['url'] == "":
238
+ # just append the hotspot to hostpots
239
+ hostpots.append(((img_x, img_y, img_x + img_w, img_y + img_h), str(idx)))
240
+ continue
241
+
242
+ print('---------------------')
243
+ # Extract text
244
+ # text = textpage.get_text_bounded(left=pdf_x - enlarge_factor * width_pdf,
245
+ # bottom=height_pdf - pdf_y - pdf_h - (enlarge_factor*height_pdf),
246
+ # right=pdf_x+pdf_w + (enlarge_factor*width_pdf),
247
+ # top=height_pdf-pdf_y + (enlarge_factor*height_pdf))
248
+ text = textpage.get_text_bounded(left=pdf_x + 50,
249
+ bottom=height_pdf - pdf_y - pdf_h,
250
+ right=pdf_x + pdf_w + 50,
251
+ top=height_pdf - pdf_y)
252
+
253
+
254
+ print("TEXT", text)
255
+
256
+ # for object_number, text in enumerate(texts, start=1):
257
+ #print("object", object_number)
258
+ #print("text", text)
259
+ ref_numbers = []
260
+ if selected_shop == "Decathlon":
261
+ full_match = re.search(ref_number_regex, text)
262
+ if full_match:
263
+ #print("FULL MATCH: ", full_match)
264
+ # Extracting all numbers from the captured sequence
265
+ numbers_string = full_match.group(1)
266
+ ref_numbers = re.findall(r'\d+', numbers_string)
267
+ else:
268
+ ref_numbers = re.findall(ref_number_regex, text)
269
+
270
+ if selected_shop == "Colruyt":
271
+ ref_numbers = [' ']
272
+
273
+ print("REF NUMBERS: ", ref_numbers)
274
+ if selected_shop == "Canadian Tire":
275
+ temp_ref_numbers = ref_numbers
276
+ found_skus_new = []
277
+ ref_numbers = []
278
+ split = False
279
+ for ref_sku in temp_ref_numbers:
280
+ if '/' in ref_sku:
281
+ split = True
282
+ sku_split = ref_sku.split('/')
283
+ sku_last = sku_split[-1]
284
+ length = len(sku_last)
285
+ base_number = sku_split[0][:-length]
286
+ ref_numbers.append(sku_split[0])
287
+ # base number + sku for each sku in sku_split except the first one
288
+ for i in range(1, len(sku_split)):
289
+ ref_numbers.append(base_number + sku_split[i])
290
+
291
+ if split == False:
292
+ ref_numbers = temp_ref_numbers
293
+
294
+
295
+ #print("REF NUMBERS: ", ref_numbers)
296
+
297
+ matches = re.findall(regex_price, text)
298
+ prices = []
299
+ for match in matches:
300
+ if match[0]: # If the first group is not empty, the format is "price currency"
301
+ prices.append(match[0] + match[1])
302
+ else: # Otherwise, the format is "currency price"
303
+ prices.append(match[2] + match[3])
304
+ # price = prices[0] if len(prices) > 0 else ""
305
+ #print("PRICES: ", prices)
306
+
307
+ if brands:
308
+ # print("BRANDS: yes")
309
+ # Extracting the product name
310
+ lines = text.split('\n')
311
+ product_name = ""
312
+ found_brand = False
313
+ name_started = False
314
+ for line in lines:
315
+ if any(brand in line for brand in brands):
316
+ found_brand = True
317
+ #print("FOUND BRAND", line)
318
+ continue
319
+ if found_brand:
320
+ #print('current line:', line)
321
+ if line.strip() == "": # Skip blank lines
322
+ continue
323
+ if name_started and (line[0].isupper() or any(char.isdigit() for char in line)):
324
+ break
325
+ product_name += line.strip() + " "
326
+ name_started = True
327
+ product_name = product_name.strip()
328
+
329
+ else:
330
+ # Regex to match sequences of uppercase letters
331
+ uppercase_words = re.findall(r'\b[A-Z]+\b', text)
332
+
333
+ # Joining the extracted words with a space
334
+ product_name = ' '.join(uppercase_words)
335
+
336
+
337
+ #print("PRODUCT NAME: ", product_name)
338
+
339
+ print("FOUND {} ref numbers".format(len(ref_numbers)))
340
+ product_name
341
+ for ref in ref_numbers:
342
+ if search_google:
343
+ # Use Google Search API to find items with the SKU
344
+ to_seach = str(selected_shop) + ' ' + str(ref)
345
+ if selected_shop == "Colruyt":
346
+ # keep only the first 2 lines of text to search
347
+ lines = text.split('\n')
348
+ text = lines[0] + ' ' + lines[1]
349
+ product_name = lines[0]
350
+ to_seach = str(selected_shop) + ' ' + text
351
+ print("to_search: ", to_seach)
352
+ request = build_request(to_seach)
353
+ search_query = make_request(request)
354
+ print(f"First Result URL: {search_query}")
355
+ if search_query is None:
356
+ search_query = search_url + urllib.parse.quote(ref)
357
+ else:
358
+ search_query = search_url + urllib.parse.quote(ref)
359
+ if ref not in sku:
360
+ sku.append(ref)
361
+ # if search_query not in hotspots_sku_links:
362
+ hotspots_sku_links.append([{'sku': ref, 'url': search_query, 'prices': prices, 'product_name': product_name}])
363
+ hostpots.append(((img_x, img_y, (img_x + img_w), (img_h+img_y)), str(ref)))
364
+ print("----- ADDED ------")
365
+ if selected_shop == "Colruyt":
366
+ sku.append(product_name)
367
+ # if search_query not in hotspots_sku_links:
368
+ hotspots_sku_links.append(
369
+ [{'sku': product_name, 'url': search_query, 'prices': prices, 'product_name': product_name}])
370
+ hostpots.append(((img_x, img_y, (img_x + img_w), (img_h + img_y)), str(ref)))
371
+ print("----- ADDED ------")
372
+ if len(ref_numbers) == 0:
373
+ hostpots.append(((img_x, img_y, (img_x + img_w), (img_h+img_y)), str(idx)))
374
+ hotspots_sku_links.append([{'sku': None, 'url': None, 'prices': None, 'product_name': None}])
375
+ print("++++++ ADDED +++++")
376
+
377
+ print("hotspots_sku_links: ", hotspots_sku_links)
378
+ print("skus: ", sku)
379
+ return (image, hostpots)
380
+
381
+
382
+ section_btn.click(section, [pdf_input, selected_shop, confidence_threshold], img_output)
383
+
384
+
385
+ def select_section(evt: gr.SelectData):
386
+ hotspot_data = hotspots_sku_links[evt.index]
387
+ markdown_output = ""
388
+ for data in hotspot_data:
389
+ url = data['url']
390
+ price_str = ", ".join(data['prices']) if data['prices'] else "No price found"
391
+ product_name = data['product_name'] if data['product_name'] else "No product name found"
392
+ if product_name != "No product name found":
393
+ markdown_output += f"[{product_name}]({url})\n\n"
394
+ else:
395
+ markdown_output += f"[SKU: {data['sku']}]({url})\n\n"
396
+ return markdown_output
397
+
398
+ img_output.select(select_section, None, selected_section)
399
+
400
+ if __name__ == "__main__":
401
+ demo.launch(share=False)
app_file_colruyt.py ADDED
@@ -0,0 +1,294 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import urllib
2
+ import pandas as pd
3
+ import numpy as np
4
+ from pdf2image import convert_from_path
5
+ import cv2
6
+ import pytesseract
7
+ import requests
8
+ import json
9
+ import re
10
+ from roboflow import Roboflow
11
+ import gradio as gr
12
+ from ultralytics import YOLO
13
+ import pypdfium2 as pdfium
14
+
15
+ search_google = True
16
+ GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
17
+ # GOOGLE_CSE_ID = "b536303d151b94476"
18
+ # GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
19
+ # GOOGLE_CSE_ID = "f3446c60c52cd4dc2"
20
+ # GOOGLE_API_KEY = "AIzaSyCoBYZK4khX_W5eW-AqEXKOclUIer6CiC8"
21
+ # GOOGLE_CSE_ID = "c3ba01991c1a14c7d"
22
+ GOOGLE_CSE_ID = "b536303d151b94476"
23
+
24
+ mode = 'local'
25
+ # mode = 'rbf'
26
+
27
+ if mode == 'local':
28
+ model = YOLO('best-flyers-yolov8-1000img-v10.pt')
29
+ else:
30
+ rf = Roboflow(api_key="TN586SQsdwjUUvMN3H8e")
31
+ project = rf.workspace().project("smartflyers")
32
+ model = project.version(9).model
33
+
34
+ # def process(selected_shop):
35
+ # print(selected_shop)
36
+ # return selected_shop
37
+ hotspots_sku_links = []
38
+ shop = None
39
+ shops = {
40
+ 'Colruyt': {
41
+ 'url': 'https://www.colruyt.be/fr/producten?searchTerm=e',
42
+ 'regex': r'',
43
+ 'multi_items': False,
44
+ },
45
+ # Add more shops and their respective information here
46
+ }
47
+
48
+ regex_price = r'(\d+\.?\d*) ?([€$])|([€$]) ?(\d+\.?\d*)'
49
+
50
+ def pdf_to_image(pdf):
51
+ pdf = pdfium.PdfDocument(pdf)
52
+ n_pages = len(pdf) # get the number of pages in the document
53
+ print(f"Number of pages: {n_pages}")
54
+ assert n_pages > 0, "The PDF document is empty"
55
+ assert n_pages == 1, "The PDF document has more than one page"
56
+ page = pdf.get_page(0)
57
+ bitmap = page.render(
58
+ scale=5, # 72dpi resolution
59
+ rotation=0, # no additional rotation
60
+ # ... further rendering options
61
+ )
62
+ pil_image = bitmap.to_pil()
63
+ pil_image.save('test.png')
64
+ width_img, height_img = pil_image.size
65
+ print("width image", width_img, "height image", height_img)
66
+ width_pdf, height_pdf = page.get_size()
67
+ print("width pdf ", width_pdf, "height pdf ", height_pdf)
68
+ #assert int(width_pdf) == width_img, "The width of the image is not the same as the width of the page"
69
+ #assert int(height_pdf) == height_img, "The height of the image is not the same as the height of the page"
70
+ return pil_image, width_pdf, height_pdf, page
71
+
72
+ def app(pdf, selected_shop):
73
+ if pdf is None:
74
+ raise gr.Error("No file uploaded. Please upload PDF.")
75
+
76
+ # Check if the uploaded file is a PDF
77
+ if selected_shop is None:
78
+ raise gr.Error("Please select a shop.")
79
+
80
+ try:
81
+ image_annotated = section(pdf)
82
+ pass
83
+ except Exception as e:
84
+ return gr.Error(f"An error occurred: {str(e)}")
85
+
86
+ return image_annotated, hotspots_sku_links[selected_shop]
87
+ def build_request(query, start=1, num=10, date_restrict=None, **params):
88
+ request = {
89
+ 'key': GOOGLE_API_KEY,
90
+ 'q': query,
91
+ 'cx': GOOGLE_CSE_ID,
92
+ # 'start': start,
93
+ # 'num': num,
94
+ # 'dateRestrict': date_restrict,
95
+ }
96
+ request.update(params)
97
+ return request
98
+
99
+
100
+ def make_request(request):
101
+ response = requests.get('https://www.googleapis.com/customsearch/v1', params=request)
102
+ # print(f"Status Code: {response.status_code}") # Print status code for debugging
103
+ if response.status_code != 200:
104
+ raise Exception("Google Custom Search request failed with status code {}".format(response.status_code))
105
+
106
+ search_results = response.json()
107
+ # print(f"Response JSON: {search_results}") # Print raw response for debugging
108
+ if 'items' in search_results and len(search_results['items']) > 0:
109
+ # find the first result with a 'linl' containing 'www.colruyt' and return it
110
+ first_result_url = None
111
+ for item in search_results['items']:
112
+ if 'link' in item and 'www.colruyt' in item['link']:
113
+ first_result_url = item['link']
114
+ break
115
+ #first_result_url = search_results['items'][0]['link']
116
+ return first_result_url
117
+ else:
118
+ return None
119
+
120
+ with gr.Blocks() as demo:
121
+
122
+ with gr.Row():
123
+ selected_shop = gr.Dropdown(shops.keys(), label="Shop")
124
+ # with gr.Row():
125
+ # input_type = gr.Radio(["PDF", "Image"], label="Input Type", value="PDF")
126
+ confidence_threshold = gr.Slider(minimum=0.1, maximum=1.0, step=0.1, label="Confidence Threshold", value=0.5)
127
+
128
+ with gr.Row():
129
+ pdf_input = gr.File(label="Input page (PDF)", file_types=["pdf"])
130
+ img_output = gr.AnnotatedImage(label="Annotated page (image)")
131
+
132
+ section_btn = gr.Button("Identify Hotspots")
133
+ # selected_section = gr.Textbox(label="Selected Hotspot")
134
+ selected_section = gr.Markdown(label="Selected Hotspot")
135
+ product_dataframe = gr.Dataframe(label="Products Found")
136
+
137
+ def get_yolo_predictions(image, confidence_threshold):
138
+ # Use Roboflow API to get predictions
139
+ predictions = model.predict(image, confidence=confidence_threshold, overlap=10).json()
140
+ # print("predictions", predictions)
141
+ return predictions
142
+
143
+ def get_yolo_predictions_local(image, confidence_threshold):
144
+ # Perform object detection on an image using the model
145
+ predictions = model(image, conf=confidence_threshold)
146
+ # print("predictions", predictions)
147
+ return predictions
148
+
149
+ def ocr(sub_image):
150
+ # Use OCR to get text from image
151
+ grey = cv2.cvtColor(sub_image, cv2.COLOR_BGR2GRAY)
152
+ text = pytesseract.image_to_string(grey, lang='eng')
153
+ return text
154
+
155
+ def section(pdf, selected_shop, confidence_threshold):
156
+ hostpots = []
157
+ # texts = []
158
+ sku = []
159
+ product_table_data = []
160
+
161
+ image, width_pdf, height_pdf, first_page = pdf_to_image(pdf)
162
+ image_name ="test.jpg"
163
+ image.save(image_name)
164
+
165
+ # # Load a text page helper
166
+ textpage = first_page.get_textpage()
167
+
168
+ text_all = textpage.get_text_range()
169
+ print("text_all", text_all)
170
+
171
+ #print("selected_shop:", selected_shop)
172
+ search_url = shops[selected_shop]['url']
173
+ ref_number_regex = shops[selected_shop]['regex']
174
+ # print("search_url:", search_url)
175
+ brands = shops[selected_shop]['brands'] if 'brands' in shops[selected_shop] else None
176
+ if brands:
177
+ brands = [brand.upper() for brand in brands]
178
+
179
+ if mode == 'rbf':
180
+ predictions = get_yolo_predictions(image)
181
+ else:
182
+ # local inference
183
+ results = get_yolo_predictions_local(image, confidence_threshold)
184
+ for result in results:
185
+ coordinates = result.boxes.xyxyn
186
+ coordinates = coordinates.to("cpu")
187
+ coordinates = coordinates.numpy()
188
+ print("FOUND {} objects".format(len(coordinates)))
189
+ # print("coordinates", coordinates)
190
+ for idx, coord in enumerate(coordinates):
191
+ print("==============")
192
+ # crop the image to the bounding box
193
+ # expansion_factor = 0.01
194
+ x = coord[0]
195
+ y = coord[1]
196
+ w = coord[2] - coord[0]
197
+ h = coord[3] - coord[1]
198
+ # sub_image = image[y:y + h, x:x + w]
199
+ print("x, y, w, h", x, y, w, h)
200
+
201
+ print('width_pdf', width_pdf)
202
+ print('height_pdf', height_pdf)
203
+
204
+ enlarge_factor = 0.0
205
+ pdf_x = x * width_pdf
206
+ pdf_y = y * height_pdf + enlarge_factor * height_pdf
207
+ pdf_w = w * width_pdf + enlarge_factor * width_pdf
208
+ pdf_h = h * height_pdf
209
+ print("PDF", pdf_x, pdf_y, pdf_w, pdf_h)
210
+
211
+ img_x = int(x * image.width)
212
+ img_y = int(y * image.height)
213
+ img_w = int(w * image.width)
214
+ img_h = int(h * image.height)
215
+ print("IMG", img_x, img_y, img_w, img_h)
216
+
217
+ text = textpage.get_text_bounded(left=pdf_x,
218
+ bottom=height_pdf - pdf_y - pdf_h,
219
+ right=pdf_x + pdf_w,
220
+ top=height_pdf - pdf_y)
221
+
222
+
223
+ print("TEXT", text)
224
+
225
+
226
+ if search_google:
227
+ # Use Google Search API to find items with the SKU
228
+ # keep only the first 2 lines of text to search
229
+ lines = text.split('\n')
230
+ # delete the \r in all lines
231
+ lines = [line.replace('\r', '') for line in lines]
232
+ # remove the blank spaces at the beginning and end of each line
233
+ lines = [line.strip() for line in lines]
234
+ text = '\n'.join(lines[:2])
235
+ # if the text is less than 3 words, add the next line
236
+ if len(text.split(' ')) < 2:
237
+ text = '\n'.join(lines[:3])
238
+ product_name = text
239
+
240
+ to_seach = str(selected_shop) + ' ' + text + str(selected_shop)
241
+ print("TO SEARCH: ", to_seach)
242
+ request = build_request(to_seach)
243
+ search_query = make_request(request)
244
+ print(f"First Result URL: {search_query}")
245
+ # search_query = "https://www.google.com/search?q="
246
+
247
+ sku.append(product_name)
248
+ # if search_query not in hotspots_sku_links:
249
+ hotspots_sku_links.append([{'sku': product_name, 'url': search_query, 'product_name': product_name}])
250
+ hostpots.append(((img_x, img_y, (img_x + img_w), (img_h+img_y)), str(product_name)))
251
+ print("----- ADDED ------")
252
+
253
+
254
+
255
+ print("hotspots_sku_links: ", hotspots_sku_links)
256
+ print("skus: ", sku)
257
+
258
+ for data in hotspots_sku_links:
259
+ for item in data:
260
+ product_table_data.append({
261
+ 'Product Name': item['product_name'] if item['product_name'] else "Not Found",
262
+ 'URL': item['url']
263
+ })
264
+
265
+
266
+ # Create a DataFrame
267
+ product_dataframe_data = pd.DataFrame(product_table_data)
268
+
269
+ # flattern the list hotspots_sku_links
270
+ # hotspots_sku_links = [item for sublist in hotspots_sku_links for item in sublist]
271
+ print("hotspots_sku_links: ", hotspots_sku_links)
272
+ print('length of hotspots_sku_links: ', len(hotspots_sku_links))
273
+ assert len(hotspots_sku_links) == len(sku)
274
+ return (image, hostpots), product_dataframe_data
275
+
276
+
277
+ section_btn.click(section, [pdf_input, selected_shop, confidence_threshold], [img_output, product_dataframe])
278
+
279
+ def select_section(evt: gr.SelectData):
280
+ hotspot_data = hotspots_sku_links[evt.index]
281
+ markdown_output = ""
282
+ for data in hotspot_data:
283
+ url = data['url']
284
+ product_name = data['product_name'] if data['product_name'] else "No product name found"
285
+ if product_name != "No product name found":
286
+ markdown_output += f"[{product_name}]({url})\n\n"
287
+ else:
288
+ markdown_output += f"[SKU: {data['sku']}]({url})\n\n"
289
+ return markdown_output
290
+
291
+ img_output.select(select_section, None, selected_section)
292
+
293
+ if __name__ == "__main__":
294
+ demo.launch(share=False)