Skip to content

Instantly share code, notes, and snippets.

@jediofgever
Last active May 21, 2019 02:47
Show Gist options
  • Save jediofgever/12bcab2b6389208c6ecab248bdab1c19 to your computer and use it in GitHub Desktop.
Save jediofgever/12bcab2b6389208c6ecab248bdab1c19 to your computer and use it in GitHub Desktop.
# Copyright (c) Facebook, Inc. and its affiliates. All Rights Reserved.
import argparse
import cv2
import numpy as np
import glob
from maskrcnn_benchmark.config import cfg
from predictor import COCODemo
import time
def main():
parser = argparse.ArgumentParser(description="PyTorch Object Detection Webcam Demo")
parser.add_argument(
"--config-file",
default="../configs/caffe2/e2e_mask_rcnn_X_101_32x8d_FPN_1x_caffe2.yaml",
metavar="FILE",
help="path to config file",
)
parser.add_argument(
"--confidence-threshold",
type=float,
default=0.7,
help="Minimum score for the prediction to be shown",
)
parser.add_argument(
"--min-image-size",
type=int,
default=224,
help="Smallest size of the image to feed to the model. "
"Model was trained with 800, which gives best results",
)
parser.add_argument(
"--show-mask-heatmaps",
dest="show_mask_heatmaps",
help="Show a heatmap probability for the top masks-per-dim masks",
action="store_true",
)
parser.add_argument(
"--masks-per-dim",
type=int,
default=2,
help="Number of heatmaps per dimension to show",
)
parser.add_argument(
"opts",
help="Modify model config options using the command-line",
default=None,
nargs=argparse.REMAINDER,
)
args = parser.parse_args()
# load config from file and command-line arguments
cfg.merge_from_file(args.config_file)
cfg.merge_from_list(args.opts)
cfg.freeze()
# prepare object that handles inference plus adds predictions on top of image
coco_demo = COCODemo(
cfg,
confidence_threshold=args.confidence_threshold,
show_mask_heatmaps=args.show_mask_heatmaps,
masks_per_dim=args.masks_per_dim,
min_image_size=args.min_image_size,
)
number_of_images = 1
for i in range(number_of_images):
img = cv2.imread('/home/atas/kitti_data/2011_09_26/2011_10_03_drive_0042_sync/image_02/data/'+str(i).zfill(10)+'.png')
start_time = time.time()
#scale_percent = 200 # percent of original size
#width = int(img.shape[1] * scale_percent / 100)
#height = int(img.shape[0] * scale_percent / 100)
#dim = (width, height)
# resize image
#resized = cv2.resize(img, dim, interpolation = cv2.INTER_AREA)
composite, top_predictions = coco_demo.run_on_opencv_image(img)
labels = top_predictions.get_field("labels")
bbox = top_predictions.bbox
print(labels, bbox)
cv2.imwrite('/home/atas/kitti_data/2011_09_26/2011_10_03_drive_0042_sync/maskrcnn_detections/detection_image_02/'+str(i).zfill(10)+'.png',composite)
output = open(output_file, 'w')
for i in range(len(labels)):
full_label_bbox = str(labels.data.cpu().numpy()[i]) + ' ' + str(bbox.cpu().numpy()[i][0]) + ' ' + str(bbox.cpu().numpy()[i][1]) + ' ' + str(bbox.cpu().numpy()[i][2]) + ' ' + str(bbox.cpu().numpy()[i][3]) + '\n'
output.write(full_label_bbox)
output.close()
print("Time: {:.2f} s / img".format(time.time() - start_time))
cv2.imshow("COCO detections", composite)
if cv2.waitKey(1) == 27:
break # esc to quit
cv2.destroyAllWindows()
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment