Spaces:
No application file
No application file
Commit ·
d20170b
1
Parent(s): 8040a44
works with 1 page pdf canadian tire :)
Browse files- app.py +1 -1
- app_file.py +61 -20
app.py
CHANGED
|
@@ -12,7 +12,7 @@ import gradio as gr
|
|
| 12 |
from ultralytics import YOLO
|
| 13 |
|
| 14 |
|
| 15 |
-
search_google =
|
| 16 |
# GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
|
| 17 |
# GOOGLE_CSE_ID = "b536303d151b94476"
|
| 18 |
# GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
|
|
|
|
| 12 |
from ultralytics import YOLO
|
| 13 |
|
| 14 |
|
| 15 |
+
search_google = False
|
| 16 |
# GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
|
| 17 |
# GOOGLE_CSE_ID = "b536303d151b94476"
|
| 18 |
# GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
|
app_file.py
CHANGED
|
@@ -12,7 +12,7 @@ import gradio as gr
|
|
| 12 |
from ultralytics import YOLO
|
| 13 |
import pypdfium2 as pdfium
|
| 14 |
|
| 15 |
-
search_google =
|
| 16 |
# GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
|
| 17 |
# GOOGLE_CSE_ID = "b536303d151b94476"
|
| 18 |
# GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
|
|
@@ -76,14 +76,20 @@ def pdf_to_image(pdf):
|
|
| 76 |
assert n_pages > 0, "The PDF document is empty"
|
| 77 |
assert n_pages == 1, "The PDF document has more than one page"
|
| 78 |
page = pdf.get_page(0)
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
width_img, height_img = pil_image.size
|
| 81 |
-
print("width", width_img, "height", height_img)
|
| 82 |
-
|
| 83 |
-
print("width",
|
| 84 |
-
assert int(
|
| 85 |
-
assert int(
|
| 86 |
-
return pil_image,
|
| 87 |
|
| 88 |
def app(pdf, selected_shop):
|
| 89 |
if pdf is None:
|
|
@@ -165,13 +171,16 @@ with gr.Blocks() as demo:
|
|
| 165 |
# texts = []
|
| 166 |
sku = []
|
| 167 |
|
| 168 |
-
image,
|
| 169 |
image_name ="test.jpg"
|
| 170 |
image.save(image_name)
|
| 171 |
|
| 172 |
# # Load a text page helper
|
| 173 |
textpage = first_page.get_textpage()
|
| 174 |
|
|
|
|
|
|
|
|
|
|
| 175 |
#print("selected_shop:", selected_shop)
|
| 176 |
search_url = shops[selected_shop]['url']
|
| 177 |
ref_number_regex = shops[selected_shop]['regex']
|
|
@@ -181,34 +190,60 @@ with gr.Blocks() as demo:
|
|
| 181 |
brands = [brand.upper() for brand in brands]
|
| 182 |
|
| 183 |
if mode == 'rbf':
|
| 184 |
-
predictions = get_yolo_predictions(
|
| 185 |
else:
|
| 186 |
# local inference
|
| 187 |
results = get_yolo_predictions_local(image, confidence_threshold)
|
| 188 |
for result in results:
|
| 189 |
-
coordinates = result.boxes.
|
| 190 |
coordinates = coordinates.to("cpu")
|
| 191 |
coordinates = coordinates.numpy()
|
| 192 |
print("FOUND {} objects".format(len(coordinates)))
|
|
|
|
| 193 |
for idx, coord in enumerate(coordinates):
|
| 194 |
# crop the image to the bounding box
|
| 195 |
# expansion_factor = 0.01
|
| 196 |
-
x =
|
| 197 |
-
y =
|
| 198 |
-
w =
|
| 199 |
-
h =
|
| 200 |
# sub_image = image[y:y + h, x:x + w]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 201 |
|
| 202 |
|
| 203 |
if shops[selected_shop]['url'] is None or shops[selected_shop]['url'] == "":
|
| 204 |
# just append the hotspot to hostpots
|
| 205 |
-
hostpots.append(((
|
| 206 |
continue
|
| 207 |
|
| 208 |
print('---------------------')
|
| 209 |
# Extract text
|
| 210 |
-
text = textpage.get_text_bounded(left=
|
| 211 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 212 |
print("TEXT", text)
|
| 213 |
|
| 214 |
# for object_number, text in enumerate(texts, start=1):
|
|
@@ -290,6 +325,7 @@ with gr.Blocks() as demo:
|
|
| 290 |
|
| 291 |
#print("PRODUCT NAME: ", product_name)
|
| 292 |
|
|
|
|
| 293 |
for ref in ref_numbers:
|
| 294 |
if search_google:
|
| 295 |
# Use Google Search API to find items with the SKU
|
|
@@ -305,8 +341,13 @@ with gr.Blocks() as demo:
|
|
| 305 |
if ref not in sku:
|
| 306 |
sku.append(ref)
|
| 307 |
# if search_query not in hotspots_sku_links:
|
| 308 |
-
|
| 309 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 310 |
|
| 311 |
print("hotspots_sku_links: ", hotspots_sku_links)
|
| 312 |
print("skus: ", sku)
|
|
|
|
| 12 |
from ultralytics import YOLO
|
| 13 |
import pypdfium2 as pdfium
|
| 14 |
|
| 15 |
+
search_google = False
|
| 16 |
# GOOGLE_API_KEY = "AIzaSyCD5eIu3N2uD6Vf5GE0VLBpkPsJ3mEZgA0"
|
| 17 |
# GOOGLE_CSE_ID = "b536303d151b94476"
|
| 18 |
# GOOGLE_API_KEY = "AIzaSyBUvPKAZa2TddKYDJI9t0ac2A_JJqdgWHo"
|
|
|
|
| 76 |
assert n_pages > 0, "The PDF document is empty"
|
| 77 |
assert n_pages == 1, "The PDF document has more than one page"
|
| 78 |
page = pdf.get_page(0)
|
| 79 |
+
bitmap = page.render(
|
| 80 |
+
scale=5, # 72dpi resolution
|
| 81 |
+
rotation=0, # no additional rotation
|
| 82 |
+
# ... further rendering options
|
| 83 |
+
)
|
| 84 |
+
pil_image = bitmap.to_pil()
|
| 85 |
+
pil_image.save('test.png')
|
| 86 |
width_img, height_img = pil_image.size
|
| 87 |
+
print("width image", width_img, "height image", height_img)
|
| 88 |
+
width_pdf, height_pdf = page.get_size()
|
| 89 |
+
print("width pdf ", width_pdf, "height pdf ", height_pdf)
|
| 90 |
+
#assert int(width_pdf) == width_img, "The width of the image is not the same as the width of the page"
|
| 91 |
+
#assert int(height_pdf) == height_img, "The height of the image is not the same as the height of the page"
|
| 92 |
+
return pil_image, width_pdf, height_pdf, page
|
| 93 |
|
| 94 |
def app(pdf, selected_shop):
|
| 95 |
if pdf is None:
|
|
|
|
| 171 |
# texts = []
|
| 172 |
sku = []
|
| 173 |
|
| 174 |
+
image, width_pdf, height_pdf, first_page = pdf_to_image(pdf)
|
| 175 |
image_name ="test.jpg"
|
| 176 |
image.save(image_name)
|
| 177 |
|
| 178 |
# # Load a text page helper
|
| 179 |
textpage = first_page.get_textpage()
|
| 180 |
|
| 181 |
+
text_all = textpage.get_text_range()
|
| 182 |
+
print("text_all", text_all)
|
| 183 |
+
|
| 184 |
#print("selected_shop:", selected_shop)
|
| 185 |
search_url = shops[selected_shop]['url']
|
| 186 |
ref_number_regex = shops[selected_shop]['regex']
|
|
|
|
| 190 |
brands = [brand.upper() for brand in brands]
|
| 191 |
|
| 192 |
if mode == 'rbf':
|
| 193 |
+
predictions = get_yolo_predictions(image)
|
| 194 |
else:
|
| 195 |
# local inference
|
| 196 |
results = get_yolo_predictions_local(image, confidence_threshold)
|
| 197 |
for result in results:
|
| 198 |
+
coordinates = result.boxes.xyxyn
|
| 199 |
coordinates = coordinates.to("cpu")
|
| 200 |
coordinates = coordinates.numpy()
|
| 201 |
print("FOUND {} objects".format(len(coordinates)))
|
| 202 |
+
# print("coordinates", coordinates)
|
| 203 |
for idx, coord in enumerate(coordinates):
|
| 204 |
# crop the image to the bounding box
|
| 205 |
# expansion_factor = 0.01
|
| 206 |
+
x = coord[0]
|
| 207 |
+
y = coord[1]
|
| 208 |
+
w = coord[2] - coord[0]
|
| 209 |
+
h = coord[3] - coord[1]
|
| 210 |
# sub_image = image[y:y + h, x:x + w]
|
| 211 |
+
print("x, y, w, h", x, y, w, h)
|
| 212 |
+
|
| 213 |
+
print('width_pdf', width_pdf)
|
| 214 |
+
print('height_pdf', height_pdf)
|
| 215 |
+
|
| 216 |
+
enlarge_factor = 0.01
|
| 217 |
+
pdf_x = x * width_pdf
|
| 218 |
+
pdf_y = y * height_pdf + enlarge_factor * height_pdf
|
| 219 |
+
pdf_w = w * width_pdf + enlarge_factor * width_pdf
|
| 220 |
+
pdf_h = h * height_pdf
|
| 221 |
+
print("PDF", pdf_x, pdf_y, pdf_w, pdf_h)
|
| 222 |
+
|
| 223 |
+
img_x = int(x * image.width)
|
| 224 |
+
img_y = int(y * image.height)
|
| 225 |
+
img_w = int(w * image.width)
|
| 226 |
+
img_h = int(h * image.height)
|
| 227 |
+
print("IMG", img_x, img_y, img_w, img_h)
|
| 228 |
|
| 229 |
|
| 230 |
if shops[selected_shop]['url'] is None or shops[selected_shop]['url'] == "":
|
| 231 |
# just append the hotspot to hostpots
|
| 232 |
+
hostpots.append(((img_x, img_y, img_x + img_w, img_y + img_h), str(idx)))
|
| 233 |
continue
|
| 234 |
|
| 235 |
print('---------------------')
|
| 236 |
# Extract text
|
| 237 |
+
# text = textpage.get_text_bounded(left=pdf_x - enlarge_factor * width_pdf,
|
| 238 |
+
# bottom=height_pdf - pdf_y - pdf_h - (enlarge_factor*height_pdf),
|
| 239 |
+
# right=pdf_x+pdf_w + (enlarge_factor*width_pdf),
|
| 240 |
+
# top=height_pdf-pdf_y + (enlarge_factor*height_pdf))
|
| 241 |
+
text = textpage.get_text_bounded(left=pdf_x + 50,
|
| 242 |
+
bottom=height_pdf - pdf_y - pdf_h,
|
| 243 |
+
right=pdf_x + pdf_w + 50,
|
| 244 |
+
top=height_pdf - pdf_y)
|
| 245 |
+
|
| 246 |
+
|
| 247 |
print("TEXT", text)
|
| 248 |
|
| 249 |
# for object_number, text in enumerate(texts, start=1):
|
|
|
|
| 325 |
|
| 326 |
#print("PRODUCT NAME: ", product_name)
|
| 327 |
|
| 328 |
+
print("FOUND {} ref numbers".format(len(ref_numbers)))
|
| 329 |
for ref in ref_numbers:
|
| 330 |
if search_google:
|
| 331 |
# Use Google Search API to find items with the SKU
|
|
|
|
| 341 |
if ref not in sku:
|
| 342 |
sku.append(ref)
|
| 343 |
# if search_query not in hotspots_sku_links:
|
| 344 |
+
hotspots_sku_links.append([{'sku': ref, 'url': search_query, 'prices': prices, 'product_name': product_name}])
|
| 345 |
+
hostpots.append(((img_x, img_y, (img_x + img_w), (img_h+img_y)), str(ref)))
|
| 346 |
+
print("----- ADDED ------")
|
| 347 |
+
if len(ref_numbers) == 0:
|
| 348 |
+
hostpots.append(((img_x, img_y, (img_x + img_w), (img_h+img_y)), str(idx)))
|
| 349 |
+
hotspots_sku_links.append([{'sku': None, 'url': None, 'prices': None, 'product_name': None}])
|
| 350 |
+
print("++++++ ADDED +++++")
|
| 351 |
|
| 352 |
print("hotspots_sku_links: ", hotspots_sku_links)
|
| 353 |
print("skus: ", sku)
|