Skip to main content

DetectionEngine.crops

DetectionEngine.crops(
    model_name=None,
    split='test',
    *,
    score_threshold=None,
    context=0.0,
    size=None,
    fit='letterbox',
    fill=None,
)

Every detection cut out of the image it was found in.

The pipeline this is for is a detector followed by a classifier: find the objects with three classes, crop them, hand the crops to something that knows eighty. It is the second thing people ask for after boxes, and doing it by hand means re-reading every image and getting the rounding right.

Since 0.8.0a49 the second half is in this package too - task: classification - and the join is still yours: this returns arrays, and a classification run reads a directory or a CSV, so the crops have to be written out in between. That is left undone on purpose, because which crops are worth keeping is a decision about the data rather than about either model.

Filtered at operating_point, not at score_threshold. The specification’s score_threshold is deliberately near zero so that average precision integrates the whole ranking - on BCCD that is hundreds of boxes an image, almost all of them the tail that AP exists to measure over. Cropping them would hand a classifier mostly noise. operating_point is where someone stands when they act on a prediction, which is what cropping is.

One record per image rather than one flat list, because a crop without its source is not traceable: key says which image, and boxes are the coordinates it came from, in that image’s own pixels.

Parameters

Name Type Description Default
model_name str | None Which trained detector. Defaults to the first. None
split str Which split’s images to read and cut from. 'test'
score_threshold float | None Crop at this confidence instead of the model’s operating_point. None
context float Extra margin per side, as a fraction of the box rather than in pixels, so one value suits a platelet and a white cell. 0 by default. 0.0
size tuple[int, int] | None Bring every crop to (height, width), or None to keep each as it was cut. None
fit str "letterbox" to preserve the aspect, "stretch" to resize both axes - a stretch changes the aspect of every non-square object and so has to be asked for. 'letterbox'
fill float | None Padding value for "letterbox", in the image’s own units. Left out it is grey - which is 0.5 here, because source_image reads through to_float and so hands this a 0-1 picture. Threaded as None rather than defaulting to 0.5 so that one meaning of not given reaches crop_boxes, whose refusal would then cover this route as well if that ever stopped being true. None

Returns

Name Type Description
list[dict[str, Any]] One record per image, each a dict of seven keys. key says which image;
list[dict[str, Any]] crops are the cut-out arrays and boxes the coordinates they came from, in
list[dict[str, Any]] that image’s own pixels; scores, labels and names are the detections that
list[dict[str, Any]] cleared the threshold. dropped is how many boxes the inverse letterbox mapped
list[dict[str, Any]] to no extent at all - which a model may legitimately produce by saying an
list[dict[str, Any]] object is off the frame, and which is reported rather than left as a count that
list[dict[str, Any]] quietly does not match the boxes.