Skip to content

Commit 41777fd

Browse files
Update depthmap_api.py
Add video depth generation endpoint This commit adds a new API endpoint for generating depth maps from input images in a video format. The endpoint supports various depth model options, including different pre-trained models. It also validates and processes video parameters such as number of frames, frames per second, trajectory, shift, border, dolly, format, and super-sampling anti-aliasing. The commit includes error handling for missing input images, invalid model types, and required video parameters. Additionally, it checks if a mesh file already exists, and if not, it generates a new one. The generated mesh is then used to create a depth video based on the specified parameters.
1 parent e4282bc commit 41777fd

File tree

1 file changed

+98
-3
lines changed

1 file changed

+98
-3
lines changed

scripts/depthmap_api.py

Lines changed: 98 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,11 +70,106 @@ async def process(
7070
if not isinstance(result, Image.Image):
7171
continue
7272
results_based += [encode_to_base64(result)]
73-
74-
if type == 'video':
75-
return {"info": "Success"}
7673

7774
return {"images": results_based, "info": "Success"}
75+
76+
@app.post("/depth/generate/video")
77+
async def process_video(
78+
depth_input_images: List[str] = Body([], title='Input Images'),
79+
options: Dict[str, object] = Body("options", title='Generation options'),
80+
):
81+
if len(depth_input_images) == 0:
82+
raise HTTPException(status_code=422, detail="No images supplied")
83+
print(f"Processing {str(len(depth_input_images))} images trough the API")
84+
85+
available_models = {
86+
'res101': 0,
87+
'dpt_beit_large_512': 1, #midas 3.1
88+
'dpt_beit_large_384': 2, #midas 3.1
89+
'dpt_large_384': 3, #midas 3.0
90+
'dpt_hybrid_384': 4, #midas 3.0
91+
'midas_v21': 5,
92+
'midas_v21_small': 6,
93+
'zoedepth_n': 7, #indoor
94+
'zoedepth_k': 8, #outdoor
95+
'zoedepth_nk': 9,
96+
}
97+
98+
model_type = options["model_type"]
99+
100+
model_id = None
101+
if isinstance(model_type, str):
102+
# Check if the string is in the available_models dictionary
103+
if model_type in available_models:
104+
model_id = available_models[model_type]
105+
else:
106+
available_strings = list(available_models.keys())
107+
raise HTTPException(status_code=400, detail={'error': 'Invalid model string', 'available_models': available_strings})
108+
elif isinstance(model_type, int):
109+
model_id = model_type
110+
else:
111+
raise HTTPException(status_code=400, detail={'error': 'Invalid model parameter type'})
112+
113+
options["model_type"] = model_id
114+
115+
video_parameters = options["video_parameters"]
116+
117+
required_params = ["vid_numframes", "vid_fps", "vid_traj", "vid_shift", "vid_border", "dolly", "vid_format", "vid_ssaa", "output_filename"]
118+
119+
missing_params = [param for param in required_params if param not in video_parameters]
120+
121+
if missing_params:
122+
raise HTTPException(status_code=400, detail={'error': f"Missing required parameter(s): {', '.join(missing_params)}"})
123+
124+
vid_numframes = video_parameters["vid_numframes"]
125+
vid_fps = video_parameters["vid_fps"]
126+
vid_traj = video_parameters["vid_traj"]
127+
vid_shift = video_parameters["vid_shift"]
128+
vid_border = video_parameters["vid_border"]
129+
dolly = video_parameters["dolly"]
130+
vid_format = video_parameters["vid_format"]
131+
vid_ssaa = int(video_parameters["vid_ssaa"])
132+
133+
output_filename = video_parameters["output_filename"]
134+
output_path = os.path.dirname(output_filename)
135+
basename, extension = os.path.splitext(os.path.basename(output_filename))
136+
137+
# Comparing video_format with the extension
138+
if vid_format != extension[1:]:
139+
raise HTTPException(status_code=400, detail={'error': f"Video format '{vid_format}' does not match with the extension '{extension}'."})
140+
141+
pil_images = []
142+
for input_image in depth_input_images:
143+
pil_images.append(to_base64_PIL(input_image))
144+
outpath = backbone.get_outpath()
145+
146+
mesh_fi_filename = video_parameters.get('mesh_fi_filename', None)
147+
148+
if mesh_fi_filename and os.path.exists(mesh_fi_filename):
149+
mesh_fi = mesh_fi_filename
150+
print("Loaded existing mesh from: ", mesh_fi)
151+
else:
152+
#If there is no mesh file generate it.
153+
options["GEN_INPAINTED_MESH"] = True
154+
155+
gen_obj = core_generation_funnel(outpath, pil_images, None, None, options)
156+
157+
mesh_fi = None
158+
for count, type, result in gen_obj:
159+
print("RESULTS")
160+
print("TYPE", type)
161+
if type == 'inpainted_mesh':
162+
mesh_fi = result
163+
break
164+
165+
if mesh_fi:
166+
print("Created mesh in: ", mesh_fi)
167+
else:
168+
raise HTTPException(status_code=400, detail={'error': "The mesh has not been created"})
169+
170+
run_makevideo(mesh_fi, vid_numframes, vid_fps, vid_traj, vid_shift, vid_border, dolly, vid_format, vid_ssaa, output_path, basename)
171+
172+
return {"info": "Success"}
78173

79174

80175
try:

0 commit comments

Comments
 (0)