Spaces:
Build error
Build error
Pranjal Sharma
commited on
Commit
•
38eb793
1
Parent(s):
3e1ce16
new
Browse files
app.py
CHANGED
@@ -1,7 +1,44 @@
|
|
|
|
1 |
import gradio as gr
|
|
|
|
|
|
|
|
|
|
|
|
|
2 |
|
3 |
-
|
4 |
-
|
|
|
|
|
5 |
|
6 |
-
|
7 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import numpy as np
|
2 |
import gradio as gr
|
3 |
+
import cv2
|
4 |
+
from detectron2 import model_zoo
|
5 |
+
from detectron2.engine import DefaultPredictor
|
6 |
+
from detectron2.config import get_cfg
|
7 |
+
from detectron2.utils.visualizer import Visualizer
|
8 |
+
from detectron2.data import MetadataCatalog
|
9 |
|
10 |
+
# Setup detectron2 logger
|
11 |
+
import detectron2
|
12 |
+
from detectron2.utils.logger import setup_logger
|
13 |
+
setup_logger()
|
14 |
|
15 |
+
def detect_objects(input_img):
|
16 |
+
# Load image
|
17 |
+
im = input_img.copy()
|
18 |
+
|
19 |
+
# Configuration
|
20 |
+
cfg = get_cfg()
|
21 |
+
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
22 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
23 |
+
cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml")
|
24 |
+
|
25 |
+
# Prediction
|
26 |
+
predictor = DefaultPredictor(cfg)
|
27 |
+
outputs = predictor(im)
|
28 |
+
|
29 |
+
# Visualization
|
30 |
+
v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=1.9)
|
31 |
+
out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
|
32 |
+
result_image = out.get_image()[:, :, ::-1]
|
33 |
+
|
34 |
+
return result_image
|
35 |
+
|
36 |
+
# Interface
|
37 |
+
image = gr.Image()
|
38 |
+
output_image = gr.Image()
|
39 |
+
|
40 |
+
title = "Object Detection using Mask R-CNN"
|
41 |
+
description = "This app detects objects in the input image using Mask R-CNN."
|
42 |
+
examples = [["./input.png"]]
|
43 |
+
|
44 |
+
gr.Interface(detect_objects, [image], output_image, title=title, description=description, examples=examples).launch()
|
input.png
ADDED