Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -6,6 +6,7 @@ import os
|
|
6 |
import torch
|
7 |
import utils
|
8 |
import plotly.graph_objects as go
|
|
|
9 |
|
10 |
from image_segmenter import ImageSegmenter
|
11 |
from monocular_depth_estimator import MonocularDepthEstimator
|
@@ -14,9 +15,11 @@ from point_cloud_generator import display_pcd
|
|
14 |
# params
|
15 |
CANCEL_PROCESSING = False
|
16 |
|
|
|
17 |
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
18 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
19 |
|
|
|
20 |
def process_image(image):
|
21 |
image = utils.resize(image)
|
22 |
image_segmentation, objects_data = img_seg.predict(image)
|
@@ -26,12 +29,14 @@ def process_image(image):
|
|
26 |
plot_fig = display_pcd(objs_pcd)
|
27 |
return image_segmentation, depth_colormap, dist_image, plot_fig
|
28 |
|
|
|
29 |
def test_process_img(image):
|
30 |
image = utils.resize(image)
|
31 |
image_segmentation, objects_data = img_seg.predict(image)
|
32 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
33 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
34 |
|
|
|
35 |
def process_video(vid_path=None):
|
36 |
vid_cap = cv2.VideoCapture(vid_path)
|
37 |
while vid_cap.isOpened():
|
@@ -54,8 +59,10 @@ def update_segmentation_options(options):
|
|
54 |
def update_confidence_threshold(thres_val):
|
55 |
img_seg.confidence_threshold = thres_val/100
|
56 |
|
|
|
57 |
def model_selector(model_type):
|
58 |
-
|
|
|
59 |
if "Small - Better performance and less accuracy" == model_type:
|
60 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
61 |
elif "Medium - Balanced performance and accuracy" == model_type:
|
@@ -69,30 +76,12 @@ def model_selector(model_type):
|
|
69 |
depth_estimator = MonocularDepthEstimator(model_type=midas_model)
|
70 |
|
71 |
def cancel():
|
|
|
72 |
CANCEL_PROCESSING = True
|
73 |
|
74 |
if __name__ == "__main__":
|
75 |
-
|
76 |
-
# testing
|
77 |
-
# img_1 = cv2.imread("assets/images/bus.jpg")
|
78 |
-
# img_1 = utils.resize(img_1)
|
79 |
-
|
80 |
-
# image_segmentation, objects_data, depthmap, depth_colormap = test_process_img(img_1)
|
81 |
-
# final_image = utils.draw_depth_info(image_segmentation, depthmap, objects_data)
|
82 |
-
# objs_pcd = utils.generate_obj_pcd(depthmap, objects_data)
|
83 |
-
# # print(objs_pcd[0][0])
|
84 |
-
# display_pcd(objs_pcd, use_matplotlib=True)
|
85 |
-
|
86 |
-
# cv2.imshow("Segmentation", image_segmentation)
|
87 |
-
# cv2.imshow("Depth", depthmap*objects_data[2][3])
|
88 |
-
# cv2.imshow("Final", final_image)
|
89 |
-
|
90 |
-
# cv2.waitKey(0)
|
91 |
-
# cv2.destroyAllWindows()
|
92 |
-
|
93 |
# gradio gui app
|
94 |
with gr.Blocks() as my_app:
|
95 |
-
|
96 |
# title
|
97 |
gr.Markdown("<h1><center>Simultaneous Segmentation and Depth Estimation</center></h1>")
|
98 |
gr.Markdown("<h3><center>Created by Vaishanth</center></h3>")
|
@@ -167,10 +156,7 @@ if __name__ == "__main__":
|
|
167 |
os.path.join(os.path.dirname(__file__), "assets/videos/overpass.mp4"),
|
168 |
os.path.join(os.path.dirname(__file__), "assets/videos/walking.mp4")],
|
169 |
inputs=vid_input,
|
170 |
-
# outputs=vid_output,
|
171 |
-
# fn=vid_segmenation,
|
172 |
)
|
173 |
-
|
174 |
|
175 |
# image tab logic
|
176 |
submit_btn_img.click(process_image, inputs=img_input, outputs=[segmentation_img_output, depth_img_output, dist_img_output, pcd_img_output])
|
@@ -185,5 +171,5 @@ if __name__ == "__main__":
|
|
185 |
options_checkbox_vid.change(update_segmentation_options, options_checkbox_vid, [])
|
186 |
conf_thres_vid.change(update_confidence_threshold, conf_thres_vid, [])
|
187 |
|
188 |
-
|
189 |
-
my_app.queue(concurrency_count=
|
|
|
6 |
import torch
|
7 |
import utils
|
8 |
import plotly.graph_objects as go
|
9 |
+
import spaces
|
10 |
|
11 |
from image_segmenter import ImageSegmenter
|
12 |
from monocular_depth_estimator import MonocularDepthEstimator
|
|
|
15 |
# params
|
16 |
CANCEL_PROCESSING = False
|
17 |
|
18 |
+
# Initialize models (but actual loading happens in decorated functions)
|
19 |
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
20 |
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
21 |
|
22 |
+
@spaces.GPU(duration=30) # Adjust duration based on your needs
|
23 |
def process_image(image):
|
24 |
image = utils.resize(image)
|
25 |
image_segmentation, objects_data = img_seg.predict(image)
|
|
|
29 |
plot_fig = display_pcd(objs_pcd)
|
30 |
return image_segmentation, depth_colormap, dist_image, plot_fig
|
31 |
|
32 |
+
@spaces.GPU(duration=30)
|
33 |
def test_process_img(image):
|
34 |
image = utils.resize(image)
|
35 |
image_segmentation, objects_data = img_seg.predict(image)
|
36 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
37 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
38 |
|
39 |
+
@spaces.GPU(duration=60) # Longer duration for video processing
|
40 |
def process_video(vid_path=None):
|
41 |
vid_cap = cv2.VideoCapture(vid_path)
|
42 |
while vid_cap.isOpened():
|
|
|
59 |
def update_confidence_threshold(thres_val):
|
60 |
img_seg.confidence_threshold = thres_val/100
|
61 |
|
62 |
+
@spaces.GPU(duration=10) # Short duration for model loading
|
63 |
def model_selector(model_type):
|
64 |
+
global img_seg, depth_estimator
|
65 |
+
|
66 |
if "Small - Better performance and less accuracy" == model_type:
|
67 |
midas_model, yolo_model = "midas_v21_small_256", "yolov8s-seg"
|
68 |
elif "Medium - Balanced performance and accuracy" == model_type:
|
|
|
76 |
depth_estimator = MonocularDepthEstimator(model_type=midas_model)
|
77 |
|
78 |
def cancel():
|
79 |
+
global CANCEL_PROCESSING
|
80 |
CANCEL_PROCESSING = True
|
81 |
|
82 |
if __name__ == "__main__":
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
83 |
# gradio gui app
|
84 |
with gr.Blocks() as my_app:
|
|
|
85 |
# title
|
86 |
gr.Markdown("<h1><center>Simultaneous Segmentation and Depth Estimation</center></h1>")
|
87 |
gr.Markdown("<h3><center>Created by Vaishanth</center></h3>")
|
|
|
156 |
os.path.join(os.path.dirname(__file__), "assets/videos/overpass.mp4"),
|
157 |
os.path.join(os.path.dirname(__file__), "assets/videos/walking.mp4")],
|
158 |
inputs=vid_input,
|
|
|
|
|
159 |
)
|
|
|
160 |
|
161 |
# image tab logic
|
162 |
submit_btn_img.click(process_image, inputs=img_input, outputs=[segmentation_img_output, depth_img_output, dist_img_output, pcd_img_output])
|
|
|
171 |
options_checkbox_vid.change(update_segmentation_options, options_checkbox_vid, [])
|
172 |
conf_thres_vid.change(update_confidence_threshold, conf_thres_vid, [])
|
173 |
|
174 |
+
# Launch with appropriate queue settings for ZeroGPU
|
175 |
+
my_app.queue(concurrency_count=1, max_size=10).launch()
|