Nighty3912 commited on
Commit
2bbf0cf
·
verified ·
1 Parent(s): fc3dd1b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +3 -197
app.py CHANGED
@@ -708,198 +708,6 @@ def draw_single_polygon(poly, image_rgb, scaling_factor, image_height, color=(0,
708
  pts_px = np.array(pts_px, dtype=np.int32)
709
  cv2.polylines(image_rgb, [pts_px], isClosed=True, color=color, thickness=thickness, lineType=cv2.LINE_AA)
710
 
711
- # def draw_and_pad(polygons_inch, scaling_factor,boundary_polygon, padding=50,
712
- # color=(0,0,255), thickness=2):
713
- # """
714
- # - polygons_inch: list of Shapely Polygons in inch units (already including boundary).
715
- # - scaling_factor: inches per pixel.
716
- # - padding: padding in pixels.
717
- # """
718
- # all_x = []
719
- # all_y = []
720
- # pixel_polys = []
721
-
722
- # # 1) Convert to pixel coords and collect bounds
723
- # for poly in polygons_inch:
724
- # coords = list(poly.exterior.coords)
725
- # pts = []
726
- # for x_in, y_in in coords:
727
- # px = int(round(x_in / scaling_factor))
728
- # py = int(round(y_in / scaling_factor))
729
- # pts.append([px, py])
730
- # all_x.append(px)
731
- # all_y.append(py)
732
- # pixel_polys.append(np.array(pts, dtype=np.int32))
733
-
734
- # # 2) Compute canvas size
735
-
736
- # min_x, max_x = min(all_x), max(all_x)
737
- # min_y, max_y = min(all_y), max(all_y)
738
- # width = max_x - min_x + 1
739
- # height = max_y - min_y + 1
740
-
741
- # # 3) Create blank white canvas
742
- # canvas = 255 * np.ones((height, width, 3), dtype=np.uint8)
743
-
744
- # # 4) Draw each polygon, flipping y within the local box
745
- # for pts in pixel_polys:
746
- # # Offset so min corner is (0,0)
747
- # pts_off = pts - np.array([[min_x, min_y]])
748
- # # Flip y: new_y = height-1 - old_y
749
- # pts_off[:,1] = (height - 1) - pts_off[:,1]
750
- # cv2.polylines(canvas, [pts_off], isClosed=True,
751
- # color=color, thickness=thickness, lineType=cv2.LINE_AA)
752
-
753
- # # 5) Pad the canvas
754
-
755
- # padded = cv2.copyMakeBorder(
756
- # canvas,
757
- # top=padding, bottom=padding,
758
- # left=padding, right=padding,
759
- # borderType=cv2.BORDER_CONSTANT,
760
- # value=[255,255,255]
761
- # )
762
- # return padded
763
-
764
- # import numpy as np
765
- # import cv2
766
-
767
- # def draw_and_pad(polygons_inch, scaling_factor, boundary_polygon, padding=50,
768
- # color=(0,0,255), thickness=2):
769
- # """
770
- # - polygons_inch: list of Shapely Polygons in inch units.
771
- # - scaling_factor: inches per pixel.
772
- # - boundary_polygon: the Shapely boundary polygon, or None.
773
- # - padding: base padding in pixels.
774
- # """
775
- # all_x, all_y = [], []
776
- # pixel_polys = []
777
-
778
- # # 1) Convert to pixel coords and collect bounds
779
- # for poly in polygons_inch:
780
- # coords = list(poly.exterior.coords)
781
- # pts = []
782
- # for x_in, y_in in coords:
783
- # px = int(round(x_in / scaling_factor))
784
- # py = int(round(y_in / scaling_factor))
785
- # pts.append([px, py])
786
- # all_x.append(px)
787
- # all_y.append(py)
788
- # pixel_polys.append(np.array(pts, dtype=np.int32))
789
-
790
- # # 2) Compute canvas size
791
- # min_x, max_x = min(all_x), max(all_x)
792
- # min_y, max_y = min(all_y), max(all_y)
793
- # width = max_x - min_x + 1
794
- # height = max_y - min_y + 1
795
-
796
- # # 3) Create blank white canvas
797
- # canvas = 255 * np.ones((height, width, 3), dtype=np.uint8)
798
-
799
- # # 4) Draw each polygon, flipping y within the local box
800
- # for pts in pixel_polys:
801
- # pts_off = pts - np.array([[min_x, min_y]])
802
- # pts_off[:,1] = (height - 1) - pts_off[:,1]
803
- # cv2.polylines(canvas, [pts_off], isClosed=True,
804
- # color=color, thickness=thickness, lineType=cv2.LINE_AA)
805
-
806
- # # 5) Decide padding amounts
807
- # if boundary_polygon is not None:
808
- # top = bottom = left = right = padding
809
- # else:
810
- # # Double the padding if there's no boundary, to avoid clipping
811
- # top = bottom = left = right = padding * 2
812
-
813
- # # 6) Pad the canvas
814
- # padded = cv2.copyMakeBorder(
815
- # canvas,
816
- # top=top, bottom=bottom,
817
- # left=left, right=right,
818
- # borderType=cv2.BORDER_CONSTANT,
819
- # value=[255,255,255]
820
- # )
821
- # return padded
822
-
823
- import numpy as np
824
- import cv2
825
-
826
- # def draw_and_pad(polygons_inch, scaling_factor, boundary_polygon, padding=50,
827
- # color=(0, 0, 255), thickness=2):
828
- # """
829
- # Draws Shapely Polygons (in inch units) on a white canvas.
830
-
831
- # When boundary_polygon is None, the computed bounds are expanded by the padding value
832
- # so that the drawn contours are not clipped at the edges after adding the final padding.
833
-
834
- # Arguments:
835
- # polygons_inch: list of Shapely Polygons in inch units (already including boundary).
836
- # scaling_factor: inches per pixel.
837
- # boundary_polygon: the Shapely boundary polygon, or None.
838
- # padding: padding in pixels.
839
- # color: color of the drawn polylines (in BGR format).
840
- # thickness: line thickness.
841
-
842
- # Returns:
843
- # padded: an image (numpy array) of the drawn polygons with an external white border.
844
- # """
845
- # all_x = []
846
- # all_y = []
847
- # pixel_polys = []
848
-
849
- # # 1) Convert each polygon to pixel coordinates and compute overall bounds.
850
- # for poly in polygons_inch:
851
- # coords = list(poly.exterior.coords)
852
- # pts = []
853
- # for x_in, y_in in coords:
854
- # px = int(round(x_in / scaling_factor))
855
- # py = int(round(y_in / scaling_factor))
856
- # pts.append([px, py])
857
- # all_x.append(px)
858
- # all_y.append(py)
859
- # pixel_polys.append(np.array(pts, dtype=np.int32))
860
-
861
- # # 2) Compute the basic canvas size from the polygon bounds.
862
- # min_x, max_x = min(all_x), max(all_x)
863
- # min_y, max_y = min(all_y), max(all_y)
864
-
865
- # # If no boundary polygon is provided, expand the bounds to add margin
866
- # # so that later when we pad externally, the contours do not get clipped.
867
- # if boundary_polygon is None:
868
- # min_x -= padding
869
- # max_x += padding
870
- # min_y -= padding
871
- # max_y += padding
872
-
873
- # width = max_x - min_x + 1
874
- # height = max_y - min_y + 1
875
-
876
- # # 3) Create a blank white canvas.
877
- # canvas = 255 * np.ones((height, width, 3), dtype=np.uint8)
878
-
879
- # # 4) Draw each polygon, flipping the y-coordinates to match image coordinates.
880
- # for pts in pixel_polys:
881
- # # Offset so the minimum corner becomes (0,0) on canvas.
882
- # pts_off = pts - np.array([[min_x, min_y]])
883
- # # Flip y: image coordinates have (0,0) at the top-left.
884
- # pts_off[:, 1] = (height - 1) - pts_off[:, 1]
885
- # cv2.polylines(canvas, [pts_off], isClosed=True,
886
- # color=color, thickness=thickness, lineType=cv2.LINE_AA)
887
-
888
- # # 5) Finally, add external padding on all sides.
889
- # padded = cv2.copyMakeBorder(
890
- # canvas,
891
- # top=padding, bottom=padding,
892
- # left=padding, right=padding,
893
- # borderType=cv2.BORDER_CONSTANT,
894
- # value=[255, 255, 255]
895
- # )
896
-
897
- # return padded
898
-
899
- import numpy as np
900
- import cv2
901
- from shapely.geometry import Polygon
902
-
903
  import numpy as np
904
  import cv2
905
  from shapely.geometry import Polygon
@@ -1211,7 +1019,7 @@ def predict(
1211
  # ---------------------
1212
  # 9) For the preview images, draw the polygons and place text similarly
1213
  # ---------------------
1214
- #draw_polygons_inch(final_polygons_inch, output_img, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
1215
  for poly in final_polygons_inch:
1216
  # Skip the boundary polygon
1217
  if boundary_polygon is not None and poly == boundary_polygon:
@@ -1219,7 +1027,7 @@ def predict(
1219
  draw_single_polygon(poly, output_img, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
1220
  new_outlines,preview_scale, off_y, padding_px, PH= draw_and_pad(final_polygons_inch, scaling_factor,boundary_polygon)
1221
 
1222
- #draw_polygons_inch(final_polygons_inch, new_outlines, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
1223
  import math
1224
  if annotation_text.strip():
1225
  # Common variables
@@ -1255,9 +1063,7 @@ def predict(
1255
  y1 = raw_y - off_y + padding_px
1256
  text_y_px = int(round(PH - 1 - y1))
1257
  text_y_px_adjusted = text_y_px - baseline
1258
- # bottom_margin_px = int(0.25 / scaling_factor)
1259
- # font_scale,_ = optimal_font_dims(new_outlines)
1260
- #text_y_outlines = int(canvas_height - (text_y_in + (0.75) / scaling_factor))
1261
 
1262
  # First outline, then inner text
1263
  cv2.putText(new_outlines, text, (text_x, text_y_px_adjusted), font, font_scale, (0, 0, 255), thickness+2, cv2.LINE_AA)
 
708
  pts_px = np.array(pts_px, dtype=np.int32)
709
  cv2.polylines(image_rgb, [pts_px], isClosed=True, color=color, thickness=thickness, lineType=cv2.LINE_AA)
710
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
711
  import numpy as np
712
  import cv2
713
  from shapely.geometry import Polygon
 
1019
  # ---------------------
1020
  # 9) For the preview images, draw the polygons and place text similarly
1021
  # ---------------------
1022
+
1023
  for poly in final_polygons_inch:
1024
  # Skip the boundary polygon
1025
  if boundary_polygon is not None and poly == boundary_polygon:
 
1027
  draw_single_polygon(poly, output_img, scaling_factor, processed_size[0], color=(0, 0, 255), thickness=2)
1028
  new_outlines,preview_scale, off_y, padding_px, PH= draw_and_pad(final_polygons_inch, scaling_factor,boundary_polygon)
1029
 
1030
+
1031
  import math
1032
  if annotation_text.strip():
1033
  # Common variables
 
1063
  y1 = raw_y - off_y + padding_px
1064
  text_y_px = int(round(PH - 1 - y1))
1065
  text_y_px_adjusted = text_y_px - baseline
1066
+
 
 
1067
 
1068
  # First outline, then inner text
1069
  cv2.putText(new_outlines, text, (text_x, text_y_px_adjusted), font, font_scale, (0, 0, 255), thickness+2, cv2.LINE_AA)