Spaces:
Runtime error
Runtime error
File size: 1,861 Bytes
110601a 84cedfd 110601a 3f676f8 110601a f5e76f1 110601a 8f9cd73 110601a 8f9cd73 110601a 8f9cd73 68ef79c f5e76f1 68ef79c fc9264e 68ef79c 7b046ad 110601a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 |
try:
import detectron2
except:
import os
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
from matplotlib.pyplot import axis
import gradio as gr
import requests
import numpy as np
from torch import nn
import requests
import torch
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
from detectron2.utils.visualizer import ColorMode
model_path = 'model_final.pth'
cfg = get_cfg()
cfg.merge_from_file("./configs/faster_rcnn_R_50_FPN_3x.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 1
cfg.MODEL.WEIGHTS = model_path
if not torch.cuda.is_available():
cfg.MODEL.DEVICE='cpu'
predictor = DefaultPredictor(cfg)
my_metadata = MetadataCatalog.get("car_dataset_val")
my_metadata.thing_classes = ["damage"]
def inference(image):
print(image.height)
height = image.height
img = np.array(image.resize((500, height)))
outputs = predictor(img)
v = Visualizer(img[:, :, ::-1],
metadata=my_metadata,
scale=0.5,
instance_mode=ColorMode.SEGMENTATION # remove the colors of unsegmented pixels. This option is only available for segmentation models
)
# v = Visualizer(img,scale=1.2)
out = v.draw_instance_predictions(outputs["pred_masks"].to("cpu"))
return out.get_image()
title = "Detectron2 Car damage Detection"
description = "This demo introduces an interactive playground for our trained Detectron2 model."
gr.Interface(
inference,
[gr.inputs.Image(type="pil", label="Input")],
gr.outputs.Image(type="numpy", label="Output"),
title=title,
description=description,
examples=[]).launch() |