aboutsummaryrefslogtreecommitdiff
path: root/src/argaze/RegionOfInterest/ROI2DScene.py
blob: d025cb26eff0a3ab1dcaaccbc30a59de244fc350 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env python

import cv2 as cv
import matplotlib.path as mpath

class ROI2DScene(list):
	"""List of ROI3D dictionary.
	```
	{
		'NAME': str,
		'VERTICES': array of (x, y) tuples,
		'POINTER_INSIDE': bool
	}
	```
	"""

	def __new__(cls):
		return super(ROI2DScene, cls).__new__(cls)

	def __init__(self):
		pass

	def __del__(self):
		pass

	def inside(self, pointer):
		"""Check if a (x, y) pointer is inside ROIs."""

		for roi in self:

			roi['POINTER_INSIDE'] = mpath.Path(roi['VERTICES']).contains_points([pointer])[0]

	def draw(self, frame):
		"""Draw ROI polygons on frame."""

		for roi in self:

			vertices = roi['VERTICES']
			inside = roi['POINTER_INSIDE']

			color = (0, 255, 0) if inside else (0, 0, 255)

			if inside:
				cv.putText(frame, roi['NAME'], (vertices[3][0], vertices[3][1]), cv.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 1, cv.LINE_AA)

			cv.line(frame, vertices[-1], vertices[0], color, 1)
			for A, B in zip(vertices, vertices[1:]):
				cv.line(frame, A, B, color, 1)