Create class for ROI
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
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
Use shapely: https://shapely.readthedocs.io/en/latest/manual.html#geometric-objects