HASS-Deepstack-object icon indicating copy to clipboard operation
HASS-Deepstack-object copied to clipboard

Create class for ROI

Open robmarkcole opened this issue 4 years ago • 2 comments

The ROI should be abstracted using a class. The class will implement a point_in_roi method that accepts a single x,y location and returns a boolean. We can keep a list of ROI on the image processing entity, then just iterate over them to filter each object. Pillow implements various shapes so stick to these:

  • rectangle - two xy points, defining the top left and bottom right of the box
  • polygon - will be a sequence of 4 xy points, to be listed clockwise from the top left point. Note Determining if a point is inside a polygon requires implementing https://en.wikipedia.org/wiki/Point_in_polygon#Ray_casting_algorithm

Since we are always defining points as xy pairs, perhaps change the config to be a list of points like:

points:
  - x: 0
    y: 10
  - x: 20
    y: 30

The rectangle and circle are then just defined by two points, and a polygon by an arbitrary number of points.

Alternatively could adopt the approach of frigate, where a ROI (frigate calls them masks) is defined as a list of xy points, e.g.

- rectangle, 0,10,10,30
- poly, 0,10,10,30,40,50,50,60

This results in much more succinct config. A nice compromise might be to combine approaches, so we have config like:

roi:
  - name: drive
     shape: rectangle
     points: 0,10,10,30

This allows validation of the number of points.

Note that shapely includes the required methods to implement point_in_roi but shapely itself has many C dependencies and we dont want to make this a new HA dependency

robmarkcole avatar Jan 10 '21 08:01 robmarkcole

This is only worth doing if we are going to add the polygon support, and since that is going to be quite complex to configure (probably requiring a dedicated tool to assist) need to think carefully about this

robmarkcole avatar Jan 24 '21 05:01 robmarkcole

Use shapely: https://shapely.readthedocs.io/en/latest/manual.html#geometric-objects

robmarkcole avatar Feb 08 '21 05:02 robmarkcole