Spaces:
No application file
No application file
Upload 2 files
Browse files- app (2).py +85 -0
- model_final (6).pth +3 -0
app (2).py
ADDED
|
@@ -0,0 +1,85 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
import detectron2
|
| 3 |
+
except:
|
| 4 |
+
import os
|
| 5 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
| 6 |
+
|
| 7 |
+
from matplotlib.pyplot import axis
|
| 8 |
+
import gradio as gr
|
| 9 |
+
import requests
|
| 10 |
+
import numpy as np
|
| 11 |
+
from torch import nn
|
| 12 |
+
import requests
|
| 13 |
+
|
| 14 |
+
import torch
|
| 15 |
+
import detectron2
|
| 16 |
+
from detectron2 import model_zoo
|
| 17 |
+
from detectron2.engine import DefaultPredictor
|
| 18 |
+
from detectron2.config import get_cfg
|
| 19 |
+
from detectron2.utils.visualizer import Visualizer
|
| 20 |
+
from detectron2.data import MetadataCatalog
|
| 21 |
+
from detectron2.utils.visualizer import ColorMode
|
| 22 |
+
|
| 23 |
+
model_path = 'model_final.pth'
|
| 24 |
+
|
| 25 |
+
cfg = get_cfg()
|
| 26 |
+
cfg.merge_from_file(model_zoo.get_config_file("COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"))
|
| 27 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.75
|
| 28 |
+
cfg.MODEL.ROI_HEADS.NUM_CLASSES = 19
|
| 29 |
+
cfg.MODEL.WEIGHTS = model_path
|
| 30 |
+
|
| 31 |
+
|
| 32 |
+
if not torch.cuda.is_available():
|
| 33 |
+
cfg.MODEL.DEVICE='cpu'
|
| 34 |
+
|
| 35 |
+
predictor = DefaultPredictor(cfg)
|
| 36 |
+
my_metadata = MetadataCatalog.get("car_part_merged_dataset_val")
|
| 37 |
+
my_metadata.thing_classes = ['_background_',
|
| 38 |
+
'back_bumper',
|
| 39 |
+
'back_glass',
|
| 40 |
+
'back_left_door',
|
| 41 |
+
'back_left_light',
|
| 42 |
+
'back_right_door',
|
| 43 |
+
'back_right_light',
|
| 44 |
+
'front_bumper',
|
| 45 |
+
'front_glass',
|
| 46 |
+
'front_left_door',
|
| 47 |
+
'front_left_light',
|
| 48 |
+
'front_right_door',
|
| 49 |
+
'front_right_light',
|
| 50 |
+
'hood',
|
| 51 |
+
'left_mirror',
|
| 52 |
+
'right_mirror',
|
| 53 |
+
'tailgate',
|
| 54 |
+
'trunk',
|
| 55 |
+
'wheel']
|
| 56 |
+
|
| 57 |
+
def inference(image):
|
| 58 |
+
print(image.height)
|
| 59 |
+
|
| 60 |
+
height = image.height
|
| 61 |
+
|
| 62 |
+
# img = np.array(image.resize((500, height)))
|
| 63 |
+
img = np.array(image)
|
| 64 |
+
outputs = predictor(img)
|
| 65 |
+
v = Visualizer(img[:, :, ::-1],
|
| 66 |
+
metadata=my_metadata,
|
| 67 |
+
scale=0.5,
|
| 68 |
+
instance_mode=ColorMode.SEGMENTATION # remove the colors of unsegmented pixels. This option is only available for segmentation models
|
| 69 |
+
)
|
| 70 |
+
#v = Visualizer(img,scale=1.2)
|
| 71 |
+
#print(outputs["instances"].to('cpu'))
|
| 72 |
+
out = v.draw_instance_predictions(outputs["instances"])
|
| 73 |
+
|
| 74 |
+
return out.get_image()[:, :, ::-1]
|
| 75 |
+
|
| 76 |
+
title = "Detectron2 Car Parts Detection"
|
| 77 |
+
description = "This demo introduces an interactive playground for our trained Detectron2 model."
|
| 78 |
+
|
| 79 |
+
gr.Interface(
|
| 80 |
+
inference,
|
| 81 |
+
[gr.inputs.Image(type="pil", label="Input")],
|
| 82 |
+
gr.outputs.Image(type="numpy", label="Output"),
|
| 83 |
+
title=title,
|
| 84 |
+
description=description,
|
| 85 |
+
examples=[]).launch()
|
model_final (6).pth
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f6e4cbbed694033cd36dfd03bb6f78c16e854ecf23834245099b7c468ecee643
|
| 3 |
+
size 351792243
|