Skip to content

Commit

Permalink
Add crop_box to inference ops
Browse files Browse the repository at this point in the history
  • Loading branch information
jrrodri committed Dec 25, 2024
1 parent 22f49fc commit 208815c
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 52 deletions.
40 changes: 1 addition & 39 deletions abraia/inference/detect.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import numpy as np
import onnxruntime as ort

from .ops import py_cpu_nms, normalize, mask_to_polygon, softmax, count_objects
from .ops import non_maximum_suppression, normalize, mask_to_polygon, softmax, count_objects
from ..utils import download_file, load_json, get_color, get_providers
from ..utils import load_image, show_image, save_image, Video, render_results

Expand All @@ -15,14 +15,6 @@ def resize(img, size):
return cv2.resize(img, (width, height), interpolation=cv2.INTER_LINEAR)


# TODO: Refactor to convert to get detection
def crop(img, size):
height, width = img.shape[:2]
left, top = (width - size) // 2, (height - size) // 2
right, bottom = left + size, top + size
return img[top:bottom, left:right]


def preprocess(img, size = 224):
img = resize(img, size)
img = normalize(img, [0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
Expand Down Expand Up @@ -52,36 +44,6 @@ def prepare_input(img, shape):
return img / 255


def iou(box1, box2):
"""Calculates the intersection-over-union of two boxes."""
tl1, wh1, br1 = [box1[0], box1[1]], [box1[2], box1[3]], [box1[0] + box1[2], box1[1] + box1[3]]
tl2, wh2, br2 = [box2[0], box2[1]], [box2[2], box2[3]], [box2[0] + box2[2], box2[1] + box2[3]]
intersection_area = np.prod(np.maximum(np.minimum(br1, br2) - np.maximum(tl1, tl2), 0))
union_area = np.prod(wh1) + np.prod(wh2) - intersection_area;
return intersection_area / union_area


# def non_maximum_suppression(objects, iou_threshold):
# results = []
# objects.sort(key=lambda obj: obj['confidence'], reverse=True)
# while len(objects) > 0:
# results.append(objects[0])
# objects = [obj for obj in objects if iou(obj['box'], objects[0]['box']) < iou_threshold]
# return results


def non_maximum_suppression(objects, iou_threshold):
dets = []
for obj in objects:
s = obj['confidence']
x, y, w, h = obj['box']
dets.append([x, y, x + w, y + h, s])
if dets:
idxs = py_cpu_nms(np.array(dets), iou_threshold)
return [objects[idx] for idx in idxs]
return []


def sigmoid_mask(z):
mask = 1 / (1 + np.exp(-z))
return (255 * mask).astype('uint8')
Expand Down
23 changes: 23 additions & 0 deletions abraia/inference/ops.py
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,24 @@ def py_cpu_nms(dets, thresh):
return keep


# def iou(box1, box2):
# """Calculates the intersection-over-union of two boxes."""
# tl1, wh1, br1 = [box1[0], box1[1]], [box1[2], box1[3]], [box1[0] + box1[2], box1[1] + box1[3]]
# tl2, wh2, br2 = [box2[0], box2[1]], [box2[2], box2[3]], [box2[0] + box2[2], box2[1] + box2[3]]
# intersection_area = np.prod(np.maximum(np.minimum(br1, br2) - np.maximum(tl1, tl2), 0))
# union_area = np.prod(wh1) + np.prod(wh2) - intersection_area;
# return intersection_area / union_area


# def non_maximum_suppression(objects, iou_threshold):
# results = []
# objects.sort(key=lambda obj: obj['confidence'], reverse=True)
# while len(objects) > 0:
# results.append(objects[0])
# objects = [obj for obj in objects if iou(obj['box'], objects[0]['box']) < iou_threshold]
# return results


def non_maximum_suppression(objects, iou_threshold):
dets = []
for obj in objects:
Expand All @@ -152,3 +170,8 @@ def count_objects(results):
colors[label] = color
objects = [{'label': label, 'count': counts[label], 'color': colors[label]} for label in counts.keys()]
return objects


def crop_box(img, box):
x, y, w, h = box
return img[y:y+w, x:x+w]
1 change: 1 addition & 0 deletions abraia/inference/plates.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ def iou(box1, box2):
return intersection_area / union_area


# TODO: Replace with non_maximum_suppression from ops
def nms(objects, iou_threshold = 0.5):
results = []
objects.sort(key=lambda obj: obj['confidence'], reverse=True)
Expand Down
13 changes: 0 additions & 13 deletions abraia/tracker/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,19 +110,6 @@ def embedding_distance(tracks, detections, metric='cosine'):
return cost_matrix


# def gate_cost_matrix(kf, cost_matrix, tracks, detections, only_position=False):
# if cost_matrix.size == 0:
# return cost_matrix
# gating_dim = 2 if only_position else 4
# gating_threshold = kalman_filter.chi2inv95[gating_dim]
# measurements = np.asarray([det.to_xyah() for det in detections])
# for row, track in enumerate(tracks):
# gating_distance = kf.gating_distance(
# track.mean, track.covariance, measurements, only_position)
# cost_matrix[row, gating_distance > gating_threshold] = np.inf
# return cost_matrix


def fuse_motion(kf, cost_matrix, tracks, detections, only_position=False, lambda_=0.98):
if cost_matrix.size == 0:
return cost_matrix
Expand Down
Binary file added images/birds_375x375.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/birds_o.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/lion_o.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified images/screenshot.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added images/shoe.jpeg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit 208815c

Please sign in to comment.