Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Create Annotator() class #4591

Merged
merged 29 commits into from
Aug 29, 2021
Merged
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
8dbcbf4
Add Annotator() class
glenn-jocher Aug 28, 2021
b4e490c
Download Arial
glenn-jocher Aug 28, 2021
4d0ec24
2x for loop
glenn-jocher Aug 28, 2021
90996f1
Cleanup
glenn-jocher Aug 28, 2021
27aa8a0
tuple 2 list
glenn-jocher Aug 28, 2021
8f3fdc0
max_size=1920
glenn-jocher Aug 28, 2021
7643b7d
bold logging results to
glenn-jocher Aug 28, 2021
56e003b
tolist()
glenn-jocher Aug 28, 2021
4ac39b2
im = annotator.im
glenn-jocher Aug 28, 2021
797fe29
PIL save in detect.py
glenn-jocher Aug 28, 2021
aa9ebb5
Smart asarray in detect.py
glenn-jocher Aug 29, 2021
a82d3f5
revert to cv2.imwrite
glenn-jocher Aug 29, 2021
578a4e3
Cleanup
glenn-jocher Aug 29, 2021
e861a55
Return result asarray
glenn-jocher Aug 29, 2021
f4a8d86
Add `Profile()` profiler
glenn-jocher Aug 29, 2021
782c1a6
CamelCase Timeout
glenn-jocher Aug 29, 2021
a1a5256
Merge add/profile
glenn-jocher Aug 29, 2021
a8eb4fc
Resize after mosaic
glenn-jocher Aug 29, 2021
d457867
pillow>=8.0.0
glenn-jocher Aug 29, 2021
7fb8541
Merge master
glenn-jocher Aug 29, 2021
b676ec6
daemon imwrite
glenn-jocher Aug 29, 2021
994d892
Add cv2 support
glenn-jocher Aug 29, 2021
75dc88f
Remove plot_wh_methods and plot_one_box
glenn-jocher Aug 29, 2021
b9733cb
pil=False for hubconf.py annotations
glenn-jocher Aug 29, 2021
42ff73c
im.shape bug fix
glenn-jocher Aug 29, 2021
502088d
colorstr common.py
glenn-jocher Aug 29, 2021
753f2b1
join daemons
glenn-jocher Aug 29, 2021
34c973e
Update t.daemon
glenn-jocher Aug 29, 2021
f847e15
Removed daemon saving
glenn-jocher Aug 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
2x for loop
  • Loading branch information
glenn-jocher committed Aug 28, 2021
commit 4d0ec24764295b3bfe399987438195d9b028557a
60 changes: 28 additions & 32 deletions utils/plots.py
Original file line number Diff line number Diff line change
Expand Up @@ -160,58 +160,54 @@ def output_to_target(output):

def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max_size=640, max_subplots=16):
# Plot image grid with labels

if isinstance(images, torch.Tensor):
images = images.cpu().float().numpy()
if isinstance(targets, torch.Tensor):
targets = targets.cpu().numpy()

# un-normalise
if np.max(images[0]) <= 1:
images *= 255

images *= 255.0 if np.max(images[0]) <= 1 else 1.0 # de-normalise (optional)
bs, _, h, w = images.shape # batch size, _, height, width
bs = min(bs, max_subplots) # limit plot images
ns = np.ceil(bs ** 0.5) # number of subplots (square)

# Check if we should resize
scale_factor = max_size / max(h, w)
if scale_factor < 1:
h = math.ceil(scale_factor * h)
w = math.ceil(scale_factor * w)
# Check if resize
scale = max_size / max(h, w)
if scale < 1:
h = math.ceil(scale * h)
w = math.ceil(scale * w)

fs = int(h * ns * 0.02) # font size
mosaic = np.full((int(ns * h), int(ns * w), 3), 255, dtype=np.uint8) # init
for i, img in enumerate(images):
for i, im in enumerate(images):
if i == max_subplots: # if last batch has fewer images than we expect
break
x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin
im = im.transpose(1, 2, 0)
if scale < 1:
im = cv2.resize(im, (w, h))
mosaic[y:y + h, x:x + w, :] = im

block_x = int(w * (i // ns))
block_y = int(h * (i % ns))

img = img.transpose(1, 2, 0)
if scale_factor < 1:
img = cv2.resize(img, (w, h))
annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs)
for i in range(i + 1):
x, y = int(w * (i // ns)), int(h * (i % ns)) # block origin

mosaic[block_y:block_y + h, block_x:block_x + w, :] = img
annotator = Annotator(mosaic, line_width=round(fs / 10), font_size=fs)
if len(targets) > 0:
image_targets = targets[targets[:, 0] == i]
boxes = xywh2xyxy(image_targets[:, 2:6]).T
classes = image_targets[:, 1].astype('int')
labels = image_targets.shape[1] == 6 # labels if no conf column
conf = None if labels else image_targets[:, 6] # check for confidence presence (label vs pred)
ti = targets[targets[:, 0] == i] # image targets
boxes = xywh2xyxy(ti[:, 2:6]).T
classes = ti[:, 1].astype('int')
labels = ti.shape[1] == 6 # labels if no conf column
conf = None if labels else ti[:, 6] # check for confidence presence (label vs pred)

if boxes.shape[1]:
if boxes.max() <= 1.01: # if normalized with tolerance 0.01
boxes[[0, 2]] *= w # scale to pixels
boxes[[1, 3]] *= h
elif scale_factor < 1: # absolute coords need scale if image scales
boxes *= scale_factor
boxes[[0, 2]] += block_x
boxes[[1, 3]] += block_y
elif scale < 1: # absolute coords need scale if image scales
boxes *= scale
boxes[[0, 2]] += x
boxes[[1, 3]] += y
for j, box in enumerate(boxes.T):
cls = int(classes[j])
cls = classes[j]
color = colors(cls)
cls = names[cls] if names else cls
if labels or conf[j] > 0.25: # 0.25 conf thresh
Expand All @@ -221,12 +217,12 @@ def plot_images(images, targets, paths=None, fname='images.jpg', names=None, max
# Draw image filename labels
if paths:
label = Path(paths[i]).name[:40] # trim to 40 char
annotator.text((block_x + 5, block_y + 5 + h), label, (220, 220, 220))
annotator.text((x + 5, y + 5 + h), label, (220, 220, 220))

# Image border
annotator.rectangle((block_x, block_y, block_x + w, block_y + h), None, (255, 255, 255), width=2)
mosaic = annotator.result()
annotator.rectangle((x, y, x + w, y + h), None, (255, 255, 255), width=2)

mosaic = annotator.result()
if fname:
r = min(1280. / max(h, w) / ns, 1.0) # ratio to limit image size
mosaic = cv2.resize(mosaic, (int(ns * w * r), int(ns * h * r)), interpolation=cv2.INTER_AREA)
Expand Down