Spaces:
Paused
Paused
add red point to scene image
Browse files- app.py +16 -13
- requirements.txt +3 -1
app.py
CHANGED
|
@@ -4,6 +4,7 @@ import requests
|
|
| 4 |
import os
|
| 5 |
from typing import Optional
|
| 6 |
import numpy as np
|
|
|
|
| 7 |
|
| 8 |
# 后端API配置(可配置化)
|
| 9 |
BACKEND_URL = os.getenv("BACKEND_URL", "http://your-backend-server:5000")
|
|
@@ -13,6 +14,9 @@ API_ENDPOINTS = {
|
|
| 13 |
"get_result": f"{BACKEND_URL}/api/v1/result"
|
| 14 |
}
|
| 15 |
|
|
|
|
|
|
|
|
|
|
| 16 |
# 模拟场景配置
|
| 17 |
SCENE_CONFIGS = {
|
| 18 |
"default_desk": {
|
|
@@ -39,23 +43,22 @@ MODEL_CHOICES = [
|
|
| 39 |
"AcT+CLIP"
|
| 40 |
]
|
| 41 |
|
| 42 |
-
def image_to_position(image: np.ndarray, evt: gr.SelectData) -> str:
|
| 43 |
-
"""
|
| 44 |
-
根据点击图像的位置(像素坐标)转换为仿真空间中的起始位置坐标
|
| 45 |
-
- image: 被点击图像的像素数据
|
| 46 |
-
- evt: 包含用户点击的位置 evt.index = (x, y)
|
| 47 |
-
|
| 48 |
-
这里假设图像宽高映射到 -1 到 1 的世界坐标范围
|
| 49 |
-
"""
|
| 50 |
h, w = image.shape[:2]
|
| 51 |
px, py = evt.index # 点击位置 (x, y)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
x = (px / w) * 2 - 1
|
| 55 |
-
y = -((py / h) * 2 - 1)
|
| 56 |
-
z = 0.1
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
-
return f"{x:.2f}, {y:.2f}, {z:.2f}"
|
| 59 |
|
| 60 |
def submit_to_backend(
|
| 61 |
scene: str,
|
|
@@ -301,7 +304,7 @@ with gr.Blocks(title="机器人导航仿真系统", css=custom_css) as demo:
|
|
| 301 |
scene_image.select(
|
| 302 |
fn=image_to_position,
|
| 303 |
inputs=[scene_image],
|
| 304 |
-
outputs=start_pos_input
|
| 305 |
)
|
| 306 |
|
| 307 |
|
|
|
|
| 4 |
import os
|
| 5 |
from typing import Optional
|
| 6 |
import numpy as np
|
| 7 |
+
import cv2
|
| 8 |
|
| 9 |
# 后端API配置(可配置化)
|
| 10 |
BACKEND_URL = os.getenv("BACKEND_URL", "http://your-backend-server:5000")
|
|
|
|
| 14 |
"get_result": f"{BACKEND_URL}/api/v1/result"
|
| 15 |
}
|
| 16 |
|
| 17 |
+
# 全局缓存原始图像
|
| 18 |
+
ORIGINAL_IMAGE = cv2.imread("scene.png")
|
| 19 |
+
|
| 20 |
# 模拟场景配置
|
| 21 |
SCENE_CONFIGS = {
|
| 22 |
"default_desk": {
|
|
|
|
| 43 |
"AcT+CLIP"
|
| 44 |
]
|
| 45 |
|
| 46 |
+
def image_to_position(image: np.ndarray, evt: gr.SelectData) -> tuple[np.ndarray, str]:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 47 |
h, w = image.shape[:2]
|
| 48 |
px, py = evt.index # 点击位置 (x, y)
|
| 49 |
|
| 50 |
+
# 坐标转换
|
| 51 |
x = (px / w) * 2 - 1
|
| 52 |
+
y = -((py / h) * 2 - 1)
|
| 53 |
+
z = 0.1
|
| 54 |
+
coord_str = f"{x:.2f}, {y:.2f}, {z:.2f}"
|
| 55 |
+
|
| 56 |
+
# 使用原始图像绘制新图(保证每次只有一个点)
|
| 57 |
+
marked = ORIGINAL_IMAGE.copy()
|
| 58 |
+
cv2.circle(marked, center=(px, py), radius=8, color=(255, 0, 0), thickness=-1)
|
| 59 |
+
|
| 60 |
+
return marked, coord_str
|
| 61 |
|
|
|
|
| 62 |
|
| 63 |
def submit_to_backend(
|
| 64 |
scene: str,
|
|
|
|
| 304 |
scene_image.select(
|
| 305 |
fn=image_to_position,
|
| 306 |
inputs=[scene_image],
|
| 307 |
+
outputs=[scene_image, start_pos_input]
|
| 308 |
)
|
| 309 |
|
| 310 |
|
requirements.txt
CHANGED
|
@@ -1,2 +1,4 @@
|
|
| 1 |
requests
|
| 2 |
-
typing-extensions
|
|
|
|
|
|
|
|
|
| 1 |
requests
|
| 2 |
+
typing-extensions
|
| 3 |
+
opencv-python
|
| 4 |
+
numpy
|