-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_simple.py
More file actions
121 lines (92 loc) · 3.36 KB
/
Copy pathtest_simple.py
File metadata and controls
121 lines (92 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
"""
GhostBridge 简单测试示例
演示如何使用 GhostBridge 的感知层功能
"""
import sys
from pathlib import Path
from dotenv import load_dotenv
# 加载环境变量
env_path = Path(r"C:\Users\ljy33\Desktop\GhostBridge\.env")
load_dotenv(env_path)
# 添加 GhostBridge 到 Python 路径
ghostbridge_path = Path(r"C:\Users\ljy33\Desktop\GhostBridge\GhostBridge")
if str(ghostbridge_path) not in sys.path:
sys.path.insert(0, str(ghostbridge_path))
from ghostbridge.perception import add_tags_to_image, identify_element
from PIL import Image, ImageDraw
def create_test_ui_image():
"""创建一个模拟的 UI 界面截图"""
img = Image.new('RGB', (800, 600), color='white')
draw = ImageDraw.Draw(img)
# 绘制登录表单
draw.rectangle([250, 150, 550, 200], outline='gray', width=2)
draw.text((270, 170), "用户名", fill='black')
draw.rectangle([250, 210, 550, 260], outline='gray', width=2)
draw.text((270, 230), "密码", fill='black')
# 绘制登录按钮
draw.rectangle([250, 300, 400, 350], fill='blue', outline='blue')
draw.text((300, 320), "登录", fill='white')
# 绘制注册按钮
draw.rectangle([420, 300, 550, 350], outline='gray', width=2)
draw.text((440, 320), "注册", fill='black')
# 绘制标题
draw.text((350, 100), "欢迎登录", fill='black')
return img
def main():
print("=" * 60)
print("GhostBridge 简单测试")
print("=" * 60)
# 1. 创建测试图像
print("\n1. 创建测试 UI 图像...")
img = create_test_ui_image()
test_image_path = Path("test_ui.png")
img.save(test_image_path)
print(f" ✓ 已保存: {test_image_path}")
# 2. 添加数字标签
print("\n2. 为图像添加数字标签...")
tagged_image = add_tags_to_image(str(test_image_path))
print(f" ✓ 已保存: {tagged_image}")
# 3. 识别元素
print("\n3. 识别 UI 元素...")
test_cases = [
"登录按钮",
"注册按钮",
"用户名输入框",
"密码输入框"
]
for instruction in test_cases:
print(f"\n 识别: {instruction}")
result = identify_element(tagged_image, instruction)
tag_id = result['tag_id']
center = result['center']
print(f" → 标签 ID: {tag_id}")
print(f" → 中心坐标: {center}")
if center:
print(f" ✓ 识别成功!")
else:
print(f" ⚠ 未找到坐标")
# 4. 显示结果
print("\n" + "=" * 60)
print("测试完成!")
print("=" * 60)
print(f"\n生成的文件:")
print(f" - 原始图像: {test_image_path}")
print(f" - 标签图像: {tagged_image}")
print(f" - 元数据: {test_image_path.stem}_tags.json")
print(f"\n💡 提示: 你可以打开 {tagged_image} 查看添加的数字标签")
# 询问是否清理
print("\n是否删除测试文件?(y/n)")
# 自动清理
cleanup = True
if cleanup:
files_to_delete = [
test_image_path,
Path(tagged_image),
Path(f"{test_image_path.stem}_tags.json")
]
for file in files_to_delete:
if file.exists():
file.unlink()
print(f" ✓ 已删除: {file}")
if __name__ == "__main__":
main()