Spaces:
Runtime error
Runtime error
Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,94 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
try:
|
| 2 |
+
import detectron2
|
| 3 |
+
except:
|
| 4 |
+
import os
|
| 5 |
+
os.system('pip install git+https://github.com/facebookresearch/detectron2.git')
|
| 6 |
+
import gradio as gr
|
| 7 |
+
import cv2
|
| 8 |
+
import torch
|
| 9 |
+
import numpy as np
|
| 10 |
+
import base64
|
| 11 |
+
import json
|
| 12 |
+
import io
|
| 13 |
+
import json
|
| 14 |
+
from PIL import Image
|
| 15 |
+
from detectron2.engine import DefaultPredictor
|
| 16 |
+
from detectron2.config import get_cfg
|
| 17 |
+
from detectron2.utils.visualizer import Visualizer
|
| 18 |
+
from detectron2.data import MetadataCatalog
|
| 19 |
+
from huggingface_hub import hf_hub_download
|
| 20 |
+
import os
|
| 21 |
+
|
| 22 |
+
model_path = hf_hub_download(repo_id="SalmanAboAraj/FinalModel", filename="model_final.pth", token=os.getenv('Token'))
|
| 23 |
+
config_path = hf_hub_download(repo_id="SalmanAboAraj/FinalModel", filename="config.yaml", token=os.getenv('Token'))
|
| 24 |
+
metadata_path = hf_hub_download(repo_id="SalmanAboAraj/FinalModel", filename="metadata.json", token=os.getenv('Token'))
|
| 25 |
+
|
| 26 |
+
|
| 27 |
+
cfg = get_cfg()
|
| 28 |
+
cfg.merge_from_file(config_path)
|
| 29 |
+
cfg.MODEL.WEIGHTS = model_path
|
| 30 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.7
|
| 31 |
+
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 32 |
+
|
| 33 |
+
predictor = DefaultPredictor(cfg)
|
| 34 |
+
|
| 35 |
+
selected_classes = {1, 2, 3, 5, 7, 10, 15, 17, 18}
|
| 36 |
+
|
| 37 |
+
def process_image_base64(image_base64):
|
| 38 |
+
image_data = base64.b64decode(image_base64)
|
| 39 |
+
image_rgb = Image.open(io.BytesIO(image_data)).convert("RGB")
|
| 40 |
+
original_size = image_rgb.size
|
| 41 |
+
|
| 42 |
+
image_rgb = image_rgb.resize((512, 512))
|
| 43 |
+
image_np = np.array(image_rgb)
|
| 44 |
+
image_bgr = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 45 |
+
|
| 46 |
+
outputs = predictor(image_bgr)
|
| 47 |
+
instances = outputs["instances"].to("cpu")
|
| 48 |
+
|
| 49 |
+
if len(instances) == 0:
|
| 50 |
+
return {"image": []}
|
| 51 |
+
|
| 52 |
+
if instances.has("pred_boxes"):
|
| 53 |
+
instances.remove("pred_boxes")
|
| 54 |
+
if instances.has("scores"):
|
| 55 |
+
instances.remove("scores")
|
| 56 |
+
|
| 57 |
+
pred_classes = instances.pred_classes.numpy()
|
| 58 |
+
|
| 59 |
+
# mask = np.isin(pred_classes, list(selected_classes))
|
| 60 |
+
# filtered_instances = instances[mask]
|
| 61 |
+
|
| 62 |
+
# if len(filtered_instances) == 0:
|
| 63 |
+
# return {"image": []}
|
| 64 |
+
|
| 65 |
+
if len(instances) == 0:
|
| 66 |
+
return {"image": []}
|
| 67 |
+
|
| 68 |
+
# mask_shape = filtered_instances[0].pred_masks.shape[1:]
|
| 69 |
+
mask_shape = instances[0].pred_masks.shape[1:]
|
| 70 |
+
image_mask = np.zeros(mask_shape, dtype=np.int8)
|
| 71 |
+
|
| 72 |
+
# for i in range(len(filtered_instances)):
|
| 73 |
+
# class_id = filtered_instances[i].pred_classes.item() - 1
|
| 74 |
+
# mask_np = filtered_instances[i].pred_masks.numpy().squeeze()
|
| 75 |
+
# image_mask[mask_np] = class_id
|
| 76 |
+
|
| 77 |
+
for i in range(len(instances)):
|
| 78 |
+
class_id = instances[i].pred_classes.item()
|
| 79 |
+
mask_np = instances[i].pred_masks.numpy().squeeze()
|
| 80 |
+
image_mask[mask_np] = class_id
|
| 81 |
+
|
| 82 |
+
image_mask_resized = cv2.resize(image_mask, original_size, interpolation=cv2.INTER_NEAREST)
|
| 83 |
+
|
| 84 |
+
return {"image": image_mask_resized.tolist()}
|
| 85 |
+
|
| 86 |
+
iface = gr.Interface(
|
| 87 |
+
fn=process_image_base64,
|
| 88 |
+
inputs="text",
|
| 89 |
+
outputs="json",
|
| 90 |
+
title="Detectron2 Object Detection",
|
| 91 |
+
description="Upload an image (Base64) to get a processed mask output."
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
iface.launch()
|