Spaces:
Build error
Build error
souranil3d
commited on
Commit
•
08aa404
1
Parent(s):
da3a604
Base inference
Browse files- .gitattributes +1 -0
- app.py +34 -0
- checkpoints_model_final_imagenet_40k_synthetic.pth +3 -0
- example.jpg +0 -0
- requirements.txt +0 -0
- utils.py +43 -0
.gitattributes
CHANGED
@@ -25,3 +25,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
25 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
26 |
*.zstandard filter=lfs diff=lfs merge=lfs -text
|
27 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
28 |
+
checkpoints_model_final_imagenet_40k_synthetic.pth filter=lfs diff=lfs merge=lfs -text
|
app.py
ADDED
@@ -0,0 +1,34 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import gradio as gr
|
2 |
+
import torch
|
3 |
+
import torchvision
|
4 |
+
import logging
|
5 |
+
from detectron2.engine import DefaultPredictor
|
6 |
+
import cv2
|
7 |
+
from detectron2.config import get_cfg
|
8 |
+
from src.utils.visualizer import add_bboxes
|
9 |
+
|
10 |
+
config_file="config.yaml"
|
11 |
+
cfg = get_cfg()
|
12 |
+
cfg.merge_from_file(config_file)
|
13 |
+
cfg.MODEL.DEVICE="cpu"
|
14 |
+
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
|
15 |
+
cfg.MODEL.WEIGHTS = "checkpoints_model_final_imagenet_40k_synthetic.pth.pth"
|
16 |
+
|
17 |
+
def predict(
|
18 |
+
config_file, checkpoint_file, img_path
|
19 |
+
):
|
20 |
+
predictor = DefaultPredictor(cfg)
|
21 |
+
im = cv2.imread(img_path)
|
22 |
+
output = predictor(im)
|
23 |
+
img = add_bboxes(im, output['instances'].pred_boxes, scores=output['instances'].scores)
|
24 |
+
return img
|
25 |
+
|
26 |
+
title = "Pet Detection"
|
27 |
+
description = "Demo for Indoor Pet Detection"
|
28 |
+
examples = [['example.jpg']]
|
29 |
+
|
30 |
+
|
31 |
+
gr.Interface(predict, inputs=gr.inputs.Image(type="file"), outputs=gr.outputs.Image(type="pil"),enable_queue=True, title=title,
|
32 |
+
description=description,
|
33 |
+
# article=article,
|
34 |
+
examples=examples).launch()
|
checkpoints_model_final_imagenet_40k_synthetic.pth
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:9e71df17784bb168aec2655fe76c196ece86706467c18080d1dc72518ccb444f
|
3 |
+
size 165717485
|
example.jpg
ADDED
requirements.txt
ADDED
File without changes
|
utils.py
ADDED
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import cv2
|
2 |
+
|
3 |
+
|
4 |
+
def draw_text(img, text,
|
5 |
+
font=cv2.FONT_HERSHEY_PLAIN,
|
6 |
+
x=0,
|
7 |
+
y=0,
|
8 |
+
font_scale=1,
|
9 |
+
font_thickness=2,
|
10 |
+
text_color=(0, 255, 0),
|
11 |
+
text_color_bg=(0, 0, 0)
|
12 |
+
):
|
13 |
+
|
14 |
+
text_size, _ = cv2.getTextSize(text, font, font_scale, font_thickness)
|
15 |
+
text_w, text_h = text_size
|
16 |
+
cv2.rectangle(img, (x, y), (x + text_w, y + text_h), text_color_bg, -1)
|
17 |
+
cv2.putText(img, text, (x, y + text_h + font_scale - 1), font, font_scale, text_color, font_thickness)
|
18 |
+
|
19 |
+
return img
|
20 |
+
|
21 |
+
|
22 |
+
def add_bboxes(img, bboxes, **kwargs):
|
23 |
+
label = "dog"
|
24 |
+
font_thickness=2
|
25 |
+
for idx, box in enumerate(bboxes):
|
26 |
+
x1, y1 = int(box[0]), int(box[1])
|
27 |
+
x2, y2 = int(box[2]), int(box[3])
|
28 |
+
if "scores" in kwargs.keys():
|
29 |
+
score = list(kwargs["scores"])[idx]
|
30 |
+
label = "{0:.2f}%".format(score*100)
|
31 |
+
font_thickness=1
|
32 |
+
img = cv2.rectangle(img, (x1, y1), (x2, y2), (36,255,12), 2)
|
33 |
+
img = draw_text(img, label, x=x1, y=y1-10, font_thickness=font_thickness)
|
34 |
+
return img
|
35 |
+
|
36 |
+
|
37 |
+
def reformat_bbox(coord):
|
38 |
+
"""
|
39 |
+
Converts XYWH to XYXY
|
40 |
+
"""
|
41 |
+
x1, y1 = coord[0], coord[1]
|
42 |
+
x2, y2 = x1 + coord[2], y1 + coord[3]
|
43 |
+
return [x1, y1, x2, y2]
|