-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAnalysisResults.js
More file actions
599 lines (557 loc) · 16.9 KB
/
AnalysisResults.js
File metadata and controls
599 lines (557 loc) · 16.9 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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
import React, { useState, useEffect } from 'react';
import {
View,
Text,
Image,
StyleSheet,
ScrollView,
TouchableOpacity,
ActivityIndicator,
Alert,
} from 'react-native';
import { LinearGradient } from 'expo-linear-gradient';
import * as FileSystem from 'expo-file-system';
const AnalysisResults = ({ route, navigation }) => {
const { imageUri } = route.params;
const [isAnalyzing, setIsAnalyzing] = useState(true);
const [results, setResults] = useState(null);
const [loadingStatus, setLoadingStatus] = useState('Preparing image...');
// Labels from your model's labels.txt
const LABELS = [
'Pterygium',
'Cataract',
'Normal',
'Glaucoma',
'stye',
'strabismus'
];
useEffect(() => {
const analyzeImage = async () => {
try {
// Convert image to base64
setLoadingStatus('Processing image...');
const base64Image = await FileSystem.readAsStringAsync(imageUri, {
encoding: FileSystem.EncodingType.Base64,
});
setLoadingStatus('Analyzing with AI...');
const predictions = await analyzeWithGoogleVision(base64Image);
// Get recommendations for the top result
const recommendations = getRecommendations(predictions[0].name);
// Set results
setResults({
primaryDiagnosis: predictions[0].name,
confidence: predictions[0].probability,
possibleConditions: predictions,
recommendations: recommendations
});
setIsAnalyzing(false);
} catch (error) {
console.error('Error analyzing image:', error);
Alert.alert(
'Analysis Error',
'There was a problem connecting to the vision service. Using demonstration data instead.',
[{ text: 'OK' }]
);
// Fall back to example data
useFallbackData();
}
};
analyzeImage();
}, []);
const analyzeWithGoogleVision = async (base64Image) => {
try {
const apiKey = 'AIzaSyDfhChMPkMuF4giMTalBELMeJywvVsk2_M';
const response = await fetch(
`https://vision.googleapis.com/v1/images:annotate?key=${apiKey}`,
{
method: 'POST',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify({
requests: [
{
image: {
content: base64Image,
},
features: [
{ type: 'LABEL_DETECTION', maxResults: 15 },
{ type: 'IMAGE_PROPERTIES', maxResults: 5 },
],
},
],
}),
}
);
if (!response.ok) {
const errorText = await response.text();
console.error('Google Vision API error:', errorText);
throw new Error(`API error: ${response.status}`);
}
const data = await response.json();
console.log('Google Vision API response:', JSON.stringify(data));
// Map Google's response to your eye conditions
return mapGoogleResultsToConditions(data);
} catch (error) {
console.error('Error with Google Vision API:', error);
throw error;
}
};
// Map Google's general labels to your specific conditions
const mapGoogleResultsToConditions = (data) => {
const labelResults = data.responses[0].labelAnnotations || [];
const colorInfo = data.responses[0].imagePropertiesAnnotation?.dominantColors?.colors || [];
const eyeConditions = [
{
name: 'Cataract',
keywords: ['cataract', 'cloudy', 'white', 'opacity', 'eye condition', 'lens', 'elderly', 'aging'],
colorTraits: { white: true, gray: true }
},
{
name: 'Pterygium',
keywords: ['pterygium', 'growth', 'tissue', 'conjunctiva', 'eye surface', 'cornea'],
colorTraits: { pink: true, red: true }
},
{
name: 'Normal',
keywords: ['eye', 'healthy', 'normal', 'iris', 'pupil', 'clear', 'vision'],
colorTraits: { blue: true, brown: true, green: true, black: true }
},
{
name: 'Glaucoma',
keywords: ['glaucoma', 'pressure', 'optic nerve', 'eye disease', 'vision loss'],
colorTraits: { red: true }
},
{
name: 'stye',
keywords: ['stye', 'sty', 'infection', 'eyelid', 'bump', 'swelling', 'redness'],
colorTraits: { red: true }
},
{
name: 'strabismus',
keywords: ['strabismus', 'crossed', 'misaligned', 'eye position', 'squint'],
colorTraits: {}
}
];
// Check for colors in the image
const hasColor = (colorName) => {
return colorInfo.some(color => {
const r = color.color.red || 0;
const g = color.color.green || 0;
const b = color.color.blue || 0;
switch(colorName.toLowerCase()) {
case 'red': return r > 200 && g < 100 && b < 100;
case 'white': return r > 200 && g > 200 && b > 200;
case 'gray': return r > 100 && r < 200 && g > 100 && g < 200 && b > 100 && b < 200;
case 'blue': return b > 200 && r < 100 && g < 150;
case 'brown': return r > 100 && g > 50 && g < 100 && b < 50;
case 'green': return g > 200 && r < 100 && b < 100;
case 'black': return r < 50 && g < 50 && b < 50;
case 'pink': return r > 200 && g > 100 && g < 200 && b > 100 && b < 200;
default: return false;
}
});
};
// Calculate scores for each condition
const results = eyeConditions.map(condition => {
let score = 10; // Base score
// Add points for matching keywords
labelResults.forEach(label => {
condition.keywords.forEach(keyword => {
if (label.description.toLowerCase().includes(keyword.toLowerCase())) {
score += label.score * 25; // Weight based on label confidence
}
});
});
// Add points for matching colors
Object.entries(condition.colorTraits).forEach(([color, important]) => {
if (important && hasColor(color)) {
score += 15;
}
});
return {
name: condition.name,
probability: Math.min(95, Math.max(5, Math.round(score)))
};
});
// If we have eye-related labels but no high scores, boost the highest
const hasEyeLabels = labelResults.some(label =>
label.description.toLowerCase().includes('eye') ||
label.description.toLowerCase().includes('vision')
);
if (hasEyeLabels && results.every(r => r.probability < 40)) {
const highestIndex = results.reduce((maxIdx, item, idx, arr) =>
item.probability > arr[maxIdx].probability ? idx : maxIdx, 0);
results[highestIndex].probability = 65 + Math.floor(Math.random() * 20);
}
// Sort by probability
return results.sort((a, b) => b.probability - a.probability);
};
// Fallback to sample data if analysis fails
const useFallbackData = () => {
setResults({
primaryDiagnosis: 'Cataract',
confidence: 87,
possibleConditions: [
{ name: 'Cataract', probability: 87 },
{ name: 'Pterygium', probability: 9 },
{ name: 'Normal', probability: 2 },
{ name: 'Glaucoma', probability: 1 },
{ name: 'stye', probability: 0.5 },
{ name: 'strabismus', probability: 0.5 }
],
recommendations: getRecommendations('Cataract')
});
setIsAnalyzing(false);
};
// Generate recommendations based on diagnosis
const getRecommendations = (diagnosis) => {
const commonRecs = [
'Consult an ophthalmologist for a definitive diagnosis',
'Avoid rubbing your eyes',
'Protect your eyes from UV exposure with sunglasses'
];
switch (diagnosis) {
case 'Cataract':
return [
...commonRecs,
'Consider cataract surgery if vision is significantly affected',
'Use brighter lighting for reading and other activities'
];
case 'Pterygium':
return [
...commonRecs,
'Use artificial tears to keep the eye moist',
'Wear protective eyewear outdoors',
'Consider surgical removal if it affects vision or causes discomfort'
];
case 'Normal':
return [
'Your eye appears healthy',
'Continue with regular eye check-ups',
'Protect your eyes from UV light',
'Take regular breaks during screen time (20-20-20 rule)'
];
case 'Glaucoma':
return [
...commonRecs,
'Use prescribed eye drops regularly',
'Attend regular follow-up appointments to monitor eye pressure',
'Consider discussing surgical options with your ophthalmologist if medication is insufficient'
];
case 'stye':
return [
...commonRecs,
'Apply warm compresses to the affected area several times a day',
'Keep eyelids clean with gentle baby shampoo',
'Avoid wearing makeup until the stye resolves'
];
case 'strabismus':
return [
...commonRecs,
'Consult a specialist for potential vision therapy',
'Special eyeglasses may help in some cases',
'Surgery may be recommended to align the eyes properly'
];
default:
return commonRecs;
}
};
const navigateToCondition = (condition) => {
// Normalize condition name to match your navigation routes
let normalizedCondition = condition.charAt(0).toUpperCase() + condition.slice(1).toLowerCase();
// Check if the screen exists in navigation
const validScreens = ['Cataract', 'Pterygium', 'Hypopyon', 'Keratitis', 'Chalazion'];
if (validScreens.includes(normalizedCondition)) {
navigation.navigate(normalizedCondition);
} else {
// If screen doesn't exist yet, alert the user
Alert.alert(
'Information',
`Detailed information for ${condition} is coming soon.`,
[{ text: 'OK' }]
);
}
};
if (isAnalyzing) {
return (
<View style={styles.loadingContainer}>
<ActivityIndicator size="large" color="#0056b3" />
<Text style={styles.loadingText}>{loadingStatus}</Text>
<Text style={styles.loadingSubtext}>Please wait while our AI examines the details</Text>
<Image
source={{ uri: imageUri }}
style={styles.previewImage}
/>
</View>
);
}
return (
<ScrollView style={styles.container}>
<LinearGradient
colors={['#6a11cb', '#2575fc']}
style={styles.header}
>
<Text style={styles.headerTitle}>Analysis Results</Text>
</LinearGradient>
<View style={styles.imageContainer}>
<Image
source={{ uri: imageUri }}
style={styles.image}
/>
</View>
<View style={styles.resultsContainer}>
<View style={styles.diagnosisContainer}>
<Text style={styles.diagnosisLabel}>Primary Diagnosis:</Text>
<Text style={styles.diagnosisText}>{results.primaryDiagnosis}</Text>
<Text style={styles.confidenceText}>Confidence: {results.confidence}%</Text>
<TouchableOpacity
style={styles.learnMoreButton}
onPress={() => navigateToCondition(results.primaryDiagnosis)}
>
<Text style={styles.learnMoreButtonText}>Learn More About {results.primaryDiagnosis}</Text>
</TouchableOpacity>
</View>
<View style={styles.card}>
<Text style={styles.cardTitle}>Possible Conditions</Text>
{results.possibleConditions.map((condition, index) => (
<View key={index} style={styles.conditionItem}>
<View style={styles.conditionNameContainer}>
<Text style={styles.conditionName}>{condition.name}</Text>
<Text style={styles.conditionProbability}>{condition.probability}%</Text>
</View>
<View style={styles.probabilityBar}>
<View
style={[
styles.probabilityFill,
{ width: `${condition.probability}%` }
]}
/>
</View>
</View>
))}
</View>
<View style={styles.card}>
<Text style={styles.cardTitle}>Recommendations</Text>
{results.recommendations.map((recommendation, index) => (
<View key={index} style={styles.recommendationItem}>
<Text style={styles.recommendationNumber}>{index + 1}</Text>
<Text style={styles.recommendationText}>{recommendation}</Text>
</View>
))}
</View>
<View style={styles.disclaimerContainer}>
<Text style={styles.disclaimerText}>
This is not a medical diagnosis. Please consult a healthcare professional for accurate diagnosis and treatment.
</Text>
</View>
<TouchableOpacity
style={styles.backButton}
onPress={() => navigation.navigate('Scan')}
>
<Text style={styles.backButtonText}>Back to Scan</Text>
</TouchableOpacity>
</View>
</ScrollView>
);
};
const styles = StyleSheet.create({
container: {
flex: 1,
backgroundColor: '#f0f0f0',
},
loadingContainer: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f0f0f0',
padding: 20,
},
loadingText: {
fontSize: 20,
fontWeight: 'bold',
color: '#0056b3',
marginTop: 20,
},
loadingSubtext: {
fontSize: 16,
color: '#666',
marginTop: 10,
textAlign: 'center',
marginBottom: 30,
},
previewImage: {
width: 200,
height: 200,
borderRadius: 10,
borderWidth: 2,
borderColor: '#fff',
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.2,
shadowRadius: 4,
elevation: 5,
},
header: {
padding: 30,
borderBottomLeftRadius: 30,
borderBottomRightRadius: 30,
alignItems: 'center',
},
headerTitle: {
fontSize: 28,
fontWeight: 'bold',
color: '#fff',
},
imageContainer: {
alignItems: 'center',
padding: 20,
},
image: {
width: 250,
height: 250,
borderRadius: 15,
borderWidth: 3,
borderColor: '#fff',
shadowColor: '#000',
shadowOffset: { width: 0, height: 4 },
shadowOpacity: 0.3,
shadowRadius: 6,
elevation: 8,
},
resultsContainer: {
padding: 20,
},
diagnosisContainer: {
backgroundColor: '#fff',
borderRadius: 15,
padding: 20,
marginBottom: 20,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 6,
elevation: 5,
alignItems: 'center',
},
diagnosisLabel: {
fontSize: 18,
color: '#666',
marginBottom: 5,
},
diagnosisText: {
fontSize: 32,
fontWeight: 'bold',
color: '#0056b3',
marginBottom: 5,
},
confidenceText: {
fontSize: 16,
color: '#666',
marginBottom: 15,
},
learnMoreButton: {
backgroundColor: '#007bff',
paddingVertical: 12,
paddingHorizontal: 20,
borderRadius: 8,
marginTop: 10,
},
learnMoreButtonText: {
color: '#fff',
fontSize: 16,
fontWeight: 'bold',
},
card: {
backgroundColor: '#fff',
borderRadius: 15,
padding: 20,
marginBottom: 20,
shadowColor: '#000',
shadowOffset: { width: 0, height: 2 },
shadowOpacity: 0.1,
shadowRadius: 6,
elevation: 5,
},
cardTitle: {
fontSize: 20,
fontWeight: 'bold',
color: '#333',
marginBottom: 15,
},
conditionItem: {
marginBottom: 15,
},
conditionNameContainer: {
flexDirection: 'row',
justifyContent: 'space-between',
marginBottom: 5,
},
conditionName: {
fontSize: 16,
fontWeight: 'bold',
color: '#333',
},
conditionProbability: {
fontSize: 16,
color: '#666',
},
probabilityBar: {
height: 8,
backgroundColor: '#e0e0e0',
borderRadius: 4,
},
probabilityFill: {
height: '100%',
backgroundColor: '#007bff',
borderRadius: 4,
},
recommendationItem: {
flexDirection: 'row',
marginBottom: 15,
alignItems: 'flex-start',
},
recommendationNumber: {
width: 25,
height: 25,
backgroundColor: '#007bff',
borderRadius: 15,
color: '#fff',
fontWeight: 'bold',
textAlign: 'center',
lineHeight: 25,
marginRight: 10,
},
recommendationText: {
flex: 1,
fontSize: 16,
color: '#333',
},
disclaimerContainer: {
backgroundColor: '#f8f9fa',
borderRadius: 10,
padding: 15,
marginBottom: 20,
borderLeftWidth: 4,
borderLeftColor: '#ffc107',
},
disclaimerText: {
fontSize: 14,
color: '#666',
fontStyle: 'italic',
},
backButton: {
backgroundColor: '#6c757d',
paddingVertical: 12,
paddingHorizontal: 20,
borderRadius: 8,
alignItems: 'center',
marginBottom: 20,
},
backButtonText: {
color: '#fff',
fontSize: 16,
fontWeight: 'bold',
},
});
export default AnalysisResults;