-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_image_transfer.py
More file actions
135 lines (102 loc) · 4.58 KB
/
test_image_transfer.py
File metadata and controls
135 lines (102 loc) · 4.58 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
#!/usr/bin/env python3
"""
Bluetooth Image Transfer Test
=============================
Test script to demonstrate image transfer over Bluetooth mesh network.
Usage:
python3 test_image_transfer.py [image_path]
"""
import sys
import os
from bluetooth_image_protocol import BluetoothImageTransfer
def create_test_image():
"""Create a simple test image if PIL is available"""
try:
from PIL import Image, ImageDraw
# Create a simple 200x200 test image
img = Image.new('RGB', (200, 200), color='lightblue')
draw = ImageDraw.Draw(img)
# Draw some text
draw.text((50, 90), "Test Image", fill='black')
draw.rectangle([20, 20, 180, 180], outline='red', width=3)
test_path = "test_image.png"
img.save(test_path)
print(f"✅ Created test image: {test_path}")
return test_path
except ImportError:
print("❌ PIL not available, cannot create test image")
return None
def test_image_protocol():
"""Test the image transfer protocol"""
print("🧪 BLUETOOTH IMAGE TRANSFER TEST")
print("=" * 40)
# Get image path from command line or create test image
if len(sys.argv) > 1:
image_path = sys.argv[1]
else:
image_path = create_test_image()
if not image_path:
print("❌ No test image available. Please provide an image path.")
return
if not os.path.exists(image_path):
print(f"❌ Image not found: {image_path}")
return
print(f"📸 Testing with image: {image_path}")
print(f"📊 Image size: {os.path.getsize(image_path)} bytes")
# Test the protocol
sender_transfer = BluetoothImageTransfer(max_chunk_size=500) # Small chunks for testing
receiver_transfer = BluetoothImageTransfer(max_chunk_size=500)
sender_name = "TestSender"
receiver_name = "TestReceiver"
try:
# Prepare image for transfer
print(f"\n📤 Preparing image for transfer...")
message_chunks = sender_transfer.prepare_image_for_transfer(
image_path, sender_name, receiver_name
)
print(f"✅ Prepared {len(message_chunks)} message chunks")
# Simulate sending and receiving
print(f"\n📡 Simulating Bluetooth transfer...")
for i, chunk in enumerate(message_chunks):
print(f" 📦 Processing chunk {i+1}/{len(message_chunks)}")
# Simulate receiving the message
result = receiver_transfer.process_received_message(chunk)
if result and result.get('type') == 'image_complete':
print(f"\n✅ Image transfer complete!")
# Get the reconstructed image
filename, image_data = receiver_transfer.get_completed_image(result['msg_id'])
# Save reconstructed image
output_path = f"received_{filename}"
with open(output_path, 'wb') as f:
f.write(image_data)
print(f"💾 Saved received image as: {output_path}")
print(f"📊 Received image size: {len(image_data)} bytes")
# Verify integrity
original_size = os.path.getsize(image_path)
if len(image_data) == original_size:
print(f"✅ Image integrity verified (sizes match)")
else:
print(f"❌ Size mismatch: original={original_size}, received={len(image_data)}")
break
print(f"\n🎉 Test completed successfully!")
except Exception as e:
print(f"❌ Test failed: {e}")
import traceback
traceback.print_exc()
def test_chunking_efficiency():
"""Test different chunk sizes to find optimal settings"""
print("\n🔬 CHUNKING EFFICIENCY TEST")
print("=" * 30)
# Create a test string to simulate image data
test_data = "A" * 10000 # 10KB of data
chunk_sizes = [100, 500, 800, 1000, 1500]
for size in chunk_sizes:
chunks = []
for i in range(0, len(test_data), size):
chunks.append(test_data[i:i+size])
overhead = len(chunks) * 50 # Assume 50 bytes overhead per message
efficiency = (len(test_data) / (len(test_data) + overhead)) * 100
print(f" Chunk size {size:4d}: {len(chunks):3d} chunks, {efficiency:.1f}% efficiency")
if __name__ == "__main__":
test_image_protocol()
test_chunking_efficiency()