Skip to content

Instantly share code, notes, and snippets.

@geoffreygarrett
Last active February 5, 2022 15:24
Show Gist options
  • Save geoffreygarrett/5d88230aa221810bb3da25558137d6e2 to your computer and use it in GitHub Desktop.
Save geoffreygarrett/5d88230aa221810bb3da25558137d6e2 to your computer and use it in GitHub Desktop.
Adding predictions to a `fiftyone` dataset.
# Add predictions to samples
with fo.ProgressBar() as pb:
for sample in pb(dataset.view()):
# Load image
image = cv2.imread(sample.filepath)
h, w, c = image.shape
# Perform inference
results = model_best(image, size=3050, augment=False)
preds = results.pandas().xyxy[0]
boxes = preds[['xmin','ymin','xmax','ymax']].values
scores = preds.confidence.values
labels = len(boxes) * ["cots"]
# Convert detections to FiftyOne format
detections = []
for label, score, box in zip(labels, scores, boxes):
# Convert to [top-left-x, top-left-y, width, height]
# in relative coordinates in [0, 1] x [0, 1]
# box = [box[0], box[1], box[0]+box[2], box[1]+box[3]]
x1, y1, x2, y2 = box
rel_box = [x1 / w, y1 / h, (x2 - x1) / w, (y2 - y1) / h]
detections.append(fo.Detection(
label=label,
bounding_box=rel_box,
confidence=score
))
# Save predictions to dataset
sample["predictions"]= fo.Detections(detections=detections)
sample.save()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment