File size: 8,533 Bytes
402fd16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
#!/usr/bin/env python3
"""
测试可视化图像API功能
"""

import requests
import base64
import json
import cv2
import numpy as np
import time
import os

def load_test_image():
    """加载测试图像"""
    # 尝试查找测试图像
    test_images = ["test_mouse.jpg", "test_mouse.png", "sample.jpg"]
    
    for img_path in test_images:
        if os.path.exists(img_path):
            print(f"✅ 找到测试图像: {img_path}")
            return cv2.imread(img_path)
    
    # 如果没有找到测试图像,创建一个简单的
    print("📸 创建合成测试图像...")
    test_img = np.random.randint(50, 200, (640, 640, 3), dtype=np.uint8)
    
    # 添加简单的小鼠形状
    cv2.circle(test_img, (320, 300), 50, (100, 100, 100), -1)  # 头部
    cv2.ellipse(test_img, (320, 380), (80, 120), 0, 0, 360, (120, 120, 120), -1)  # 身体
    cv2.ellipse(test_img, (320, 500), (20, 80), 0, 0, 360, (90, 90, 90), -1)  # 尾巴
    
    return test_img

def image_to_base64(image):
    """将OpenCV图像转换为Base64字符串"""
    _, buffer = cv2.imencode('.jpg', image)
    return base64.b64encode(buffer).decode('utf-8')

def base64_to_image(base64_str):
    """将Base64字符串转换为OpenCV图像"""
    img_bytes = base64.b64decode(base64_str)
    nparr = np.frombuffer(img_bytes, np.uint8)
    return cv2.imdecode(nparr, cv2.IMREAD_COLOR)

def test_api_with_visualization():
    """测试带可视化功能的API"""
    print("=== 测试可视化图像API功能 ===")
    
    # 加载测试图像
    test_image = load_test_image()
    if test_image is None:
        print("❌ 无法加载测试图像")
        return False
    
    print(f"测试图像尺寸: {test_image.shape}")
    
    # 转换为Base64
    image_base64 = image_to_base64(test_image)
    
    # 测试不同的配置
    test_configs = [
        {
            "name": "不返回图像",
            "data": {
                "image": image_base64,
                "conf_threshold": 0.1,
                "return_image": False,
                "frame_id": 1,
                "timestamp": time.time()
            }
        },
        {
            "name": "返回图像(不包括边界框)",
            "data": {
                "image": image_base64,
                "conf_threshold": 0.1,
                "return_image": True,
                "draw_bbox": False,
                "frame_id": 2,
                "timestamp": time.time()
            }
        },
        {
            "name": "返回图像(包括边界框)",
            "data": {
                "image": image_base64,
                "conf_threshold": 0.1,
                "return_image": True,
                "draw_bbox": True,
                "frame_id": 3,
                "timestamp": time.time()
            }
        }
    ]
    
    api_url = "http://localhost:8765/api/process_frame"
    
    for i, config in enumerate(test_configs):
        print(f"\n--- 测试 {i+1}: {config['name']} ---")
        
        try:
            # 发送请求
            start_time = time.time()
            response = requests.post(api_url, json=config["data"])
            request_time = time.time() - start_time
            
            if response.status_code == 200:
                result = response.json()
                print(f"✅ 请求成功 (耗时: {request_time:.3f}s)")
                print(f"检测到小鼠: {result['mouse_detected']}")
                
                if result['mouse_detected']:
                    print(f"置信度: {result['confidence']:.3f}")
                    print(f"关键点数量: {len(result['keypoints'])}")
                    print(f"处理FPS: {result['fps']:.1f}")
                
                # 检查是否有可视化图像
                if "visualization_image" in result and result["visualization_image"]:
                    print("✅ 包含可视化图像")
                    
                    # 解码并保存可视化图像
                    vis_image = base64_to_image(result["visualization_image"])
                    output_filename = f"test_result_{i+1}_{config['name'].replace(' ', '_').replace('(', '').replace(')', '')}.jpg"
                    cv2.imwrite(output_filename, vis_image)
                    print(f"可视化图像已保存: {output_filename}")
                    
                else:
                    if "visualization_image" in result:
                        print(f"❌ visualization_image存在但为空: {result['visualization_image'] is None}")
                    else:
                        print("❌ 响应中没有visualization_image字段")
                    print(f"响应中的所有字段: {list(result.keys())}")
                    # 打印部分响应内容(不包括过长的字段)
                    debug_result = {k: v for k, v in result.items() if k != 'visualization_image'}
                    print(f"响应内容(除图像外): {json.dumps(debug_result, indent=2, ensure_ascii=False)}")
                
            else:
                print(f"❌ 请求失败: {response.status_code}")
                print(f"错误信息: {response.text}")
                
        except Exception as e:
            print(f"❌ 请求出错: {str(e)}")
    
    return True

def test_websocket_with_visualization():
    """测试WebSocket的可视化功能"""
    print("\n=== 测试WebSocket可视化功能 ===")
    
    try:
        import websockets
        import asyncio
        
        async def test_ws():
            # 加载测试图像
            test_image = load_test_image()
            image_base64 = image_to_base64(test_image)
            
            uri = "ws://localhost:8765/ws/stream"
            
            async with websockets.connect(uri) as websocket:
                print("✅ WebSocket连接成功")
                
                # 发送带可视化的数据
                frame_data = {
                    "image": image_base64,
                    "conf_threshold": 0.1,  # 降低置信度阈值
                    "return_image": True,
                    "draw_bbox": False,
                    "frame_id": 100,
                    "timestamp": time.time()
                }
                
                await websocket.send(json.dumps(frame_data))
                
                # 接收结果
                response = await websocket.recv()
                result = json.loads(response)
                
                if result['success']:
                    print(f"✅ WebSocket处理成功")
                    print(f"检测到小鼠: {result['mouse_detected']}")
                    
                    if "visualization_image" in result and result["visualization_image"]:
                        print("✅ 包含可视化图像")
                        
                        # 保存WebSocket结果
                        vis_image = base64_to_image(result["visualization_image"])
                        cv2.imwrite("websocket_result.jpg", vis_image)
                        print("WebSocket可视化图像已保存: websocket_result.jpg")
                    else:
                        print("❌ WebSocket结果不包含可视化图像")
                else:
                    print(f"❌ WebSocket处理失败: {result.get('error', 'Unknown error')}")
        
        # 运行异步测试
        asyncio.run(test_ws())
        return True
        
    except ImportError:
        print("❌ 未安装websockets库,跳过WebSocket测试")
        return False
    except Exception as e:
        print(f"❌ WebSocket测试出错: {str(e)}")
        return False

def main():
    """主函数"""
    print("🚀 开始测试可视化图像API功能")
    print("确保WebRTC服务已启动在 http://localhost:8765")
    
    # 检查服务是否可用
    try:
        response = requests.get("http://localhost:8765/api/status", timeout=5)
        if response.status_code == 200:
            print("✅ WebRTC服务可用")
        else:
            print("❌ WebRTC服务不可用")
            return
    except Exception as e:
        print(f"❌ 无法连接到WebRTC服务: {str(e)}")
        print("请先启动服务: python gradio_webrtc_api.py")
        return
    
    # 测试REST API
    test_api_with_visualization()
    
    # 测试WebSocket
    test_websocket_with_visualization()
    
    print("\n🎉 测试完成!检查生成的图像文件查看结果。")

if __name__ == "__main__":
    main()