<jupyter_start><jupyter_text>Mask R-CNN demoThis notebook illustrates one possible way of using `maskrcnn_benchmark` for computing predictions on images from an arbitrary URL.Let's start with a few standard imports<jupyter_code>import matplotlib.pyplot as plt
import matplotlib.pylab as pylab

#import requests
from io import BytesIO
from PIL import Image
import numpy as np
# this makes our figures bigger
pylab.rcParams['figure.figsize'] = 20, 12
# Manual Adjust here
import os
print(os.getcwd())
#os.chdir("Projects/maskrcnn-benchmark2")
#print(path.join(path.dirname(os.getcwd()), ''))
from os import path
import sys
sys.path.append(path.join(path.dirname(os.getcwd()), ''))
from maskrcnn_benchmark.config import cfg
from predictor import COCODemo<jupyter_output><empty_output><jupyter_text>Those are the relevant imports for the detection model We provide a helper class `COCODemo`, which loads a model from the config file, and performs pre-processing, model prediction and post-processing for us.We can configure several model options by overriding the config options.In here, we make the model run on the CPU<jupyter_code>config_file = "/Users/mingrenshen/Projects/maskrcnn-benchmark2/configs/predict.yaml"

# update the config options with the config file
cfg.merge_from_file(config_file)
# manual override some options
cfg.merge_from_list(["MODEL.DEVICE", "cpu"])<jupyter_output><empty_output><jupyter_text>Now we create the `COCODemo` object. It contains a few extra options for conveniency, such as the confidence threshold for detections to be shown.<jupyter_code>coco_demo = COCODemo(
    cfg,
    min_image_size=800,
    confidence_threshold=0.7,
)<jupyter_output><empty_output><jupyter_text>Let's define a few helper functions for loading images from a URL<jupyter_code>def load(url):
    """
    Given an url of an image, downloads the image and
    returns a PIL image
    """
    response = requests.get(url)
    pil_image = Image.open(BytesIO(response.content)).convert("RGB")
    # convert to BGR format
    image = np.array(pil_image)[:, :, [2, 1, 0]]
    return image

def imshow(img):
    plt.imshow(img[:, :, [2, 1, 0]])
    plt.axis("off")<jupyter_output><empty_output><jupyter_text>Let's now load an image from the COCO dataset. It's reference is in the comment<jupyter_code># from http://cocodataset.org/#explore?id=345434
import cv2

image = cv2.imread("../datasets/val/grid1_roi2_500kx_0p5nm_haadf1_0035.jpg")
imshow(image)<jupyter_output><empty_output><jupyter_text>Computing the predictionsWe provide a `run_on_opencv_image` function, which takes an image as it was loaded by OpenCV (in `BGR` format), and computes the predictions on them, returning an image with the predictions overlayed on the image.<jupyter_code># compute predictions
predictions = coco_demo.run_on_opencv_image(image)
imshow(predictions)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09404388714733543, 'fraction_numerical': 0.012887495646116336, 'mean_word_length': 5.8708133971291865, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 25, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '229803', 'license': 'MIT', 'n_tokens_mistral': 954, 'n_tokens_neox': 920, 'n_words': 281, 'path': 'demo/Mask_R-CNN_demo.ipynb', 'repo_name': 'iphyer/DefectDetection-MaskRCNN', 'size': '2871'}