Skip to content

Commit

Permalink
Debugging over the wire step
Browse files Browse the repository at this point in the history
  • Loading branch information
brandon-groundlight committed Jul 31, 2024
1 parent be8db8a commit 7c2b2da
Show file tree
Hide file tree
Showing 5 changed files with 76 additions and 39 deletions.
7 changes: 4 additions & 3 deletions src/groundlight/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
ImageQuery,
PaginatedDetectorList,
PaginatedImageQueryList,
ROI,
)
from urllib3.exceptions import InsecureRequestWarning

Expand Down Expand Up @@ -726,7 +727,7 @@ def _wait_for_result(
image_query = self._fixup_image_query(image_query)
return image_query

def add_label(self, image_query: Union[ImageQuery, str], label: Union[Label, str], rois: Optional[list] = None):
def add_label(self, image_query: Union[ImageQuery, str], label: Union[Label, str], rois: Optional[list[ROI]] = None):
"""
Add a new label to an image query. This answers the detector's question.
Expand All @@ -747,8 +748,8 @@ def add_label(self, image_query: Union[ImageQuery, str], label: Union[Label, str
if not image_query_id.startswith(("chk_", "iq_")):
raise ValueError(f"Invalid image query id {image_query_id}")
api_label = convert_display_label_to_internal(image_query_id, label)
json_rois = [roi.json() for roi in rois] if rois else None
request_params = LabelValueRequest(label=api_label, image_query_id=image_query_id, rois=json_rois)
request_params = LabelValueRequest(label=api_label, image_query_id=image_query_id, rois=rois)
import IPython; IPython.embed()
self.labels_api.create_label(request_params)

def start_inspection(self) -> str:
Expand Down
26 changes: 25 additions & 1 deletion src/groundlight/experimental_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
from groundlight_openapi_client.model.note_request import NoteRequest
from groundlight_openapi_client.model.rule_request import RuleRequest
from groundlight_openapi_client.model.verb_enum import VerbEnum
from model import Detector, DetectorGroup, PaginatedRuleList, Rule, ROI
from model import Detector, DetectorGroup, PaginatedRuleList, Rule, ROI, BBoxGeometry

from .client import Groundlight

Expand Down Expand Up @@ -201,3 +201,27 @@ def list_detector_groups(self) -> List[DetectorGroup]:
:return: a list of all detector groups
"""
return [DetectorGroup(**det.to_dict()) for det in self.detector_group_api.get_detector_groups()]

def create_roi(self, label: str, top_left: Tuple[int, int], bottom_right: Tuple[int, int]) -> ROI:
"""
Adds a region of interest to the given detector
NOTE: This feature is only available to Pro tier and higher
If you would like to learn more, reach out to us at https://groundlight.ai
:param label: the label of the item in the roi
:param top_left: the top left corner of the roi
:param bottom_right: the bottom right corner of the roi
"""

return ROI(
label=label,
score=1.0,
geometry=BBoxGeometry(
left = top_left[0],
top = top_left[1],
right = bottom_right[0],
bottom = bottom_right[1],
x = (top_left[0] + bottom_right[0]) / 2,
y = (top_left[1] + bottom_right[1]) / 2,
)
)
29 changes: 0 additions & 29 deletions test/integration/test_groundlight.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,35 +64,6 @@ def is_valid_display_label(label: str) -> bool:
return label in VALID_DISPLAY_LABELS


@pytest.fixture(name="gl")
def fixture_gl() -> Groundlight:
"""Creates a Groundlight client object for testing."""
_gl = Groundlight()
_gl.DEFAULT_WAIT = 10
return _gl


@pytest.fixture(name="detector")
def fixture_detector(gl: Groundlight) -> Detector:
"""Creates a new Test detector."""
name = f"Test {datetime.utcnow()}" # Need a unique name
query = "Is there a dog?"
pipeline_config = "never-review"
return gl.create_detector(name=name, query=query, pipeline_config=pipeline_config)


@pytest.fixture(name="image_query_yes")
def fixture_image_query_yes(gl: Groundlight, detector: Detector) -> ImageQuery:
iq = gl.submit_image_query(detector=detector.id, image="test/assets/dog.jpeg", human_review="NEVER")
return iq


@pytest.fixture(name="image_query_no")
def fixture_image_query_no(gl: Groundlight, detector: Detector) -> ImageQuery:
iq = gl.submit_image_query(detector=detector.id, image="test/assets/cat.jpeg", human_review="NEVER")
return iq


@pytest.fixture(name="image")
def fixture_image() -> str:
return "test/assets/dog.jpeg"
Expand Down
28 changes: 27 additions & 1 deletion test/unit/conftest.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,33 @@
import pytest
from groundlight import ExperimentalApi
from datetime import datetime

from groundlight import ExperimentalApi, Groundlight, Detector, ImageQuery

@pytest.fixture(name="gl")
def fixture_gl() -> Groundlight:
"""Creates a Groundlight client object for testing."""
_gl = Groundlight()
_gl.DEFAULT_WAIT = 10
return _gl

@pytest.fixture(name="detector")
def fixture_detector(gl: Groundlight) -> Detector:
"""Creates a new Test detector."""
name = f"Test {datetime.utcnow()}" # Need a unique name
query = "Is there a dog?"
pipeline_config = "never-review"
return gl.create_detector(name=name, query=query, pipeline_config=pipeline_config)

@pytest.fixture(name="image_query_yes")
def fixture_image_query_yes(gl: Groundlight, detector: Detector) -> ImageQuery:
iq = gl.submit_image_query(detector=detector.id, image="test/assets/dog.jpeg", human_review="NEVER")
return iq

@pytest.fixture(name="image_query_no")
def fixture_image_query_no(gl: Groundlight, detector: Detector) -> ImageQuery:
iq = gl.submit_image_query(detector=detector.id, image="test/assets/cat.jpeg", human_review="NEVER")
return iq

@pytest.fixture(name="gl_experimental")
def _gl() -> ExperimentalApi:
return ExperimentalApi()
25 changes: 20 additions & 5 deletions test/unit/test_experimental.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,28 @@
from datetime import datetime

from groundlight import ExperimentalApi
from groundlight import ExperimentalApi, Detector, ImageQuery, ROI


def test_detector_groups(gl: ExperimentalApi):
def test_detector_groups(gl_experimental: ExperimentalApi):
"""
verify that we can create a detector group and retrieve it
"""
name = f"Test {datetime.utcnow()}"
created_group = gl.create_detector_group(name)
all_groups = gl.list_detector_groups()
created_group = gl_experimental.create_detector_group(name)
all_groups = gl_experimental.list_detector_groups()
assert created_group in all_groups

def test_submit_roi(gl_experimental: ExperimentalApi, image_query_yes: ImageQuery):
"""
verify that we can submit an ROI
"""
label_name = "dog"
roi = gl_experimental.create_roi(label_name, (0, 0), (0.5, 0.5))
gl_experimental.add_label(image_query_yes.id, "YES", [roi])

def test_submit_rois(gl_experimental: ExperimentalApi, image_query_no: ImageQuery):
"""
verify that we can submit multiple ROIs
"""
label_name = "dog"
roi = gl_experimental.create_roi(label_name, (0, 0), (0.5, 0.5))
gl_experimental.add_label(image_query_no, "YES", [roi]*3)

0 comments on commit 7c2b2da

Please sign in to comment.