Fertroll10 commited on
Commit
2b6a34a
·
verified ·
1 Parent(s): 3294f60

Upload folder using huggingface_hub

Browse files
Files changed (4) hide show
  1. README.md +18 -7
  2. __pycache__/app.cpython-311.pyc +0 -0
  3. app.py +139 -0
  4. requirements.txt +4 -0
README.md CHANGED
@@ -1,13 +1,24 @@
1
  ---
2
- title: Cyanbridge Walking Aid
3
- emoji: 🌖
4
- colorFrom: pink
5
- colorTo: gray
6
  sdk: gradio
7
  sdk_version: 6.20.0
8
- python_version: '3.12'
9
  app_file: app.py
10
- pinned: false
 
 
11
  ---
12
 
13
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
 
 
 
 
 
 
 
 
 
 
1
  ---
2
+ title: CyanBridge Walking Aid
3
+ emoji: "\U0001F9ED"
4
+ colorFrom: blue
5
+ colorTo: indigo
6
  sdk: gradio
7
  sdk_version: 6.20.0
 
8
  app_file: app.py
9
+ short_description: YOLO and depth guidance for walking aid research
10
+ python_version: "3.12"
11
+ startup_duration_timeout: 30m
12
  ---
13
 
14
+ # CyanBridge Walking Aid
15
+
16
+ An experimental accessibility-assistance demo that combines local inference models:
17
+
18
+ - YOLO11n for COCO object detection.
19
+ - YOLO-World for user-provided open-vocabulary object prompts.
20
+ - Depth Anything V2 Small for relative-depth guidance.
21
+
22
+ The output is advisory only. It is not a replacement for a cane, guide dog, mobility training, awareness, or professional advice.
23
+
24
+ The `/api/analyze` endpoint is reserved for the CyanBridge Pro relay. It requires its server-side `X-CyanBridge-Key` header and is never called with a client-side secret.
__pycache__/app.cpython-311.pyc ADDED
Binary file (11.9 kB). View file
 
app.py ADDED
@@ -0,0 +1,139 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import base64
2
+ import io
3
+ import os
4
+ from typing import Any
5
+
6
+ import gradio as gr
7
+ import spaces
8
+ import torch
9
+ from fastapi import Header, HTTPException
10
+ from huggingface_hub import hf_hub_download
11
+ from PIL import Image, ImageDraw
12
+ from transformers import AutoImageProcessor, AutoModelForDepthEstimation
13
+ from ultralytics import YOLO
14
+
15
+ YOLO11_PATH = hf_hub_download("Ultralytics/YOLO11", "yolo11n.pt")
16
+ YOLO_WORLD_PATH = hf_hub_download("Bingsu/yolo-world-mirror", "yolov8s-worldv2.pt")
17
+ DEPTH_MODEL_ID = "depth-anything/Depth-Anything-V2-Small-hf"
18
+
19
+ YOLO11 = YOLO(YOLO11_PATH)
20
+ YOLO_WORLD = YOLO(YOLO_WORLD_PATH)
21
+ DEPTH_PROCESSOR = AutoImageProcessor.from_pretrained(DEPTH_MODEL_ID)
22
+ DEPTH_MODEL = AutoModelForDepthEstimation.from_pretrained(DEPTH_MODEL_ID)
23
+ SPACE_KEY = os.environ.get("CYANBRIDGE_SPACE_KEY", "")
24
+
25
+
26
+ def _device() -> str:
27
+ return "cuda" if torch.cuda.is_available() else "cpu"
28
+
29
+
30
+ def _depth_summary(image: Image.Image) -> tuple[Image.Image, dict[str, Any]]:
31
+ device = _device()
32
+ DEPTH_MODEL.to(device)
33
+ inputs = DEPTH_PROCESSOR(images=image, return_tensors="pt").to(device)
34
+ with torch.inference_mode():
35
+ predicted_depth = DEPTH_MODEL(**inputs).predicted_depth
36
+ depth = torch.nn.functional.interpolate(
37
+ predicted_depth.unsqueeze(1),
38
+ size=(image.height, image.width),
39
+ mode="bicubic",
40
+ align_corners=False,
41
+ ).squeeze().float().cpu()
42
+ normalized = (depth - depth.min()) / (depth.max() - depth.min() + 1e-6)
43
+ depth_image = Image.fromarray((normalized.numpy() * 255).astype("uint8"))
44
+ lower_center = normalized[int(image.height * 0.6):, int(image.width * 0.33):int(image.width * 0.67)]
45
+ return depth_image, {
46
+ "relative_depth": "Higher values indicate relatively closer surfaces.",
47
+ "center_ground_mean": round(float(lower_center.mean()), 3),
48
+ "center_ground_range": round(float(lower_center.max() - lower_center.min()), 3),
49
+ }
50
+
51
+
52
+ def _detections(image: Image.Image, detector: str, classes: str) -> tuple[Image.Image, list[dict[str, Any]]]:
53
+ model = YOLO_WORLD if detector == "YOLO-World" else YOLO11
54
+ if detector == "YOLO-World":
55
+ prompts = [item.strip() for item in classes.split(",") if item.strip()]
56
+ if prompts:
57
+ model.set_classes(prompts)
58
+ results = model.predict(image, device=_device(), verbose=False, conf=0.35)
59
+ result = results[0]
60
+ annotated = Image.fromarray(result.plot()[..., ::-1])
61
+ names = result.names
62
+ output: list[dict[str, Any]] = []
63
+ for box in result.boxes:
64
+ class_id = int(box.cls.item())
65
+ output.append(
66
+ {
67
+ "label": str(names[class_id]),
68
+ "confidence": round(float(box.conf.item()), 3),
69
+ "box": [round(float(value), 1) for value in box.xyxy[0].tolist()],
70
+ }
71
+ )
72
+ return annotated, output
73
+
74
+
75
+ @spaces.GPU(duration=60)
76
+ def analyze_image(image: Image.Image, detector: str, classes: str) -> tuple[Image.Image, Image.Image, dict[str, Any]]:
77
+ """Detect objects and estimate relative depth for an uploaded walking-scene image."""
78
+ if image is None:
79
+ raise gr.Error("Upload an image first.")
80
+ image = image.convert("RGB")
81
+ detected_image, detected_objects = _detections(image, detector, classes)
82
+ depth_image, depth = _depth_summary(image)
83
+ return detected_image, depth_image, {"detector": detector, "objects": detected_objects, "depth": depth}
84
+
85
+
86
+ def _authorized(key: str | None) -> None:
87
+ if not SPACE_KEY or key != SPACE_KEY:
88
+ raise HTTPException(status_code=401, detail="relay authorization required")
89
+
90
+
91
+ @spaces.GPU(duration=60)
92
+ def analyze_relay_image(image: Image.Image, detector: str, classes: str) -> dict[str, Any]:
93
+ """Run the model stack for an authorized CyanBridge Pro relay request."""
94
+ _, detected_objects = _detections(image.convert("RGB"), detector, classes)
95
+ _, depth = _depth_summary(image.convert("RGB"))
96
+ return {"detector": detector, "objects": detected_objects, "depth": depth}
97
+
98
+
99
+ with gr.Blocks(title="CyanBridge Walking Aid") as demo:
100
+ gr.Markdown(
101
+ "# CyanBridge Walking Aid\n"
102
+ "Experimental object detection and relative-depth guidance. Do not rely on this demo as your sole mobility aid."
103
+ )
104
+ with gr.Row():
105
+ image_input = gr.Image(type="pil", label="Walking scene")
106
+ with gr.Column():
107
+ detector_input = gr.Radio(["YOLO11n", "YOLO-World"], value="YOLO11n", label="Detector")
108
+ classes_input = gr.Textbox(value="person, bicycle, curb, stairs, pothole", label="YOLO-World classes")
109
+ run_button = gr.Button("Analyze scene", variant="primary")
110
+ with gr.Row():
111
+ detection_output = gr.Image(label="Object detection")
112
+ depth_output = gr.Image(label="Relative depth")
113
+ summary_output = gr.JSON(label="Advisory result")
114
+ run_button.click(
115
+ analyze_image,
116
+ inputs=[image_input, detector_input, classes_input],
117
+ outputs=[detection_output, depth_output, summary_output],
118
+ api_name="analyze",
119
+ )
120
+
121
+
122
+ @demo.app.post("/api/analyze")
123
+ def analyze_relay(payload: dict[str, Any], x_cyanbridge_key: str | None = Header(default=None)) -> dict[str, Any]:
124
+ _authorized(x_cyanbridge_key)
125
+ encoded = str(payload.get("image_base64", ""))
126
+ if not encoded:
127
+ raise HTTPException(status_code=400, detail="image_base64 is required")
128
+ try:
129
+ image = Image.open(io.BytesIO(base64.b64decode(encoded))).convert("RGB")
130
+ except Exception as error:
131
+ raise HTTPException(status_code=400, detail="invalid image payload") from error
132
+ detector = str(payload.get("detector", "YOLO11n"))
133
+ if detector not in {"YOLO11n", "YOLO-World"}:
134
+ raise HTTPException(status_code=400, detail="unsupported detector")
135
+ classes = str(payload.get("classes", ""))
136
+ return analyze_relay_image(image, detector, classes)
137
+
138
+
139
+ demo.launch(mcp_server=True)
requirements.txt ADDED
@@ -0,0 +1,4 @@
 
 
 
 
 
1
+ ultralytics>=8.3.0
2
+ transformers>=4.48.0
3
+ torchvision
4
+ pillow