36 lines
930 B
Python
36 lines
930 B
Python
import json
|
|
from typing import Sequence
|
|
|
|
import cv2
|
|
|
|
from cv_analysis.utils.rectangle import Rectangle
|
|
|
|
|
|
def contour_to_rectangle(contour):
|
|
return box_to_rectangle(cv2.boundingRect(contour))
|
|
|
|
|
|
def box_to_rectangle(box: Sequence[int]) -> Rectangle:
|
|
x, y, w, h = box
|
|
return Rectangle(x, y, x + w, y + h)
|
|
|
|
|
|
def rectangle_to_box(rectangle: Rectangle) -> Sequence[int]:
|
|
return [rectangle.x1, rectangle.y1, rectangle.width, rectangle.height]
|
|
|
|
|
|
class RectangleJSONEncoder(json.JSONEncoder):
|
|
def __init__(self, *args, **kwargs):
|
|
json.JSONEncoder.__init__(self, *args, **kwargs)
|
|
self._replacement_map = {}
|
|
|
|
def default(self, o):
|
|
if isinstance(o, Rectangle):
|
|
return {"x1": o.x1, "x2": o.x2, "y1": o.y1, "y2": o.y2}
|
|
else:
|
|
return json.JSONEncoder.default(self, o)
|
|
|
|
def encode(self, o):
|
|
result = json.JSONEncoder.encode(self, o)
|
|
return result
|