Skip to content

Feat/load numpy coordinates#27

Open
srikeerthis wants to merge 2 commits intoroboflow:mainfrom
srikeerthis:feat/load-numpy-coordinates
Open

Feat/load numpy coordinates#27
srikeerthis wants to merge 2 commits intoroboflow:mainfrom
srikeerthis:feat/load-numpy-coordinates

Conversation

@srikeerthis
Copy link
Copy Markdown

What does this PR do?

This PR introduces the ability to load existing polygon coordinates back into the PolygonZone canvas. Previously, users could only export coordinates. With this addition, users can paste their previously generated Python/NumPy arrays or JSON objects back into the tool to view or edit them on a new image.

Specifically, this adds:

  • A text area and load button to the control panel (index.html).
  • Parsing logic to handle native Python output (np.array([[...]])) and custom JSON outputs (script.js).
  • Auto-detection for normalized coordinates (values <= 1.0), automatically scaling them back to absolute pixel values using the loaded image's dimensions.
  • Seamless integration with the existing masterPoints state so loaded polygons can be immediately edited.

Related Issue(s): #24

Type of Change

  • New feature (non-breaking change that adds functionality)

Testing

I have manually tested the following scenarios to ensure robustness:

  • Format Parsing: Successfully loads native np.array(...) strings copy-pasted directly from the Python output.
  • Format Parsing: Successfully loads the custom JSON output ({"x": ..., "y": ...}).
  • Normalization: Auto-detects normalized points (0.0 - 1.0) and accurately scales them to the loaded image's natural dimensions.
  • Data Wrapping: Accepts both 3D arrays (multiple polygons) and raw 2D arrays (single polygons), normalizing them internally.
  • Error Handling (No Image): Alerts the user and gracefully aborts if no image is currently loaded.
  • Error Handling (Garbage Data): Alerts the user and gracefully aborts if malformed text or invalid array structures are pasted, preventing crashes.
  • State Integration: Polygons loaded via input can immediately be modified using the existing "Edit Mode" (E).

Tests:

  • Image: Image
  • Standard
    • NumPy Format
      • Single Polygon:
        [[91, 99], [343, 99], [349, 209], [99, 212]]
      
      • Multiple Polygon:
      [[[783, 158], [797, 233], [689, 241], [687, 174]],[[429, 1454], [555, 1397], [797, 1384], [898, 1496], [829, 1620], [732, 1623], [679, 1687], [579, 1671], [416, 1542]]]
      
  • Normalized:
    • NumPy Format
      • Single Polygon:
       [[0.23, 0.53], [0.36, 0.48], [0.43, 0.49], [0.5, 0.54], [0.42, 0.62], [0.33, 0.65], [0.21, 0.55]] 
      
    • JSON Format
      • Single Poylgon:
        { 
         [{"x": 0.23, "y": 0.53}, {"x": 0.36, "y": 0.48}, {"x": 0.43, "y": 0.49}, {"x": 0.5, "y": 0.54}, {"x": 0.42, "y": 0.62}, {"x": 0.33, "y": 0.65}, {"x": 0.21, "y": 0.55}]
        }
      
      • Multiple Polygon:
      {
          [{"x": 0.07, "y": 0.06}, {"x": 0.27, "y": 0.06}, {"x": 0.27, "y": 0.12}, {"x": 0.08, "y": 0.12}],
          [{"x": 0.61, "y": 0.09}, {"x": 0.62, "y": 0.13}, {"x": 0.53, "y": 0.14}, {"x": 0.53, "y": 0.1}],
          [{"x": 0.33, "y": 0.83}, {"x": 0.43, "y": 0.8}, {"x": 0.62, "y": 0.79}, {"x": 0.7, "y": 0.85}, {"x": 0.64, "y": 0.93}, {"x": 0.57, "y": 0.93}, {"x": 0.53, "y": 0.96}, {"x": 0.45, "y": 0.95}, {"x": 0.32, "y": 0.88}]
      }
      
  • I have tested this change locally
  • I have added/updated tests for this change (Note: Relies on manual QA as there is no automated test suite)

Checklist

  • My code follows the style guidelines of this project
  • I have performed a self-review of my own code
  • I have commented my code where necessary, particularly in hard-to-understand areas
  • My changes generate no new warnings or errors
  • I have updated the documentation accordingly (if applicable)

Additional Context

Visual Walkthrough:
Test Performed

@srikeerthis srikeerthis mentioned this pull request Apr 4, 2026
2 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant