Commit 982f42b9 authored by Yuxin Wu's avatar Yuxin Wu

Remove FloatBox and IntBox

parent 95cdb963
......@@ -364,6 +364,7 @@ _DEPRECATED_NAMES = set([
'dump_dataflow_to_lmdb',
'dump_dataflow_to_tfrecord',
'pyplot2img',
'IntBox', 'FloatBox',
# renamed stuff:
'DumpTensor',
......
......@@ -8,7 +8,6 @@ from tabulate import tabulate
import tqdm
from tensorpack.utils import logger
from tensorpack.utils.rect import FloatBox
from tensorpack.utils.timer import timed_operation
from tensorpack.utils.argtools import log_once
......@@ -130,13 +129,15 @@ class COCODetection(object):
x1, y1, w, h = obj['bbox']
# bbox is originally in float
# x1/y1 means upper-left corner and w/h means true w/h. This can be verified by segmentation pixels.
# But we do assume that (0.0, 0.0) is upper-left corner of the first pixel
box = FloatBox(float(x1), float(y1),
float(x1 + w), float(y1 + h))
box.clip_by_shape([height, width])
# But we do make an assumption here that (0.0, 0.0) is upper-left corner of the first pixel
x1 = np.clip(float(x1), 0, width)
y1 = np.clip(float(y1), 0, height)
w = np.clip(float(x1 + w), 0, width) - x1
h = np.clip(float(y1 + h), 0, height) - y1
# Require non-zero seg area and more than 1x1 box size
if obj['area'] > 1 and box.is_box() and box.area() >= 4:
obj['bbox'] = [box.x1, box.y1, box.x2, box.y2]
if obj['area'] > 1 and w > 0 and h > 0 and w * h >= 4:
obj['bbox'] = [x1, y1, x1 + w, y1 + h]
valid_objs.append(obj)
if add_mask:
......
......@@ -23,7 +23,7 @@ which is better than other TensorFlow implementations.
### Usage
1. Download the pre-trained model:
1. Download the pre-trained model (converted from caffe):
```bash
wget http://models.tensorpack.com/OpticalFlow/flownet2.npz
......@@ -36,7 +36,7 @@ wget http://models.tensorpack.com/OpticalFlow/flownet2-c.npz
2. Run inference
```bash
python flownet2.py
python flownet2.py \
--left left.png --right right.png \
--load flownet2.npz --model flownet2
```
......
......@@ -113,20 +113,20 @@ class ImageAugmentor(Augmentor):
floating point images in range [0, 1] or [0, 255].
"""
def augment_coords(self, coords, param):
return self._augment_coords(coords, param)
def _augment_coords(self, coords, param):
"""
Augment the coordinates given the param.
By default, keeps coordinates unchanged.
By default, an augmentor keeps coordinates unchanged.
If a subclass changes coordinates but couldn't implement this method,
it should ``raise NotImplementedError()``.
Args:
coords: Nx2 floating point nparray where each row is (x, y)
coords: Nx2 floating point numpy array where each row is (x, y)
Returns:
new coords
"""
return self._augment_coords(coords, param)
def _augment_coords(self, coords, param):
return coords
......
......@@ -8,7 +8,6 @@ import sys
import io
from .fs import mkdir_p
from .argtools import shape2d
from .rect import BoxBase, IntBox
from .palette import PALETTE_RGB
try:
......@@ -367,8 +366,7 @@ def draw_boxes(im, boxes, labels=None, color=None):
"""
Args:
im (np.ndarray): a BGR image in range [0,255]. It will not be modified.
boxes (np.ndarray or list[BoxBase]): If an ndarray,
must be of shape Nx4 where the second dimension is [x1, y1, x2, y2].
boxes (np.ndarray): a numpy array of shape Nx4 where each row is [x1, y1, x2, y2].
labels: (list[str] or None)
color: a 3-tuple (in range [0, 255]). By default will choose automatically.
......@@ -377,14 +375,7 @@ def draw_boxes(im, boxes, labels=None, color=None):
"""
FONT = cv2.FONT_HERSHEY_SIMPLEX
FONT_SCALE = 0.4
if isinstance(boxes, list):
arr = np.zeros((len(boxes), 4), dtype='int32')
for idx, b in enumerate(boxes):
assert isinstance(b, BoxBase), b
arr[idx, :] = [int(b.x1), int(b.y1), int(b.x2), int(b.y2)]
boxes = arr
else:
boxes = boxes.astype('int32')
boxes = np.asarray(boxes, dtype='int32')
if labels is not None:
assert len(labels) == len(boxes), "{} != {}".format(len(labels), len(boxes))
areas = (boxes[:, 2] - boxes[:, 0] + 1) * (boxes[:, 3] - boxes[:, 1] + 1)
......@@ -415,17 +406,18 @@ def draw_boxes(im, boxes, labels=None, color=None):
if top_left[1] < 0: # out of image
top_left[1] = box[3] - 1.3 * lineh
bottom_left[1] = box[3] - 0.3 * lineh
textbox = IntBox(int(top_left[0]), int(top_left[1]),
int(top_left[0] + linew), int(top_left[1] + lineh))
textbox.clip_by_shape(im.shape[:2])
x1, y1 = int(top_left[0]), int(top_left[1])
x2, y2 = int(top_left[0] + linew), int(top_left[1] + lineh)
x1, x2 = [np.clip(x, 0, img.shape[1] - 1) for x in [x1, x2]]
y1, y2 = [np.clip(y, 0, img.shape[0] - 1) for y in [y1, y2]]
if color is None:
# find the best color
mean_color = textbox.roi(im).mean(axis=(0, 1))
mean_color = im[y1:y2 + 1, x1:x2 + 1].mean(axis=(0, 1))
best_color_ind = (np.square(COLOR_CANDIDATES - mean_color) *
COLOR_DIFF_WEIGHT).sum(axis=1).argmax()
best_color = COLOR_CANDIDATES[best_color_ind].tolist()
cv2.putText(im, label, (textbox.x1, textbox.y2),
cv2.putText(im, label, (x1, y2),
FONT, FONT_SCALE, color=best_color, lineType=cv2.LINE_AA)
cv2.rectangle(im, (box[0], box[1]), (box[2], box[3]),
color=best_color, thickness=1)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment