Skip to content
This repository has been archived by the owner on Dec 1, 2021. It is now read-only.

remove unnecessary variable assignments #1070

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
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
23 changes: 11 additions & 12 deletions output_template/python/lmnet/utils/demo.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,19 +49,18 @@ def add_fps(orig, fps):
return orig


def check_range(upper, lower, checked_val):
if upper < checked_val:
checked_val = upper
elif lower > checked_val:
checked_val = lower
return checked_val
def clamp(lower, upper, target):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if target < lower:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about

tmp = max(lower, target)
tmp = min(max, target)
return tmp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the advantage of this?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

less lines and little bit readable.
Umm, but this is my personal opinion.
Not necessary change.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about return min(upper, max(lower, target)) ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tfujiwar How do you think about this?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about return min(upper, max(lower, target))

I think that's the best way.
In general, reducing conditional branches helps to keep code simpler.

return lower
if upper < target:
return upper
return target


def add_rectangle(classes, orig, preds, pred_shape):
orig_h, orig_w = orig.shape[:2]
locs = [pred[:, 0:4] for pred in preds]
labels_n = np.array([pred[:, 4] for pred in preds]).astype(np.int) # TODO magic-number
labels_n = labels_n.flatten()
labels_n = np.array([pred[:, 4] for pred in preds]).astype(np.int).flatten() # TODO magic-number

labels = [classes[i_label] for i_label in labels_n]
scores = preds[0][:, 5]
Expand All @@ -73,10 +72,10 @@ def add_rectangle(classes, orig, preds, pred_shape):
for idx, loc in enumerate(locs):
t, b, le, r = ltwh_to__tblr(loc)

le = check_range(orig_w, 0, le)
r = check_range(orig_w, 0, r)
t = check_range(orig_h, 0, t)
b = check_range(orig_h, 0, b)
le = clamp(0, orig_w, le)
r = clamp(0, orig_w, r)
t = clamp(0, orig_h, t)
b = clamp(0, orig_h, b)

color_r = COLORS[labels_n[idx] % len(COLORS)]
thick = 2
Expand Down