-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest.image_processing.py
More file actions
234 lines (210 loc) · 8.29 KB
/
test.image_processing.py
File metadata and controls
234 lines (210 loc) · 8.29 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
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
from fastapi.testclient import TestClient
from main import app
from typing import Dict, Any
client = TestClient(app)
# filename -> expected toothpaste_id (0 means "not in database")
test_1_initial = {
"apteq_0.png": 3,
"apteq_1.jpeg": 3,
"apteq_2.jpeg": 3,
"apteq_3.jpeg": 3,
"apteq_4.jpeg": 3,
"apteq_5.jpeg": 3,
"apteq_6.jpeg": 3,
"apteq_7.jpeg": 3,
"apteq_8.jpg": 3,
"apteq_9.jpg": 3,
"be_confident.jpg": 0,
"biotene.png": 0, # Not in existing set
"colgate_0.png": 2,
"drjen_0.png": 0, # Not in existing set
"drjen_1.webp": 0, # Not in existing set
"elmex_0.png": 4,
"elmex_1.jpg": 4,
"gum_bio.webp": 0,
"oxygenol_0.jpg": 0,
"pronamel_0.png": 0, # Not in existing set (Sensodyne/Pronamel is 5)
"pronamel_1.png": 0, # Not in existing set
"pronamel_2.png": 0, # Not in existing set
"pronamel_3.png": 0, # Not in existing set
"pronamel_4.png": 0, # Not in existing set
"pronamel_5.png": 0, # Not in existing set
"yotuel_0.jpg": 0, # Not in existing set
"yotuel_1.webp": 0, # Not in existing set
}
test_2_second = {
"apteq_0.png": 3,
"apteq_1.jpeg": 3,
"apteq_2.jpeg": 3,
"apteq_3.jpeg": 3,
"apteq_4.jpeg": 3,
"apteq_5.jpeg": 3,
"apteq_6.jpeg": 3,
"apteq_7.jpeg": 3,
"apteq_8.jpg": 3,
"apteq_9.jpg": 3,
"be_confident.jpg": 0,
"biotene.png": 6, # Now in existing set
"colgate_0.png": 2,
"drjen_0.png": 0, # Not in existing set
"drjen_1.webp": 0, # Not in existing set
"elmex_0.png": 4,
"elmex_1.jpg": 4,
"gum_bio.webp": 0,
"oxygenol_0.jpg": 0,
"pronamel_0.png": 5, # Now in existing set (Sensodyne/Pronamel is 5)
"pronamel_1.png": 5,
"pronamel_2.png": 5,
"pronamel_3.png": 5,
"pronamel_4.png": 5,
"pronamel_5.png": 5,
"yotuel_0.jpg": 0, # Not in existing set
"yotuel_1.webp": 0, # Not in existing set
}
test_3_third = {
"apteq_0.png": 3,
"apteq_1.jpeg": 3,
"apteq_2.jpeg": 3,
"apteq_3.jpeg": 3,
"apteq_4.jpeg": 3,
"apteq_5.jpeg": 3,
"apteq_6.jpeg": 3,
"apteq_7.jpeg": 3,
"apteq_8.jpg": 3,
"apteq_9.jpg": 3,
"be_confident.jpg": 0,
"biotene.png": 6,
"colgate_0.png": 2,
"drjen_0.png": 7, # Now in existing set
"drjen_1.webp": 7, # Now in existing set
"elmex_0.png": 4,
"elmex_1.jpg": 4,
"gum_bio.webp": 0,
"oxygenol_0.jpg": 0,
"pronamel_0.png": 5,
"pronamel_1.png": 5,
"pronamel_2.png": 5,
"pronamel_3.png": 5,
"pronamel_4.png": 5,
"pronamel_5.png": 5,
"yotuel_0.jpg": 8, # Now in existing set
"yotuel_1.webp": 8, # Now in existing set
}
images_path = "./toothpastes/"
results: Dict[str, Any] = {
"matches_existing": {"count": 0, "total_similarity": 0.0},
"misses_false_positive": {"count": 0, "total_similarity": 0.0},
"misses_wrong_brand": {"count": 0, "total_similarity": 0.0},
"matches_not_existing": 0,
"misses_false_negative": 0,
"errors": 0,
"total": 0,
}
def get_mime_type(filename: str) -> str:
"""Determine MIME type based on file extension."""
ext = filename.lower().split('.')[-1]
mime_types = {
'jpg': 'image/jpeg',
'jpeg': 'image/jpeg',
'png': 'image/png',
'webp': 'image/webp',
}
return mime_types.get(ext, 'image/jpeg')
def test_image_processing(test_suite: Dict[str, int]):
"""Process all images in test suite and track results."""
for filename, expected_match in test_suite.items():
try:
file_path = f"{images_path}{filename}"
with open(file_path, 'rb') as f:
response = client.post(
"/api/search-by-image",
files={'file': (filename, f, get_mime_type(filename))},
)
print(f"Response for {filename}: {response.status_code} - {response.json()}")
if response.status_code == 200:
response_data = response.json()
if 'results' in response_data and len(response_data['results']) > 0:
top_result = response_data['results'][0]
returned_id = top_result.get("id")
similarity = top_result.get("similarity", 0.0)
# Case 1: API returned None/null (not in database)
if returned_id is None:
if expected_match == 0:
results["matches_not_existing"] += 1
else:
results["misses_false_negative"] += 1
# Case 2: API returned an ID (found in database)
else:
if returned_id == expected_match and expected_match != 0:
results["matches_existing"]["count"] += 1
results["matches_existing"]["total_similarity"] += similarity
elif expected_match == 0:
results["misses_false_positive"]["count"] += 1
results["misses_false_positive"]["total_similarity"] += similarity
elif returned_id != expected_match and expected_match != 0:
results["misses_wrong_brand"]["count"] += 1
results["misses_wrong_brand"]["total_similarity"] += similarity
results["total"] += 1
else:
print(f"Warning: No results returned for {filename}")
results["total"] += 1
else:
print(f"Error: Got status code {response.status_code} for {filename}")
results["errors"] += 1
results["total"] += 1
except FileNotFoundError:
results["errors"] += 1
results["total"] += 1
print(f"Error: File not found {filename}")
except Exception as e:
results["errors"] += 1
results["total"] += 1
print(f"Error processing {filename}: {e}")
def test_analysis():
print("\n" + "=" * 60)
print("TEST RESULTS ANALYSIS")
print("=" * 60)
# Matches (existing brands)
if results["matches_existing"]["count"] > 0:
avg_similarity = results["matches_existing"]["total_similarity"] / results["matches_existing"]["count"]
print(f"\n✓ Matches (existing brands):")
print(f" Count: {results['matches_existing']['count']}")
print(f" Avg Similarity: {avg_similarity:.2f}")
else:
print(f"\n✓ Matches (existing brands): 0")
# Matches (not existing)
print(f"\n✓ Matches (not in database):")
print(f" Count: {results['matches_not_existing']}")
# Misses - False Positives
if results["misses_false_positive"]["count"] > 0:
avg_similarity = results["misses_false_positive"]["total_similarity"] / results["misses_false_positive"][
"count"]
print(f"\n✗ False Positives (matched existing, should be not-existing):")
print(f" Count: {results['misses_false_positive']['count']}")
print(f" Avg Similarity: {avg_similarity:.2f}")
else:
print(f"\n✗ False Positives: 0")
# Misses - False Negatives
print(f"\n✗ False Negatives (not matched, should be existing):")
print(f" Count: {results['misses_false_negative']}")
# Misses - Wrong Brand
if results["misses_wrong_brand"]["count"] > 0:
avg_similarity = results["misses_wrong_brand"]["total_similarity"] / results["misses_wrong_brand"]["count"]
print(f"\n✗ Wrong Brand Matches:")
print(f" Count: {results['misses_wrong_brand']['count']}")
print(f" Avg Similarity: {avg_similarity:.2f}")
else:
print(f"\n✗ Wrong Brand Matches: 0")
# Summary
print("\n" + "=" * 60)
total_correct = results["matches_existing"]["count"] + results["matches_not_existing"]
if results["total"] > 0:
accuracy = (total_correct / results["total"]) * 100
print(f"Total Accuracy: {accuracy:.1f}% ({total_correct}/{results['total']})")
print(f"Errors: {results['errors']}")
print(f"Total Tests: {results['total']}")
print("=" * 60 + "\n")
if __name__ == "__main__":
print("Starting image processing tests...")
test_image_processing(test_3_third)
test_analysis()