Spaces:
Sleeping
Sleeping
automatic zoom
Browse files- app.py +67 -2
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -418,6 +418,9 @@ from transformers import (
|
|
| 418 |
)
|
| 419 |
import re
|
| 420 |
import xml.etree.ElementTree as ET
|
|
|
|
|
|
|
|
|
|
| 421 |
|
| 422 |
|
| 423 |
# ======================
|
|
@@ -548,10 +551,72 @@ def recolor_svg(svg_text: str, predicted_ids: set[str]) -> str:
|
|
| 548 |
|
| 549 |
return ET.tostring(root, encoding="unicode")
|
| 550 |
|
| 551 |
-
def
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 552 |
root = ET.fromstring(svg_text)
|
| 553 |
root.attrib["viewBox"] = " ".join(str(x) for x in viewbox)
|
| 554 |
-
# Ensure it scales nicely
|
| 555 |
root.attrib["preserveAspectRatio"] = "xMidYMid meet"
|
| 556 |
return ET.tostring(root, encoding="unicode")
|
| 557 |
|
|
|
|
| 418 |
)
|
| 419 |
import re
|
| 420 |
import xml.etree.ElementTree as ET
|
| 421 |
+
import numpy as np
|
| 422 |
+
import xml.etree.ElementTree as ET
|
| 423 |
+
from svgpathtools import parse_path
|
| 424 |
|
| 425 |
|
| 426 |
# ======================
|
|
|
|
| 551 |
|
| 552 |
return ET.tostring(root, encoding="unicode")
|
| 553 |
|
| 554 |
+
def compute_viewbox_from_ids(svg_text: str, ids: set[str], margin_ratio: float = 0.08):
|
| 555 |
+
"""
|
| 556 |
+
Compute a tight viewBox around the given country ids based on their path geometry.
|
| 557 |
+
Returns (min_x, min_y, width, height)
|
| 558 |
+
"""
|
| 559 |
+
root = ET.fromstring(svg_text)
|
| 560 |
+
|
| 561 |
+
xmin, ymin = np.inf, np.inf
|
| 562 |
+
xmax, ymax = -np.inf, -np.inf
|
| 563 |
+
|
| 564 |
+
# Find all path/polygon with id in ids
|
| 565 |
+
for el in root.iter():
|
| 566 |
+
el_id = el.attrib.get("id")
|
| 567 |
+
if not el_id or el_id not in ids:
|
| 568 |
+
continue
|
| 569 |
+
|
| 570 |
+
tag = el.tag.split("}")[-1].lower()
|
| 571 |
+
|
| 572 |
+
if tag == "path":
|
| 573 |
+
d = el.attrib.get("d")
|
| 574 |
+
if not d:
|
| 575 |
+
continue
|
| 576 |
+
p = parse_path(d)
|
| 577 |
+
# svgpathtools bbox: (xmin, xmax, ymin, ymax)
|
| 578 |
+
bxmin, bxmax, bymin, bymax = p.bbox()
|
| 579 |
+
xmin = min(xmin, bxmin)
|
| 580 |
+
xmax = max(xmax, bxmax)
|
| 581 |
+
ymin = min(ymin, bymin)
|
| 582 |
+
ymax = max(ymax, bymax)
|
| 583 |
+
|
| 584 |
+
elif tag == "polygon":
|
| 585 |
+
pts = el.attrib.get("points", "").strip()
|
| 586 |
+
if not pts:
|
| 587 |
+
continue
|
| 588 |
+
coords = []
|
| 589 |
+
for chunk in pts.replace(",", " ").split():
|
| 590 |
+
coords.append(float(chunk))
|
| 591 |
+
xs = coords[0::2]
|
| 592 |
+
ys = coords[1::2]
|
| 593 |
+
xmin = min(xmin, min(xs))
|
| 594 |
+
xmax = max(xmax, max(xs))
|
| 595 |
+
ymin = min(ymin, min(ys))
|
| 596 |
+
ymax = max(ymax, max(ys))
|
| 597 |
+
|
| 598 |
+
if not np.isfinite(xmin):
|
| 599 |
+
# Didn't find those ids
|
| 600 |
+
return None
|
| 601 |
+
|
| 602 |
+
w = xmax - xmin
|
| 603 |
+
h = ymax - ymin
|
| 604 |
+
|
| 605 |
+
# Add a margin
|
| 606 |
+
mx = w * margin_ratio
|
| 607 |
+
my = h * margin_ratio
|
| 608 |
+
|
| 609 |
+
xmin -= mx
|
| 610 |
+
ymin -= my
|
| 611 |
+
w += 2 * mx
|
| 612 |
+
h += 2 * my
|
| 613 |
+
|
| 614 |
+
return (float(xmin), float(ymin), float(w), float(h))
|
| 615 |
+
|
| 616 |
+
|
| 617 |
+
def set_viewbox(svg_text: str, viewbox):
|
| 618 |
root = ET.fromstring(svg_text)
|
| 619 |
root.attrib["viewBox"] = " ".join(str(x) for x in viewbox)
|
|
|
|
| 620 |
root.attrib["preserveAspectRatio"] = "xMidYMid meet"
|
| 621 |
return ET.tostring(root, encoding="unicode")
|
| 622 |
|
requirements.txt
CHANGED
|
@@ -1,4 +1,6 @@
|
|
| 1 |
transformers>=4.30.0
|
| 2 |
torch>=2.0.0
|
| 3 |
pandas>=1.5.0
|
| 4 |
-
gradio>=4.0
|
|
|
|
|
|
|
|
|
| 1 |
transformers>=4.30.0
|
| 2 |
torch>=2.0.0
|
| 3 |
pandas>=1.5.0
|
| 4 |
+
gradio>=4.0
|
| 5 |
+
svgpathtools
|
| 6 |
+
numpy
|