def run_detect(filename): base_file_name = os.path.basename(filename) saved_file_name = os.path.join(MASKED_DIR, base_file_name) logging.info('Loading image: %s', base_file_name) # Convert png with alpha channel with shape[2] == 4 into shape[2] ==3 RGB images image = skimage.io.imread(filename) if len(image.shape) > 2 and image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR) logging.info('Image info: %s', image) # Run detection results = model.detect([image], verbose=1) r = results[0] logging.info('Runing model.detect([image], verbose=1).') # Just apply mask then save images print_img = visualize.apply_mask_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'], None, None, None, None, None, [(1.0, 1.0, 1.0)]) skimage.io.imsave(saved_file_name, print_img) logging.info('Finished apply_mask_instances.') return True
def run_detect(filename): base_file_name = os.path.basename(filename) base_dir_name = os.path.dirname(filename) logging.info('Dir name: %s',base_dir_name) logging.info('File name: %s',base_file_name) split_file_name, split_file_ext = os.path.splitext(base_file_name) saved_dir_name = 'masked' saved_file_name = str.join('\\', (base_dir_name, saved_dir_name, base_file_name)) logging.info('Saved as: %s',saved_file_name) # Convert png with alpha channel with shape[2] == 4 into shape[2] ==3 RGB images image = skimage.io.imread(filename) if len(image.shape) > 2 and image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR) # Run detection results = model.detect([image], verbose=1) r= results[0] # Just apply mask then save images print_img = visualize.apply_mask_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) skimage.io.imsave(saved_file_name,print_img) return True
# convert png with alpha channel with shape[2] == 4 into shape[2] ==3 RGB images image = skimage.io.imread(filename) if len(image.shape) > 2 and image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR) # processing time log t0 = time.perf_counter() # Run detection results = model.detect([image], verbose=1) r = results[0] # without displaying images for batch program ###print_img = visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) ###skimage.io.imsave(saved_file_name,print_img) # just apply mask then save images print_img = visualize.apply_mask_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) skimage.io.imsave(saved_file_name, print_img) # processing time log t1 = time.perf_counter() print('LOGGING:::::: Completed in %f sec' % (t1 - t0)) print('LOGGING:::::: End\n') # end
def apply_mask_to_image(config_name, weights_name): # Class names # Index of the class in the list is its ID. For example, to get ID of # the teddy bear class, use: class_names.index('teddy bear') class_names = ['BG', 'numberplate'] # Create model object in inference mode. model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config_name) # Load pre-trained weights NUMBERPLATE_WEIGHTS_FILE = os.path.join(NUMBERPLATE_WEIGHTS_PATH, weights_name) model.load_weights(NUMBERPLATE_WEIGHTS_FILE, by_name=True) logging.info('%s weights file loaded.' % args.weights) # Validation images extension image_type_ok_list = ['jpeg', 'png', 'gif', 'bmp'] # Loop every file in the folder file_count = 0 for file_names in os.scandir(IMAGE_DIR): if file_names.is_file(): image_type = imghdr.what(file_names) if image_type in image_type_ok_list: # Start file_count += 1 logging.info('Counting %d start' % file_count) # Date time log img_timestamp = time.localtime() date_string = str.join('/', (str(img_timestamp.tm_year), str(img_timestamp.tm_mon).zfill(2), str(img_timestamp.tm_mday).zfill(2))) time_string = str.join(':', (str(img_timestamp.tm_hour).zfill(2), str(img_timestamp.tm_min).zfill(2), str(img_timestamp.tm_sec).zfill(2))) logging.info('Date Time: %s %s', date_string, time_string) # Filename and filepath log filename = os.path.join(IMAGE_DIR, file_names) logging.info('Loading image: %s', filename) # Convert png with alpha channel with shape[2] == 4 into shape[2] ==3 RGB images image = skimage.io.imread(filename) if len(image.shape) > 2 and image.shape[2] == 4: image = cv2.cvtColor(image, cv2.COLOR_BGRA2BGR) # Processing start time t0 = time.perf_counter() # Run detection results = model.detect([image], verbose=1) r = results[0] # Just apply mask then save images base_file_name = os.path.basename(filename) base_dir_name = os.path.dirname(filename) saved_dir_name = 'masked' saved_file_name = str.join( '\\', (base_dir_name, saved_dir_name, base_file_name)) print_img = visualize.apply_mask_instances( image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores']) skimage.io.imsave(saved_file_name, print_img) logging.info('Saved masked image as: %s', saved_file_name) # Processing ending time t1 = time.perf_counter() logging.info('Completed 1 image in %f sec', (t1 - t0)) logging.info('End') return True