OCR API#

What discover_redaction_zones() and scan_pixel_content() build on. Everything on this page is documented but internal (see API stability), except DiscoveryResult.filter(), to_zones() and to_dataframe(), which are frozen at 1.0.

Zone discovery#

session.discover_redaction_zones() returns a DiscoveryResult, which holds one DiscoveryCandidate per text region read at or above min_confidence.

DiscoveryResult(candidates: List[DiscoveryCandidate], n_sources: int)#

Holds the raw results of a discovery scan and provides methods to filter and group them into actionable redaction zones.

filter(predicate: float | Callable = 0.0) → DiscoveryResult#

Returns a new result with filtered candidates.

Parameters:

predicate – Either a float, the minimum confidence kept (default 0.0, which keeps every candidate), or a callable that takes a DiscoveryCandidate and returns True to keep it.

Returns:

The kept candidates, with the same source count.

Return type:

DiscoveryResult

to_zones(pad_x: int = 20, pad_y: int = 10, min_occurrence: float = 0.1) → List[Dict[str, Any]]#

Groups candidates into suggested redaction zones.

Parameters:
  • pad_x – Horizontal padding for merging (higher values merge words on same line).

  • pad_y – Vertical padding.

  • min_occurrence – Fraction of valid sources (0.0-1.0) required to suggest a zone.

Returns:

One dict per zone: zone as [y1, y2, x1, x2], type (LIKELY_NAME, PROPER_NOUN or TEXT), occurrence, mean confidence, and up to three examples. Zones 5 pixels or less wide or tall are dropped.

Return type:

List[Dict[str, Any]]

to_dataframe()#

Returns a pandas DataFrame of the candidates, one row per candidate.

Returns:

One column per DiscoveryCandidate field.

Return type:

pandas.DataFrame

Raises:

ImportError – If pandas is not installed.

DiscoveryCandidate(text: str, confidence: float, box: List[int], source_index: int, classification: str)#

A single text region detected during discovery.

Attributes:

Name

Type

Description

text

str

The text OCR read.

confidence

float

OCR confidence, 0 to 100.

box

List[int]

[x, y, w, h] in pixels. A redaction zone is spelled differently, [y1, y2, x1, x2]; to_zones() converts.

source_index

int

Which sampled instance it was read from, counting only the instances that could be read.

classification

str

TEXT, NAME_PATTERN, PROPER_NOUN or PROPER_NOUN_CANDIDATE.

ZoneDiscoverer#

Utilities for analyzing text regions and classifying them.

static group_boxes(boxes: List[List[int]], padding: int = 0, pad_x: int = None, pad_y: int = None) → List[List[int]]#

Groups boxes into overlapping clusters.

Parameters:
  • boxes – Boxes as [x, y, w, h].

  • padding – Padding on both axes, used where pad_x or pad_y is not given.

  • pad_x – Horizontal padding when testing overlap.

  • pad_y – Vertical padding when testing overlap.

Returns:

The indexes into boxes of each cluster.

Return type:

List[List[int]]

Verification#

RedactionVerifier(rules: List[Dict[str, Any]] = None)#

Verifies pixel redaction strategies by comparing OCR results against configured redaction zones.

get_matching_rule(equipment: Any) → Dict[str, Any]#

Finds the first redaction rule that applies to this equipment.

A rule applies in these cases, as redact() reads it (services.rule_covers, #1108):

  • Its serial_number is the equipment’s Device Serial Number.

  • Its serial_number is "", and the equipment has no Device Serial Number. No equipment at all is such a case too.

  • Its serial_number is "*".

  • Its match: names the equipment’s manufacturer, its model, or both.

The first applying rule in rule order wins, so a "*" rule listed before an exact rule is returned. Until #808, only an exact serial matched. The scan itself reads the zones of every applying rule, not only the zones of this rule.

Parameters:

equipment (Equipment) – The instance’s equipment, or None.

Returns:

The rule dictionary, or None when no rule covers it.

Return type:

Dict[str, Any]

is_covered(text_box: Tuple[int, int, int, int], zone_box: Tuple[int, int, int, int], threshold=0.5) → bool#

Checks whether the zone_box covers enough of the text_box.

Parameters:
  • text_box – OCR box space (x, y, w, h).

  • zone_box – zone space (y1, y2, x1, x2), as redaction_zones entries are stored and as redaction applies them.

  • threshold (float) – Fraction of text area that must be covered (0.0 - 1.0).

Returns:

True if covered.

Return type:

bool

verify_instance(instance: Instance, equipment: Any = None) → List[PhiFinding]#

Runs OCR on the instance and classifies each text region.

  • If the best zone covers 80% or more of the text, the region is safe and is ignored.

  • If the best zone covers more than 0% but less than 80%, the region is reported as a PARTIAL_LEAK.

  • If no zone covers the text (0%), the region is reported as a NEW_LEAK.

Text of two characters or fewer is skipped as noise.

Parameters:
  • instance (Instance) – The instance to read.

  • equipment (Equipment, optional) – Selects the redaction rules that apply (services.rules_matching). With None, only a "" or a "*" rule applies. With no applying rule, every region is a NEW_LEAK.

Returns:

One finding per leaked region. The list is also [] when OCR is unavailable, and then [] does not mean that nothing leaks. Session.scan_pixel_content() checks for OCR first and refuses instead.

Return type:

List[PhiFinding]

Automation#

ConfigAutomator#

Reads OCR findings and suggests changes to the redaction configuration.

static suggest_config_updates(report: PhiReport) → List[Dict[str, Any]]#

Generates a list of suggested configuration changes.

A NEW_LEAK finding suggests its text box as a new zone on the machine’s exact rule. When only a "*" rule covers the machine (rule_serial is "*"), the finding suggests a new exact rule for the machine’s serial instead (ADD_RULE). It never suggests a zone on "*", which every machine reads.

A PARTIAL_LEAK finding suggests growing its best-matching zone to cover the text, on the rule that holds that zone. When that rule is "*", the finding suggests a new exact rule that carries the grown zone instead (ADD_RULE), and "*" is never widened.

The verifier decides which rule gets a NEW_LEAK’s zone, narrowest first (owner ruling on #1108): exact serial, then match:, then "", then "*". A machine with no serial that only "*" covers gets an ADD_RULE for serial_number: "". The finding names a match: rule by rule_match, and its suggestions carry match with serial None. A finding with neither rule_serial nor rule_match in its metadata gets no suggestion.

Zones are in config space, (y1, y2, x1, x2), the order that every consumer of redaction_zones reads. OCR boxes arrive as (x, y, w, h), and this method converts them.

Parameters:

report (PhiReport) – Findings from a pixel scan. Each finding carries leak_type, text_box, best_zone and rule_serial in its metadata, and machine_serial, manufacturer and model_name for an ADD_RULE.

Returns:

One dict per suggestion, with serial, action and reason. An ADD_ZONE suggestion carries zone. An EXPAND_ZONE suggestion carries original_zone and new_zone. Each of these zones is [y1, y2, x1, x2]. An ADD_RULE suggestion carries zone, manufacturer and model_name, and its serial is the machine’s.

Return type:

List[Dict[str, Any]]

Pixel analysis#

OCR over pixel data: find burned-in text and where it sits.

TextRegion(text: str, box: Tuple[int, int, int, int], confidence: float, frame_index: int = 0)#

Represents a region of text detected within an image or frame.

Attributes:

Name

Type

Description

text

str

The detected text string.

box

Tuple[int, int, int, int]

The bounding box of the text region (x, y, w, h).

confidence

float

The confidence score of the detection (0-100).

frame_index

int

The index of the frame where the text was detected (default 0).

analyze_pixels(instance: Instance) → List[TextRegion]#

Analyzes the pixel data of a DICOM Instance for burned-in text.

A load or OCR failure is logged at ERROR and what was read is returned. A frame this call loaded is released before it returns, with unload_pixel_data(); one that was resident before the call is left as it was. A caller who wants the frame afterwards calls instance.get_pixel_data().

Parameters:

instance (Instance) – The instance to read.

Returns:

Raw findings, not filtered against any zone. Also [] when OCR is unavailable or nothing could be read, so [] is not “no text”; the Session methods check availability first and report what this only logs.

Return type:

List[TextRegion]

detect_text_regions(pixel_data: ndarray, frame_idx: int = 0) → List[TextRegion]#

Runs OCR on the provided pixel data and returns text regions with bounding boxes.

Parameters:
  • pixel_data (np.ndarray) – The image data (should be 2D).

  • frame_idx (int) – The frame index associated with this data.

Returns:

Detected text regions. Also [] when OCR is unavailable, and [] when OCR raised (logged at ERROR), so [] here does not mean “no text”. Session.scan_pixel_content() and discover_redaction_zones() check availability first and refuse instead, and report the failures this function only logs.

Return type:

List[TextRegion]