Alessio Grancini
commited on
Update app.py
Browse files
app.py
CHANGED
@@ -1,9 +1,7 @@
|
|
1 |
-
from ultralytics import YOLO
|
2 |
import cv2
|
3 |
import gradio as gr
|
4 |
import numpy as np
|
5 |
import os
|
6 |
-
import torch
|
7 |
import utils
|
8 |
import plotly.graph_objects as go
|
9 |
import spaces
|
@@ -15,24 +13,26 @@ from point_cloud_generator import display_pcd
|
|
15 |
# params
|
16 |
CANCEL_PROCESSING = False
|
17 |
|
18 |
-
# Initialize
|
19 |
-
img_seg =
|
20 |
-
depth_estimator =
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
21 |
|
22 |
@spaces.GPU
|
23 |
def process_image(image):
|
24 |
try:
|
25 |
print("Starting image processing")
|
26 |
-
|
27 |
-
print("Image resized")
|
28 |
|
29 |
-
|
30 |
image_segmentation, objects_data = img_seg.predict(image)
|
31 |
-
print("Segmentation complete")
|
32 |
-
|
33 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
34 |
-
print("Depth estimation complete")
|
35 |
-
|
36 |
dist_image = utils.draw_depth_info(image, depthmap, objects_data)
|
37 |
objs_pcd = utils.generate_obj_pcd(depthmap, objects_data)
|
38 |
plot_fig = display_pcd(objs_pcd)
|
@@ -43,37 +43,47 @@ def process_image(image):
|
|
43 |
print(traceback.format_exc())
|
44 |
raise
|
45 |
|
46 |
-
@spaces.GPU
|
47 |
def test_process_img(image):
|
|
|
48 |
image = utils.resize(image)
|
49 |
image_segmentation, objects_data = img_seg.predict(image)
|
50 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
51 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
52 |
|
53 |
-
@spaces.GPU
|
54 |
def process_video(vid_path=None):
|
55 |
-
|
56 |
-
|
57 |
-
|
58 |
-
|
59 |
-
|
60 |
-
|
61 |
-
|
62 |
-
|
63 |
-
|
64 |
-
|
65 |
-
|
66 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
67 |
|
68 |
def update_segmentation_options(options):
|
|
|
69 |
img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False
|
70 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
71 |
img_seg.is_show_segmentation_boundary = True if 'Show Segmentation Boundary' in options else False
|
72 |
|
73 |
def update_confidence_threshold(thres_val):
|
|
|
74 |
img_seg.confidence_threshold = thres_val/100
|
75 |
|
76 |
-
@spaces.GPU
|
77 |
def model_selector(model_type):
|
78 |
global img_seg, depth_estimator
|
79 |
|
@@ -94,7 +104,6 @@ def cancel():
|
|
94 |
CANCEL_PROCESSING = True
|
95 |
|
96 |
if __name__ == "__main__":
|
97 |
-
# gradio gui app
|
98 |
with gr.Blocks() as my_app:
|
99 |
# title
|
100 |
gr.Markdown("<h1><center>Simultaneous Segmentation and Depth Estimation</center></h1>")
|
@@ -185,5 +194,4 @@ 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(max_size=20).launch()
|
|
|
|
|
1 |
import cv2
|
2 |
import gradio as gr
|
3 |
import numpy as np
|
4 |
import os
|
|
|
5 |
import utils
|
6 |
import plotly.graph_objects as go
|
7 |
import spaces
|
|
|
13 |
# params
|
14 |
CANCEL_PROCESSING = False
|
15 |
|
16 |
+
# Initialize classes without loading models
|
17 |
+
img_seg = None
|
18 |
+
depth_estimator = None
|
19 |
+
|
20 |
+
def initialize_models():
|
21 |
+
global img_seg, depth_estimator
|
22 |
+
if img_seg is None:
|
23 |
+
img_seg = ImageSegmenter(model_type="yolov8s-seg")
|
24 |
+
if depth_estimator is None:
|
25 |
+
depth_estimator = MonocularDepthEstimator(model_type="midas_v21_small_256")
|
26 |
|
27 |
@spaces.GPU
|
28 |
def process_image(image):
|
29 |
try:
|
30 |
print("Starting image processing")
|
31 |
+
initialize_models()
|
|
|
32 |
|
33 |
+
image = utils.resize(image)
|
34 |
image_segmentation, objects_data = img_seg.predict(image)
|
|
|
|
|
35 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
|
|
|
|
36 |
dist_image = utils.draw_depth_info(image, depthmap, objects_data)
|
37 |
objs_pcd = utils.generate_obj_pcd(depthmap, objects_data)
|
38 |
plot_fig = display_pcd(objs_pcd)
|
|
|
43 |
print(traceback.format_exc())
|
44 |
raise
|
45 |
|
46 |
+
@spaces.GPU
|
47 |
def test_process_img(image):
|
48 |
+
initialize_models()
|
49 |
image = utils.resize(image)
|
50 |
image_segmentation, objects_data = img_seg.predict(image)
|
51 |
depthmap, depth_colormap = depth_estimator.make_prediction(image)
|
52 |
return image_segmentation, objects_data, depthmap, depth_colormap
|
53 |
|
54 |
+
@spaces.GPU
|
55 |
def process_video(vid_path=None):
|
56 |
+
try:
|
57 |
+
initialize_models()
|
58 |
+
vid_cap = cv2.VideoCapture(vid_path)
|
59 |
+
while vid_cap.isOpened():
|
60 |
+
ret, frame = vid_cap.read()
|
61 |
+
if ret:
|
62 |
+
print("making predictions ....")
|
63 |
+
frame = utils.resize(frame)
|
64 |
+
image_segmentation, objects_data = img_seg.predict(frame)
|
65 |
+
depthmap, depth_colormap = depth_estimator.make_prediction(frame)
|
66 |
+
dist_image = utils.draw_depth_info(frame, depthmap, objects_data)
|
67 |
+
yield cv2.cvtColor(image_segmentation, cv2.COLOR_BGR2RGB), depth_colormap, cv2.cvtColor(dist_image, cv2.COLOR_BGR2RGB)
|
68 |
+
|
69 |
+
return None
|
70 |
+
except Exception as e:
|
71 |
+
print(f"Error in process_video: {str(e)}")
|
72 |
+
import traceback
|
73 |
+
print(traceback.format_exc())
|
74 |
+
raise
|
75 |
|
76 |
def update_segmentation_options(options):
|
77 |
+
initialize_models()
|
78 |
img_seg.is_show_bounding_boxes = True if 'Show Boundary Box' in options else False
|
79 |
img_seg.is_show_segmentation = True if 'Show Segmentation Region' in options else False
|
80 |
img_seg.is_show_segmentation_boundary = True if 'Show Segmentation Boundary' in options else False
|
81 |
|
82 |
def update_confidence_threshold(thres_val):
|
83 |
+
initialize_models()
|
84 |
img_seg.confidence_threshold = thres_val/100
|
85 |
|
86 |
+
@spaces.GPU
|
87 |
def model_selector(model_type):
|
88 |
global img_seg, depth_estimator
|
89 |
|
|
|
104 |
CANCEL_PROCESSING = True
|
105 |
|
106 |
if __name__ == "__main__":
|
|
|
107 |
with gr.Blocks() as my_app:
|
108 |
# title
|
109 |
gr.Markdown("<h1><center>Simultaneous Segmentation and Depth Estimation</center></h1>")
|
|
|
194 |
options_checkbox_vid.change(update_segmentation_options, options_checkbox_vid, [])
|
195 |
conf_thres_vid.change(update_confidence_threshold, conf_thres_vid, [])
|
196 |
|
197 |
+
my_app.queue(max_size=10).launch()
|
|