def person_blocker(args): # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) image = imageio.imread(args.image) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] # Filter masks to only the selected objects objects = np.array(args.objects) # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero(np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) mask_selected = mask_selected.astype(np.uint8) # Dilate Mask mask_selected = dilate_mask(mask_selected) # Replace object masks with noise # image_masked = image.copy() # mask = create_mask_color(image, [255, 255, 255]) # image_masked[mask_selected > 0] = mask[mask_selected > 0] # imageio.imwrite(args.output, image_masked) mask_selected[mask_selected > 0] = 255 mask_selected = 255 - mask_selected imageio.imwrite(args.output, mask_selected)
def person_blocker(args): # If the output path does not exist, create it if not os.path.exists(args.write_output): os.makedirs(args.write_output) # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) # Start reading the frames reader = imageio.get_reader(args.image) vlen = len(reader) for i, image in enumerate(reader): # Output frame number print(str(i) + "/" + str(vlen)) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] position_ids = [''.format(x) for x in range(r['class_ids'].shape[0])] outstr = (args.write_output + "/labeled{:06d}.png").format(i) visualize_labelmod.display_instances( image, r['rois'], r['masks'], r['class_ids'], get_class_names(), scores=r['scores'], selected_class=args.selected_class, outname=outstr)
def person_blocker(image_path, sender_id): image = imageio.imread(image_path) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] # Filter masks to only the selected objects objects = np.array('person') # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero(np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) # Replace object masks with white noise mask_color = (255, 255, 255) image_masked = image.copy() noisy_color = create_noisy_color(image, mask_color) image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] dest = "/tmp/{0}".format(image_path.split("/")[-1]) imageio.imwrite(dest, image_masked) key = "fb/processed/{0}".format(sender_id + dest) # Upload image to S3 as the FB need URL to send image in messages response = s3.put_object( ACL='public-read', Body=open(dest, 'rb').read(), Bucket=BUCKET, ContentType="image/png", Key=key, ) file_url = "https://s3.amazonaws.com/{0}/{1}".format(BUCKET, key) print(file_url) send_to_fb(sender_id, file_url)
def person_blocker(args): # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) image = imageio.imread(args.image) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] if args.labeled: position_ids = [ '[{}]'.format(x) for x in range(r['class_ids'].shape[0]) ] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], get_class_names(), position_ids) sys.exit() # Filter masks to only the selected objects objects = np.array(args.objects) # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero(np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) # Replace object masks with noise mask_color = string_to_rgb_triplet(args.color) image_masked = image.copy() noisy_color = create_noisy_color(image, mask_color) image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] imageio.imwrite('person_blocked.png', image_masked) # Create GIF. The noise will be random for each frame, # which creates a "static" effect images = [image_masked] num_images = 10 # should be a divisor of 30 for _ in range(num_images - 1): new_image = image.copy() noisy_color = create_noisy_color(image, mask_color) new_image[mask_selected > 0] = noisy_color[mask_selected > 0] images.append(new_image) imageio.mimsave('person_blocked.gif', images, fps=30., subrectangles=True)
default=None) parser.add_argument('-o', '--objects', nargs='+', help='object(s)/object ID(s) to block. ' + 'Use the -names flag to print a list of ' + 'valid objects', default='person') parser.add_argument('-c', '--color', nargs='?', default='(255, 255, 255)', help='color of the "block"') parser.add_argument('-l', '--labeled', dest='labeled', action='store_true', help='generate labeled image instead') parser.add_argument('-n', '--names', dest='names', action='store_true', help='prints class names and exits.') parser.set_defaults(labeled=False, names=False) args = parser.parse_args() if args.names: print(get_class_names()) sys.exit() person_blocker(args)
def person_blocker(args): # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) image = imageio.imread(filename) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] if args.labeled: position_ids = ['[{}]'.format(x) for x in range(r['class_ids'].shape[0])] visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], get_class_names(), position_ids) sys.exit() # Filter masks to only the selected objects objects = np.array(args.objects) # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero(np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) # Replace object masks with noise mask_color = string_to_rgb_triplet(args.color) image_masked = image.copy() noisy_color = create_noisy_color(image, mask_color) image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] img_dir = '/Volumes/seagate/projects/person-blocker/images2/' img_pre = 'person_blocked_{0}'.format(strftime("%Y%m%d%H%M%S",gmtime())) img_name = img_dir + img_pre + '.png' gif_name = img_dir + img_pre + '.gif' imageio.imwrite(img_name, image_masked) # Create GIF. The noise will be random for each frame, # which creates a "static" effect # this works great, but takes some time and produces 7+ meg file #images = [image_masked] #num_images = 10 # should be a divisor of 30 # #for _ in range(num_images - 1): # new_image = image.copy() # noisy_color = create_noisy_color(image, mask_color) # new_image[mask_selected > 0] = noisy_color[mask_selected > 0] # images.append(new_image) # #imageio.mimsave(gif_name, images, fps=30., subrectangles=True) # print json try: # Create unique image name uniqueid = 'person_uuid_{0}_{1}'.format(strftime("%Y%m%d%H%M%S%f",gmtime()),uuid.uuid4()) host = os.uname()[1] currenttime= strftime("%Y-%m-%d %H:%M:%S",gmtime()) ipaddress = IP_address() end = time.time() row = { 'uuid': uniqueid, 'runtime': str(round(end - start)), 'host': host, 'ts': currenttime, 'ipaddress': ipaddress, 'imagefilename': img_pre, 'originalfilename': filename } print( json.dumps(row) ) except: print("{\"message\": \"Failed to run\"}")
def person_blocker(args): # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) COCO_MODEL_PATH = args.model or os.path.join(SCRIPT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(SCRIPT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) image = imageio.imread(args.infile) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] dirname = os.path.dirname(args.infile) basename = os.path.basename(args.infile) barename = os.path.splitext(basename)[0] if args.objects is None: # now paste it all together outfile = os.path.join(dirname, "labels_{}.png".format(barename)) position_ids = [ '[{}]'.format(x) for x in range(r['class_ids'].shape[0]) ] print("Saving: {}".format(outfile)) visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], get_class_names(), position_ids, outfile=outfile) sys.exit() # Object IDs: indices_list = [] objects_list = list(args.objects.split(",")) for objects_entry in objects_list: if np.chararray.isnumeric(objects_entry): indices_list += [objects_entry] else: selected_class_ids = np.flatnonzero( np.in1d(get_class_names(), [objects_entry])) indices_list += np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)).tolist() object_indices = np.asarray(indices_list).astype(int) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) # Replace object masks with noise mask_colors = list(map(string_to_rgb_triplet, args.colors.split(","))) if args.bgcolor.lower() != "none": bg_color = string_to_rgb_triplet(args.bgcolor) image_masked = np.full(shape=(image.shape[0], image.shape[1], 3), fill_value=bg_color).astype(np.uint8) else: image_masked = image.copy() noisy_color = create_noisy_color(image, mask_colors[0]) image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] outfile = os.path.join(dirname, "mask_{}.png".format(barename)) print("Saving: {}".format(outfile)) imageio.imwrite(outfile, image_masked)
def person_blocker(args): # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) print("[INFO] starting video stream...") vs = VideoStream(src=0).start() time.sleep(2.0) fps = FPS().start() while True: frame = vs.read() frame = imutils.resize(frame, width=1000) image = imageio.imread(frame) results = model.detect([image], verbose=0) r = results[0] # if args.labeled: # position_ids = ['[{}]'.format(x) # for x in range(r['class_ids'].shape[0])] # visualize.display_instances(img, r['rois'], # r['masks'], r['class_ids'], # get_class_names(), position_ids) # sys.exit() # Filter masks to only the selected objects objects = np.array(args.objects) # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero(np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) if len(object_indices) > 0 else None # Replace object masks with noise # mask_color = string_to_rgb_triplet(args.color) image_masked = img.copy() # noisy_color = create_noisy_color(image, mask_color) noisy_color = create_noisy_color(img, (208, 130, 238)) if mask_selected is not None: image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] image_masked = imutils.resize(image_masked, width=1500) cv2.imshow("Frame", image_masked) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break update the FPS counter fps.update() fps.stop() print("[INFO] elapsed time: {:.2f}".format(fps.elapsed())) print("[INFO] approx. FPS: {:.2f}".format(fps.fps())) do a bit of cleanup cv2.destroyAllWindows() vs.stop() exit()
def person_blocker(args): # If the output path does not exist, create it if not os.path.exists(args.write_output): os.makedirs(args.write_output) # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) # Start reading the frames reader = imageio.get_reader(args.image) vlen = len(reader) for i, image in enumerate(reader): # Output frame number print(str(i)+"/"+str(vlen)) # Create masks for all objects results = model.detect([image], verbose=0) r = results[0] # if args.labeled: # position_ids = ['[{}]'.format(x) # for x in range(r['class_ids'].shape[0])] # visualize.display_instances(image, r['rois'], # r['masks'], r['class_ids'], # get_class_names(), position_ids) # sys.exit() # Filter masks to only the selected objects objects = np.array(args.objects) # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero(np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) # Replace object masks with noise mask_color = string_to_rgb_triplet(args.color) image_masked = image.copy() noisy_color = create_noisy_color(image, mask_color) image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] # Write number of detected objects n_persons = sum(r['class_ids'] == 1) img = Image.fromarray(image_masked.astype(np.uint8), 'RGB') draw = ImageDraw.Draw(img) font = ImageFont.truetype("arial.ttf", 60) draw.text((0, 0), "N="+str(n_persons), fill=(255,0,0), font = font) # plt.imshow(image_masked.astype(np.uint8)) # plt.text(0, 0, "N = " + str(n_persons), fontsize=20, color = "red") # Save masked frame #plt.show() outstr = (args.write_output + "/frame{:06d}.jpg").format(i) img.save(outstr)
def person_blocker(args): # Required to load model, but otherwise unused ROOT_DIR = os.getcwd() COCO_MODEL_PATH = args.model or os.path.join(ROOT_DIR, "mask_rcnn_coco.h5") MODEL_DIR = os.path.join(ROOT_DIR, "logs") # Required to load model if not os.path.exists(COCO_MODEL_PATH): utils.download_trained_weights(COCO_MODEL_PATH) # Load model and config config = InferenceConfig() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(COCO_MODEL_PATH, by_name=True) print("[INFO] starting video stream...") url = f"http://*****:*****@140.193.201.45:8080/shot.jpg" while True: # Create masks for all objects img_response = requests.get(url) img_array = np.array(bytearray(img_response.content), dtype=np.uint8) if img_array is not None: img = cv2.imdecode(img_array, -1) results = model.detect([img], verbose=0) r = results[0] # Filter masks to only the selected objects objects = np.array(args.objects) # Object IDs: if np.all(np.chararray.isnumeric(objects)): object_indices = objects.astype(int) # Types of objects: else: selected_class_ids = np.flatnonzero( np.in1d(get_class_names(), objects)) object_indices = np.flatnonzero( np.in1d(r['class_ids'], selected_class_ids)) mask_selected = np.sum(r['masks'][:, :, object_indices], axis=2) if len(object_indices) > 0 else None # Replace object masks with noise # mask_color = string_to_rgb_triplet(args.color) image_masked = img.copy() # noisy_color = create_noisy_color(image, mask_color) noisy_color = create_noisy_color(img, (208, 130, 238)) if mask_selected is not None: image_masked[mask_selected > 0] = noisy_color[mask_selected > 0] image_masked = imutils.resize(image_masked, width=1500) cv2.imshow("Frame", image_masked) key = cv2.waitKey(1) & 0xFF if key == ord("q"): break exit()