import numpy as np import gradio as gr import cv2 from detectron2 import model_zoo from detectron2.engine import DefaultPredictor from detectron2.config import get_cfg from detectron2.utils.visualizer import Visualizer from detectron2.data import MetadataCatalog # Setup detectron2 logger import detectron2 from detectron2.utils.logger import setup_logger setup_logger() def detect_objects(input_img): # Load image im = input_img.copy() # Configuration cfg = get_cfg() cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")) cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml") # Prediction predictor = DefaultPredictor(cfg) outputs = predictor(im) # Visualization v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.9) out = v.draw_instance_predictions(outputs["instances"].to("cpu")) result_image = out.get_image()[:, :, ::-1] return result_image # Interface image = gr.Image() output_image = gr.Image() title = "Object Detection using Mask R-CNN" description = "This app detects objects in the input image using Mask R-CNN." examples = [["./input.png"]] gr.Interface(detect_objects, [image], output_image, title=title, description=description, examples=examples).launch()