Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -19,7 +19,7 @@ from ultralytics.utils.plotting import save_one_box
|
|
| 19 |
from transformers import AutoModelForImageSegmentation
|
| 20 |
from torchvision import transforms
|
| 21 |
from scalingtestupdated import calculate_scaling_factor
|
| 22 |
-
from shapely.geometry import Polygon, Point, MultiPolygon
|
| 23 |
from scipy.interpolate import splprep, splev
|
| 24 |
from scipy.ndimage import gaussian_filter1d
|
| 25 |
from u2net import U2NETP
|
|
@@ -270,6 +270,8 @@ def build_tool_polygon(points_inch):
|
|
| 270 |
return Polygon(points_inch)
|
| 271 |
|
| 272 |
def polygon_to_exterior_coords(poly: Polygon):
|
|
|
|
|
|
|
| 273 |
if poly.geom_type == "MultiPolygon":
|
| 274 |
biggest = max(poly.geoms, key=lambda g: g.area)
|
| 275 |
poly = biggest
|
|
@@ -363,6 +365,8 @@ def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width
|
|
| 363 |
max_x = -float("inf")
|
| 364 |
max_y = -float("inf")
|
| 365 |
for poly in polygons_inch:
|
|
|
|
|
|
|
| 366 |
b = poly.bounds
|
| 367 |
min_x = min(min_x, b[0])
|
| 368 |
min_y = min(min_y, b[1])
|
|
@@ -402,18 +406,24 @@ def add_rectangular_boundary(doc, polygons_inch, boundary_length, boundary_width
|
|
| 402 |
msp.add_lwpolyline(rect_coords, close=True, dxfattribs={"layer": "BOUNDARY"})
|
| 403 |
return boundary_polygon
|
| 404 |
|
| 405 |
-
def draw_polygons_inch(polygons_inch, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
|
| 406 |
-
for poly in polygons_inch:
|
| 407 |
-
if poly.geom_type == "MultiPolygon":
|
| 408 |
-
for subpoly in poly.geoms:
|
| 409 |
-
draw_single_polygon(subpoly, image_rgb, scaling_factor, image_height, color, thickness)
|
| 410 |
-
else:
|
| 411 |
-
draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color, thickness)
|
| 412 |
-
|
| 413 |
def draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 414 |
ext = list(poly.exterior.coords)
|
| 415 |
if len(ext) < 3:
|
| 416 |
return
|
|
|
|
| 417 |
pts_px = []
|
| 418 |
for (x_in, y_in) in ext:
|
| 419 |
px = int(x_in / scaling_factor)
|
|
@@ -422,6 +432,10 @@ def draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color=(0,
|
|
| 422 |
pts_px = np.array(pts_px, dtype=np.int32)
|
| 423 |
cv2.polylines(image_rgb, [pts_px], isClosed=True, color=color, thickness=thickness, lineType=cv2.LINE_AA)
|
| 424 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 425 |
# ---------------------
|
| 426 |
# Main Predict Function with Finger Cut Clearance, Boundary Box, Annotation and Sharpness Enhancement
|
| 427 |
# ---------------------
|
|
@@ -590,6 +604,8 @@ def predict(
|
|
| 590 |
inner_max_x = -float("inf")
|
| 591 |
inner_max_y = -float("inf")
|
| 592 |
for poly in final_polygons_inch:
|
|
|
|
|
|
|
| 593 |
b = poly.bounds
|
| 594 |
inner_min_x = min(inner_min_x, b[0])
|
| 595 |
inner_min_y = min(inner_min_y, b[1])
|
|
|
|
| 19 |
from transformers import AutoModelForImageSegmentation
|
| 20 |
from torchvision import transforms
|
| 21 |
from scalingtestupdated import calculate_scaling_factor
|
| 22 |
+
from shapely.geometry import Polygon, Point, MultiPolygon, GeometryCollection
|
| 23 |
from scipy.interpolate import splprep, splev
|
| 24 |
from scipy.ndimage import gaussian_filter1d
|
| 25 |
from u2net import U2NETP
|
|
|
|
| 270 |
return Polygon(points_inch)
|
| 271 |
|
| 272 |
def polygon_to_exterior_coords(poly: Polygon):
|
| 273 |
+
if poly.geom_type == "GeometryCollection":
|
| 274 |
+
return []
|
| 275 |
if poly.geom_type == "MultiPolygon":
|
| 276 |
biggest = max(poly.geoms, key=lambda g: g.area)
|
| 277 |
poly = biggest
|
|
|
|
| 365 |
max_x = -float("inf")
|
| 366 |
max_y = -float("inf")
|
| 367 |
for poly in polygons_inch:
|
| 368 |
+
if poly.geom_type == "GeometryCollection":
|
| 369 |
+
continue
|
| 370 |
b = poly.bounds
|
| 371 |
min_x = min(min_x, b[0])
|
| 372 |
min_y = min(min_y, b[1])
|
|
|
|
| 406 |
msp.add_lwpolyline(rect_coords, close=True, dxfattribs={"layer": "BOUNDARY"})
|
| 407 |
return boundary_polygon
|
| 408 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
def draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
|
| 410 |
+
# Handle different geometry types
|
| 411 |
+
if poly.geom_type == "GeometryCollection":
|
| 412 |
+
return
|
| 413 |
+
elif poly.geom_type == "MultiPolygon":
|
| 414 |
+
for geom in poly.geoms:
|
| 415 |
+
draw_single_polygon(geom, image_rgb, scaling_factor, image_height, color, thickness)
|
| 416 |
+
return
|
| 417 |
+
elif poly.geom_type not in ["Polygon", "LinearRing"]:
|
| 418 |
+
return
|
| 419 |
+
|
| 420 |
+
if not hasattr(poly, 'exterior') or not poly.exterior:
|
| 421 |
+
return
|
| 422 |
+
|
| 423 |
ext = list(poly.exterior.coords)
|
| 424 |
if len(ext) < 3:
|
| 425 |
return
|
| 426 |
+
|
| 427 |
pts_px = []
|
| 428 |
for (x_in, y_in) in ext:
|
| 429 |
px = int(x_in / scaling_factor)
|
|
|
|
| 432 |
pts_px = np.array(pts_px, dtype=np.int32)
|
| 433 |
cv2.polylines(image_rgb, [pts_px], isClosed=True, color=color, thickness=thickness, lineType=cv2.LINE_AA)
|
| 434 |
|
| 435 |
+
def draw_polygons_inch(polygons_inch, image_rgb, scaling_factor, image_height, color=(0,0,255), thickness=2):
|
| 436 |
+
for poly in polygons_inch:
|
| 437 |
+
draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color, thickness)
|
| 438 |
+
|
| 439 |
# ---------------------
|
| 440 |
# Main Predict Function with Finger Cut Clearance, Boundary Box, Annotation and Sharpness Enhancement
|
| 441 |
# ---------------------
|
|
|
|
| 604 |
inner_max_x = -float("inf")
|
| 605 |
inner_max_y = -float("inf")
|
| 606 |
for poly in final_polygons_inch:
|
| 607 |
+
if poly.geom_type == "GeometryCollection":
|
| 608 |
+
continue
|
| 609 |
b = poly.bounds
|
| 610 |
inner_min_x = min(inner_min_x, b[0])
|
| 611 |
inner_min_y = min(inner_min_y, b[1])
|