Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -53,12 +53,12 @@ def pretty_print_response(resp: dict):
|
|
| 53 |
def process_webcam_stream(image):
|
| 54 |
"""Send webcam image to HF MCP Server and get result"""
|
| 55 |
if image is None:
|
| 56 |
-
return
|
| 57 |
|
| 58 |
-
# Convert to base64
|
|
|
|
| 59 |
buffered = io.BytesIO()
|
| 60 |
-
|
| 61 |
-
img.save(buffered, format="JPEG")
|
| 62 |
b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 63 |
|
| 64 |
# Prepare payload
|
|
@@ -69,21 +69,23 @@ def process_webcam_stream(image):
|
|
| 69 |
"hf_token": HF_TOKEN
|
| 70 |
}
|
| 71 |
|
| 72 |
-
# Send to HF Space
|
| 73 |
client = Client(HF_SPACE)
|
| 74 |
try:
|
| 75 |
resp = client.predict(payload, api_name=API_NAME)
|
|
|
|
| 76 |
pretty_print_response(resp)
|
|
|
|
|
|
|
| 77 |
return (
|
| 78 |
resp.get("description", ""),
|
| 79 |
resp.get("human", ""),
|
| 80 |
-
", ".join(resp.get("objects", []))
|
| 81 |
resp.get("environment", "")
|
| 82 |
)
|
| 83 |
except Exception as e:
|
| 84 |
console.print(f"[bold red]Error sending to HF:[/bold red] {e}")
|
| 85 |
-
return
|
| 86 |
-
|
| 87 |
|
| 88 |
with gr.Blocks() as demo:
|
| 89 |
gr.Markdown("## 🎥 Robot Vision Webcam Stream")
|
|
|
|
| 53 |
def process_webcam_stream(image):
|
| 54 |
"""Send webcam image to HF MCP Server and get result"""
|
| 55 |
if image is None:
|
| 56 |
+
return None, None, None, None
|
| 57 |
|
| 58 |
+
# Convert PIL Image to base64
|
| 59 |
+
import io
|
| 60 |
buffered = io.BytesIO()
|
| 61 |
+
image.save(buffered, format="JPEG") # <--- directly save PIL Image
|
|
|
|
| 62 |
b64_img = base64.b64encode(buffered.getvalue()).decode("utf-8")
|
| 63 |
|
| 64 |
# Prepare payload
|
|
|
|
| 69 |
"hf_token": HF_TOKEN
|
| 70 |
}
|
| 71 |
|
| 72 |
+
# Send to HF Space using streaming-friendly predict
|
| 73 |
client = Client(HF_SPACE)
|
| 74 |
try:
|
| 75 |
resp = client.predict(payload, api_name=API_NAME)
|
| 76 |
+
# Print table in console
|
| 77 |
pretty_print_response(resp)
|
| 78 |
+
|
| 79 |
+
# Return selected fields for Gradio display
|
| 80 |
return (
|
| 81 |
resp.get("description", ""),
|
| 82 |
resp.get("human", ""),
|
| 83 |
+
", ".join(resp.get("objects", [])),
|
| 84 |
resp.get("environment", "")
|
| 85 |
)
|
| 86 |
except Exception as e:
|
| 87 |
console.print(f"[bold red]Error sending to HF:[/bold red] {e}")
|
| 88 |
+
return None, None, None, None
|
|
|
|
| 89 |
|
| 90 |
with gr.Blocks() as demo:
|
| 91 |
gr.Markdown("## 🎥 Robot Vision Webcam Stream")
|