SmitaMahajan commited on
Commit
bb78000
·
verified ·
1 Parent(s): 495db4c

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +81 -3
app.py CHANGED
@@ -1,6 +1,84 @@
 
 
 
 
1
  import gradio as gr
 
 
 
 
 
2
 
3
- def dummy(input_image):
4
- return input_image
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5
 
6
- gr.Interface(fn=dummy, inputs="image", outputs="image").launch()
 
1
+ import os
2
+ import cv2
3
+ import torch
4
+ import numpy as np
5
  import gradio as gr
6
+ from detectron2.config import get_cfg
7
+ from detectron2.engine import DefaultPredictor
8
+ from detectron2 import model_zoo
9
+ from detectron2.utils.visualizer import Visualizer
10
+ from detectron2.data import MetadataCatalog
11
 
12
+ # Setup Detectron2 model
13
+ cfg = get_cfg()
14
+ cfg.merge_from_file(model_zoo.get_config_file(
15
+ "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
16
+ ))
17
+ cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5
18
+ cfg.MODEL.WEIGHTS = model_zoo.get_checkpoint_url(
19
+ "COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
20
+ )
21
+ cfg.MODEL.DEVICE = "cpu" # Ensure CPU for Hugging Face Spaces
22
+
23
+ predictor = DefaultPredictor(cfg)
24
+ metadata = MetadataCatalog.get(cfg.DATASETS.TRAIN[0])
25
+
26
+ # Distance calculation helper
27
+ def calculate_pixel_distance(box1, box2):
28
+ x1, y1 = (box1[0] + box1[2]) / 2, (box1[1] + box1[3]) / 2
29
+ x2, y2 = (box2[0] + box2[2]) / 2, (box2[1] + box2[3]) / 2
30
+ return int(np.linalg.norm([x2 - x1, y2 - y1]))
31
+
32
+ def detect_objects(image):
33
+ image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
34
+ outputs = predictor(image_rgb)
35
+ instances = outputs["instances"].to("cpu")
36
+ boxes = instances.pred_boxes.tensor.numpy()
37
+ classes = instances.pred_classes.numpy()
38
+ class_names = [metadata.get("thing_classes", [])[i] for i in classes]
39
+
40
+ v = Visualizer(image_rgb, metadata, scale=1.0)
41
+ out = v.draw_instance_predictions(instances)
42
+ annotated = out.get_image()
43
+
44
+ # Prepare object list with indices
45
+ objects = [f"{i}: {name}" for i, name in enumerate(class_names)]
46
+ return annotated, boxes.tolist(), objects
47
+
48
+ # Store detected boxes across calls
49
+ global_boxes = []
50
+
51
+ def interface(image):
52
+ global global_boxes
53
+ annotated, boxes, labels = detect_objects(image)
54
+ global_boxes = boxes
55
+ return annotated, gr.update(choices=labels, value=[]), gr.update(choices=labels, value=[])
56
+
57
+ def measure_distance(idx1, idx2):
58
+ try:
59
+ box1 = global_boxes[int(idx1.split(":")[0])]
60
+ box2 = global_boxes[int(idx2.split(":")[0])]
61
+ pixel_dist = calculate_pixel_distance(box1, box2)
62
+ return f"Pixel distance: {pixel_dist}px"
63
+ except Exception:
64
+ return "Error in selection. Please try again."
65
+
66
+ # Gradio UI
67
+ with gr.Blocks() as demo:
68
+ gr.Markdown("## 🧠 Detectron2 Object Detection + Distance Estimation")
69
+ with gr.Row():
70
+ input_img = gr.Image(type="numpy", label="Upload Image")
71
+ output_img = gr.Image(type="numpy", label="Detected Image")
72
+ with gr.Row():
73
+ obj1 = gr.Dropdown(label="Select Object 1")
74
+ obj2 = gr.Dropdown(label="Select Object 2")
75
+ distance_btn = gr.Button("Calculate Distance")
76
+ distance_output = gr.Textbox(label="Result")
77
+ clear_btn = gr.Button("Clear")
78
+
79
+ input_img.change(fn=interface, inputs=input_img, outputs=[output_img, obj1, obj2])
80
+ distance_btn.click(fn=measure_distance, inputs=[obj1, obj2], outputs=distance_output)
81
+ clear_btn.click(lambda: [None, None, None, "", []], inputs=[], outputs=[input_img, output_img, distance_output, obj1, obj2])
82
+
83
+ demo.launch()
84