Skip to content

Commit

Permalink
Update detection result images
Browse files Browse the repository at this point in the history
  • Loading branch information
kyakuno committed Mar 9, 2021
1 parent 17e6449 commit 06a73fd
Show file tree
Hide file tree
Showing 15 changed files with 52 additions and 43 deletions.
Binary file modified object_detection/centernet/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
52 changes: 29 additions & 23 deletions object_detection/efficientdet/efficientdet.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
sys.path.append('../../util')
from utils import get_base_parser, update_parser, get_savepath # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from detector_utils import load_image # noqa: E402
from detector_utils import load_image, plot_results # noqa: E402
from nms_utils import bb_intersection_over_union # noqa: E402
from webcamera_utils import get_capture # noqa: E402

Expand Down Expand Up @@ -191,22 +191,26 @@ def postprocess(
return out


def display(preds, imgs):
for i in range(len(imgs)):
if len(preds[i]['rois']) == 0:
continue
def convert_to_ailia_detector_object(preds, w, h):
i = 0
detector_object = []
for j in range(len(preds[i]['rois'])):
(x1, y1, x2, y2) = preds[i]['rois'][j].astype(np.int)
obj = preds[i]['class_ids'][j]
score = float(preds[i]['scores'][j])

for j in range(len(preds[i]['rois'])):
(x1, y1, x2, y2) = preds[i]['rois'][j].astype(np.int)
cv2.rectangle(imgs[i], (x1, y1), (x2, y2), (255, 255, 0), 2)
obj = obj_list[preds[i]['class_ids'][j]]
score = float(preds[i]['scores'][j])
r = ailia.DetectorObject(
category=obj,
prob=score,
x=x1 / w,
y=y1 / h,
w=(x2 - x1) / w,
h=(y2 - y1) / h,
)

cv2.putText(imgs[i], '{}, {:.3f}'.format(obj, score),
(x1, y1 + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5,
(255, 255, 0), 1)
detector_object.append(r)

return imgs
return detector_object


# ======================
Expand Down Expand Up @@ -267,26 +271,26 @@ def recognize_from_image(image_path, net):
# inference
logger.info('Start inference...')
if args.benchmark:
# if not args.onnx:
# net.set_profile_mode(True)
if not args.profile:
net.set_profile_mode(True)
logger.info('BENCHMARK mode')
for i in range(5):
start = int(round(time.time() * 1000))
pred = predict(img, net)
end = int(round(time.time() * 1000))
logger.info(f'\tailia processing time {end - start} ms')
# if not args.onnx:
# print(net.get_summary())
if not args.profile:
print(net.get_summary())
else:
pred = predict(img, net)

# plot result
imgs = display(pred, [img])
detect_object = convert_to_ailia_detector_object(pred, img.shape[1], img.shape[0])
img = plot_results(detect_object, img, obj_list)

# plot result
savepath = get_savepath(args.savepath, image_path)
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, imgs[0])
cv2.imwrite(savepath, img)

if args.profile:
print(net.get_summary())
Expand All @@ -305,8 +309,10 @@ def recognize_from_video(video, net):
pred = predict(frame, net)

# plot result
imgs = display(pred, [frame])
cv2.imshow('frame', imgs[0])
detect_object = convert_to_ailia_detector_object(pred, frame.shape[1], frame.shape[0])
img = plot_results(detect_object, frame, obj_list)

cv2.imshow('frame', img)

capture.release()
logger.info('Script finished successfully.')
Expand Down
Binary file modified object_detection/efficientdet/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
41 changes: 22 additions & 19 deletions object_detection/m2det/m2det.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
sys.path.append('../../util')
from utils import get_base_parser, update_parser, get_savepath # noqa: E402
from model_utils import check_and_download_models # noqa: E402
from detector_utils import load_image # noqa: E402
from detector_utils import plot_results, load_image # noqa: E402
import webcamera_utils # noqa: E402

# logger
Expand Down Expand Up @@ -115,25 +115,23 @@ def to_color(indx, base):
COLORS = [to_color(x, BASE) for x in range(len(COCO_CATEGORY))]


def draw_detection(im, bboxes, scores, cls_inds):
imgcv = np.copy(im)
h, w, _ = imgcv.shape
def convert_to_ailia_detector_object(bboxes, scores, cls_inds, w, h):
detector_object = []
for i, box in enumerate(bboxes):
cls_indx = int(cls_inds[i])
box = [int(_) for _ in box]
thick = int((h + w) / 300)
cv2.rectangle(
imgcv,
(box[0], box[1]),
(box[2], box[3]),
COLORS[cls_indx],
thick

r = ailia.DetectorObject(
category=cls_indx,
prob=scores[i],
x=box[0] / w,
y=box[1] / h,
w=(box[2] - box[0]) / w,
h=(box[3] - box[1]) / h,
)
mess = '%s: %.3f' % (COCO_CATEGORY[cls_indx], scores[i])
cv2.putText(imgcv, mess, (box[0], box[1] - 7),
0, 1e-3 * h, COLORS[cls_indx], thick // 3)
return imgcv

detector_object.append(r)

return detector_object

# ======================
# Main functions
Expand Down Expand Up @@ -212,10 +210,12 @@ def recognize_from_image(filename, detector):
pass

# show image
im2show = draw_detection(img, boxes, scores, cls_inds)
detect_object = convert_to_ailia_detector_object(boxes, scores, cls_inds, img.shape[1], img.shape[0])
img = plot_results(detect_object, img, COCO_CATEGORY)

savepath = get_savepath(args.savepath, filename)
logger.info(f'saved at : {savepath}')
cv2.imwrite(savepath, im2show)
cv2.imwrite(savepath, img)

if args.profile:
print(detector.get_summary())
Expand All @@ -241,7 +241,10 @@ def recognize_from_video(video, detector):
break

boxes, scores, cls_inds = detect_objects(img, detector)
img = draw_detection(img, boxes, scores, cls_inds)

# show image
detect_object = convert_to_ailia_detector_object(boxes, scores, cls_inds, img.shape[1], img.shape[0])
img = plot_results(detect_object, img, COCO_CATEGORY)
cv2.imshow('frame', img)

# save results
Expand Down
Binary file modified object_detection/m2det/output.png
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 object_detection/mobilenet_ssd/output.png
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 object_detection/pedestrian_detection/output.png
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 object_detection/yolov1-tiny/output.png
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 object_detection/yolov2/output.png
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 object_detection/yolov3-tiny/output.png
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 object_detection/yolov3/output.png
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 object_detection/yolov4-tiny/output.png
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 object_detection/yolov4/output.png
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 object_detection/yolov5/output.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion util/detector_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ def plot_results(detector, img, category, segm_masks=None, logging=True):
color = colors[idx]
cv2.rectangle(img, top_left, bottom_right, color, thickness=-1)

text_color = (255,255,255)
text_color = (255,255,255,255)
cv2.putText(
img,
text,
Expand Down

0 comments on commit 06a73fd

Please sign in to comment.