Spaces:
Build error
Build error
File size: 2,331 Bytes
4df19d3 9dcfe1d 4df19d3 | 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 | try:
import detectron2
except:
import os
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
import gradio as gr
import torch
import cv2
import pandas as pd
from detectron2.utils.visualizer import Visualizer
from detectron2.data import MetadataCatalog
from detectron2.config import get_cfg
from detectron2.data.detection_utils import read_image
from detectron2.engine import DefaultPredictor
cfg = get_cfg()
cfg.merge_from_file("resnet.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
cfg.MODEL.DEVICE='cpu'
predictor = DefaultPredictor(cfg)
def inference(image_path):
# load image
image = read_image(image_path, format="BGR")
# Make inference on the image
outputs = predictor(image)
metadata = MetadataCatalog.get("custom_dataset_train")
MetadataCatalog.get("custom_dataset_train").set(thing_classes=['100km/h', '120km/h', '20km/h', '30km/h', '40km/h', '50km/h', '60km/h', '70km/h', '80km/h'])
v = Visualizer(image, metadata, scale=1)
out = v.draw_instance_predictions(outputs['instances'])
# Detection summary table
cls_idxs = outputs['instances'].pred_classes.numpy()
thing_classes=['100km/h', '120km/h', '20km/h', '30km/h', '40km/h', '50km/h', '60km/h', '70km/h', '80km/h']
# get labels from class indices
labels = [thing_classes[i] for i in cls_idxs]
scores = outputs['instances'].scores.numpy()
df = pd.DataFrame({'Detected speed limit': labels, 'Confidence score': scores})
# Return the visualization as an RGB image
return out.get_image()[:, :, ::-1], df
examples = ["examples/1.jpg", "examples/2.jpg", "examples/3.jpg"]
with gr.Blocks(theme='gradio/monochrome') as demo:
gr.Markdown("# Detección de velocidad - one Shot - ")
gr.Markdown("**Edición**: *SITIA*")
with gr.Row():
with gr.Column():
image = gr.Image(type="filepath")
button = gr.Button("Infer")
with gr.Column():
detection = gr.Image(label="Output")
detection_summary = gr.DataFrame(label="Detection summary")
examples_block = gr.Examples(inputs=image, examples=examples, fn=inference, outputs=[detection, detection_summary], cache_examples=True)
button.click(fn=inference, inputs=image, outputs=[detection, detection_summary])
demo.launch()
|