SelimGilon commited on
Commit
fadf8a7
·
1 Parent(s): 85ee3ea

works well

Browse files
Files changed (1) hide show
  1. app.py +33 -53
app.py CHANGED
@@ -22,16 +22,15 @@ else:
22
  project = rf.workspace().project("smartflyers")
23
  model = project.version(9).model
24
 
 
 
 
 
25
  shop = None
26
-
27
- def process(selected_shop):
28
- print(selected_shop)
29
- return selected_shop
30
-
31
  shops = {
32
  'Latulippe': {
33
  'url': 'https://latulippe.com/fr/recherche/?keywords=',
34
- 'regex': r'N° (\w+)'
35
  },
36
  'Decathlon': {
37
  'url': 'https://www.decathlon.fr/search?Ntt=',
@@ -40,27 +39,11 @@ shops = {
40
  # Add more shops and their respective information here
41
  }
42
 
43
- hotspots_sku_links = []
44
-
45
- section_labels = [
46
- "apple",
47
- "banana",
48
- "carrot",
49
- "donut",
50
- "eggplant",
51
- "fish",
52
- "grapes",
53
- "hamburger",
54
- "ice cream",
55
- "juice",
56
- ]
57
-
58
  def app(img, selected_shop):
59
  if img is None:
60
  raise gr.Error("No file uploaded. Please upload an image.")
61
 
62
  # Check if the uploaded file is a PDF
63
-
64
  if selected_shop is None:
65
  raise gr.Error("Please select a shop.")
66
 
@@ -73,38 +56,33 @@ def app(img, selected_shop):
73
  return image_annotated, hotspots_sku_links[selected_shop]
74
 
75
 
76
- iiface = gr.Interface(
77
- fn=app,
78
- inputs=[gr.Dropdown(choices=list(shops.keys()), label="Select Shop"), gr.Image(label="Input page (image)", sources=["upload", "webcam"])],
79
-
80
- outputs= [gr.AnnotatedImage(label="Annotated page (image)"), gr.Textbox(label="Selected Hotspot")],
81
- title="Hotspot Identifier",
82
- description="Identify hotspots on a page",
83
- )
84
 
85
 
86
  with gr.Blocks() as demo:
87
 
88
-
89
  with gr.Row():
90
  selected_shop = gr.Dropdown(shops.keys(), label="Shop")
91
 
92
-
93
  with gr.Row():
94
  img_input = gr.Image(label="Input page (image)", sources=["upload", "webcam"])
95
  img_output = gr.AnnotatedImage(label="Annotated page (image)")
96
 
97
  section_btn = gr.Button("Identify Hotspots")
98
- selected_section = gr.Textbox(label="Selected Hotspot")
99
-
100
-
101
  def get_yolo_predictions(image):
102
  # Use Roboflow API to get predictions
103
  predictions = model.predict(image, confidence=0.1, overlap=10).json()
104
  # print("predictions", predictions)
105
  return predictions
106
 
107
-
108
  def get_yolo_predictions_local(image):
109
  # Perform object detection on an image using the model
110
  predictions = model(image, conf=0.1)
@@ -117,13 +95,15 @@ with gr.Blocks() as demo:
117
  text = pytesseract.image_to_string(grey, lang='eng')
118
  return text
119
 
120
- def section(img):
121
  hostpots = []
122
  texts = []
123
  sku = []
124
 
125
- search_url = 'https://latulippe.com/fr/recherche/?keywords='
126
- ref_number_regex = r'N° ?M(\d+)'
 
 
127
 
128
  if mode == 'rbf':
129
  predictions = get_yolo_predictions(img)
@@ -143,15 +123,14 @@ with gr.Blocks() as demo:
143
  text = ocr(sub_image)
144
  texts.append(text)
145
 
146
- print("bbox", idx)
147
  for object_number, text in enumerate(texts, start=1):
148
  print("object", object_number)
149
- print("text", text)
150
  ref_numbers = re.findall(ref_number_regex, text)
151
  for ref in ref_numbers:
152
- print("ref", ref)
153
  # append 'M' before the ref number
154
- ref = 'M' + ref
155
  search_query = search_url + urllib.parse.quote(ref)
156
  if ref not in sku:
157
  sku.append(ref)
@@ -161,9 +140,7 @@ with gr.Blocks() as demo:
161
 
162
  return (img, hostpots)
163
  else:
164
- # get the height and width of the image
165
-
166
- height, width, channels = img.shape
167
  results = get_yolo_predictions_local(img)
168
  for result in results:
169
  coordinates = result.boxes.xyxy
@@ -186,26 +163,29 @@ with gr.Blocks() as demo:
186
  print("text", text)
187
  ref_numbers = re.findall(ref_number_regex, text)
188
  for ref in ref_numbers:
189
- print("ref", ref)
190
  # append 'M' before the ref number
191
- ref = 'M' + ref
192
  search_query = search_url + urllib.parse.quote(ref)
193
  if ref not in sku:
194
  sku.append(ref)
195
  # if search_query not in hotspots_sku_links:
196
  hotspots_sku_links.append(search_query)
197
  hostpots.append(((int(coord[0]), int(coord[1]), int(coord[2]), int(coord[3])), str(ref)))
198
- print("hotspots_sku_links", len(), hotspots_sku_links)
199
- print("sku", sku)
200
- print("hostpots", hostpots)
201
  return (img, hostpots)
202
 
203
 
204
 
205
- section_btn.click(section, [img_input], img_output)
 
 
 
206
 
207
  def select_section(evt: gr.SelectData):
208
- return hotspots_sku_links[evt.index]
 
 
209
 
210
  img_output.select(select_section, None, selected_section)
211
 
 
22
  project = rf.workspace().project("smartflyers")
23
  model = project.version(9).model
24
 
25
+ # def process(selected_shop):
26
+ # print(selected_shop)
27
+ # return selected_shop
28
+ hotspots_sku_links = []
29
  shop = None
 
 
 
 
 
30
  shops = {
31
  'Latulippe': {
32
  'url': 'https://latulippe.com/fr/recherche/?keywords=',
33
+ 'regex': r'N° (M\d+)'
34
  },
35
  'Decathlon': {
36
  'url': 'https://www.decathlon.fr/search?Ntt=',
 
39
  # Add more shops and their respective information here
40
  }
41
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42
  def app(img, selected_shop):
43
  if img is None:
44
  raise gr.Error("No file uploaded. Please upload an image.")
45
 
46
  # Check if the uploaded file is a PDF
 
47
  if selected_shop is None:
48
  raise gr.Error("Please select a shop.")
49
 
 
56
  return image_annotated, hotspots_sku_links[selected_shop]
57
 
58
 
59
+ # iiface = gr.Interface(
60
+ # fn=app,
61
+ # inputs=[gr.Dropdown(choices=list(shops.keys()), label="Select Shop"), gr.Image(label="Input page (image)", sources=["upload", "webcam"])],
62
+ # outputs= [gr.AnnotatedImage(label="Annotated page (image)"), gr.Textbox(label="Selected Hotspot")],
63
+ # title="Hotspot Identifier",
64
+ # description="Identify hotspots on a page",
65
+ # )
 
66
 
67
 
68
  with gr.Blocks() as demo:
69
 
 
70
  with gr.Row():
71
  selected_shop = gr.Dropdown(shops.keys(), label="Shop")
72
 
 
73
  with gr.Row():
74
  img_input = gr.Image(label="Input page (image)", sources=["upload", "webcam"])
75
  img_output = gr.AnnotatedImage(label="Annotated page (image)")
76
 
77
  section_btn = gr.Button("Identify Hotspots")
78
+ # selected_section = gr.Textbox(label="Selected Hotspot")
79
+ selected_section = gr.Markdown(label="Selected Hotspot")
 
80
  def get_yolo_predictions(image):
81
  # Use Roboflow API to get predictions
82
  predictions = model.predict(image, confidence=0.1, overlap=10).json()
83
  # print("predictions", predictions)
84
  return predictions
85
 
 
86
  def get_yolo_predictions_local(image):
87
  # Perform object detection on an image using the model
88
  predictions = model(image, conf=0.1)
 
95
  text = pytesseract.image_to_string(grey, lang='eng')
96
  return text
97
 
98
+ def section(img, selected_shop):
99
  hostpots = []
100
  texts = []
101
  sku = []
102
 
103
+ print("selected_shop:", selected_shop)
104
+ search_url = shops[selected_shop]['url']
105
+ ref_number_regex = shops[selected_shop]['regex']
106
+ print("search_url:", search_url)
107
 
108
  if mode == 'rbf':
109
  predictions = get_yolo_predictions(img)
 
123
  text = ocr(sub_image)
124
  texts.append(text)
125
 
126
+ # print("bbox", idx)
127
  for object_number, text in enumerate(texts, start=1):
128
  print("object", object_number)
129
+ # print("text", text)
130
  ref_numbers = re.findall(ref_number_regex, text)
131
  for ref in ref_numbers:
132
+ print("SKU:", ref)
133
  # append 'M' before the ref number
 
134
  search_query = search_url + urllib.parse.quote(ref)
135
  if ref not in sku:
136
  sku.append(ref)
 
140
 
141
  return (img, hostpots)
142
  else:
143
+ # local inference
 
 
144
  results = get_yolo_predictions_local(img)
145
  for result in results:
146
  coordinates = result.boxes.xyxy
 
163
  print("text", text)
164
  ref_numbers = re.findall(ref_number_regex, text)
165
  for ref in ref_numbers:
166
+ print("SKU:", ref)
167
  # append 'M' before the ref number
 
168
  search_query = search_url + urllib.parse.quote(ref)
169
  if ref not in sku:
170
  sku.append(ref)
171
  # if search_query not in hotspots_sku_links:
172
  hotspots_sku_links.append(search_query)
173
  hostpots.append(((int(coord[0]), int(coord[1]), int(coord[2]), int(coord[3])), str(ref)))
174
+ print("hotspots_sku_links: ", hotspots_sku_links)
175
+ print("skus: ", sku)
 
176
  return (img, hostpots)
177
 
178
 
179
 
180
+ section_btn.click(section, [img_input, selected_shop], img_output)
181
+
182
+ # def select_section(evt: gr.SelectData):
183
+ # return hotspots_sku_links[evt.index]
184
 
185
  def select_section(evt: gr.SelectData):
186
+ # Format the URL as a Markdown link
187
+ url = hotspots_sku_links[evt.index]
188
+ return f"[Click here to visit the selected hotspot's link]({url})"
189
 
190
  img_output.select(select_section, None, selected_section)
191