33import os
44import base64
55import io
6+ import time
67from typing import Dict , List , Optional , Tuple , Union , cast
78from pathlib import Path
89
@@ -186,6 +187,7 @@ def __init__(
186187 self .verbose = verbose
187188 self .save_visualizations = save_visualizations
188189 self .visualization_dir = visualization_dir
190+ Path (self .visualization_dir ).mkdir (parents = True , exist_ok = True )
189191
190192 if self .verbose :
191193 console .rule ("[bold blue]Device Configuration" )
@@ -266,7 +268,7 @@ async def predict(
266268
267269 # Parse response
268270 if detection :
269- result = parse_gemini_detection_response (response , ( width , height ) )
271+ result = parse_gemini_detection_response (response )
270272 else :
271273 result = parse_gemini_point_response (response , (width , height ))
272274
@@ -311,7 +313,25 @@ async def predict(
311313 "image_height" : height ,
312314 "image" : image_b64
313315 }
314- results .append (formatted_result )
316+ results .append (formatted_result )
317+
318+ if self .save_visualizations :
319+ for resp in results :
320+ try :
321+ image = decode_image (resp ["image" ])
322+ if resp ["points" ]:
323+ image = draw_points (image , resp ["points" ])
324+ if resp ["detections" ]:
325+ image = draw_boxes (image , {"detections" : resp ["detections" ]})
326+ ts = time .strftime ("%Y%m%d-%H%M%S" )
327+ filename = os .path .join (
328+ self .visualization_dir ,
329+ f"server_pointing_{ ts } _{ resp ['prompt' ].replace (' ' , '_' )} _{ resp ['image_index' ]} .png" ,
330+ )
331+ image .save (filename )
332+ console .print (f"Saved visualization to { filename } " , style = "yellow" )
333+ except Exception as exc :
334+ console .print (f"[red]Failed to save visualization: { exc } [/red]" )
315335
316336 return results
317337
@@ -426,4 +446,4 @@ def cli(
426446 """Start the PointingGeminiSAM2 service"""
427447 start_server (host , port , use_gpu , save_visualizations )
428448
429- typer .run (cli )
449+ typer .run (cli )
0 commit comments