Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion tree-disk-capture/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,8 @@ expo-env.d.ts
.DS_Store

# env file
.env
.env

# Generated files
.eslintrc.js
public/canvaskit.wasm
73 changes: 42 additions & 31 deletions tree-disk-capture/app/(history)/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -67,8 +67,8 @@ export default function CaptureDetails() {
rings: true,
});

const handleSegmentation = async () => {
if (!capture) return;
const handleSegmentation = async (): Promise<CaptureWithAnalysis | null> => {
if (!capture) return null;

setIsAnalyzing(true);
setError(null);
Expand All @@ -93,100 +93,111 @@ export default function CaptureDetails() {
if (savedCapture && savedCapture.analysis) {
setCapture(savedCapture);
setAnalysisData(savedCapture.analysis || newAnalysis);
return savedCapture;
}
return null;
} catch (error) {
console.error('Segmentation failed:', error);
setError('Segmentation failed. Please try again.');
return null;
} finally {
setLoadingProgress(prev => ({ ...prev, segmentation: false }));
setIsAnalyzing(false);
}
};

const handlePithDetection = async () => {
if (!capture || !capture.analysis?.segmentation?.imageBase64) {
const handlePithDetection = async (captureOverride?: CaptureWithAnalysis): Promise<CaptureWithAnalysis | null> => {
const currentCapture = captureOverride || capture;
if (!currentCapture || !currentCapture.analysis?.segmentation?.imageBase64) {
setError('Segmentation must be completed first.');
return;
return null;
}

setIsAnalyzing(true);
setError(null);
setLoadingProgress(prev => ({ ...prev, pithDetection: true }));

const newAnalysis: any = {
...capture.analysis
...currentCapture.analysis
};

try {
console.log('Starting pith detection for capture:', capture.id);
console.log('Starting pith detection for capture:', currentCapture.id);

// Pith Detection
const pithData = await detectPith(capture.analysis.segmentation.imageBase64);
const pithData = await detectPith(currentCapture.analysis.segmentation.imageBase64);
newAnalysis.pith = {
...capture.analysis?.pith,
...currentCapture.analysis?.pith,
x: pithData.x,
y: pithData.y
};

const currentCapture: CaptureWithAnalysis = { ...capture, analysis: newAnalysis };
const savedCapture = await updateCapture(currentCapture);
const updatedCapture: CaptureWithAnalysis = { ...currentCapture, analysis: newAnalysis };
const savedCapture = await updateCapture(updatedCapture);
if (savedCapture && savedCapture.analysis) {
setCapture(savedCapture);
setAnalysisData(savedCapture.analysis || newAnalysis);
return savedCapture;
}
return null;
} catch (error) {
console.error('Pith detection failed:', error);
setError('Pith detection failed. Please try again.');
return null;
} finally {
setLoadingProgress(prev => ({ ...prev, pithDetection: false }));
setIsAnalyzing(false);
}
};

const handleRingDetection = async () => {
if (!capture || !capture.analysis?.segmentation?.imageBase64) {
const handleRingDetection = async (captureOverride?: CaptureWithAnalysis): Promise<CaptureWithAnalysis | null> => {
const currentCapture = captureOverride || capture;
if (!currentCapture || !currentCapture.analysis?.segmentation?.imageBase64) {
setError('Segmentation must be completed first.');
return;
return null;
}

if (!capture.analysis?.pith) {
if (!currentCapture.analysis?.pith) {
setError('Pith detection must be completed first.');
return;
return null;
}

setIsAnalyzing(true);
setError(null);
setLoadingProgress(prev => ({ ...prev, ringDetection: true }));

const newAnalysis: any = {
...capture.analysis
...currentCapture.analysis
};

try {
console.log('Starting ring detection for capture:', capture.id);
console.log('Starting ring detection for capture:', currentCapture.id);

// Ring Detection
const { age, base64 } = await detectRings(
capture.analysis.segmentation.imageBase64,
capture.analysis.pith.x,
capture.analysis.pith.y,
currentCapture.analysis.segmentation.imageBase64,
currentCapture.analysis.pith.x,
currentCapture.analysis.pith.y,
edgeParams.sigma
);
newAnalysis.rings = {
...capture.analysis?.rings,
...currentCapture.analysis?.rings,
imageBase64: base64
};
newAnalysis.predictedAge = age;

const currentCapture: CaptureWithAnalysis = { ...capture, analysis: newAnalysis };
const savedCapture = await updateCapture(currentCapture);
const updatedCapture: CaptureWithAnalysis = { ...currentCapture, analysis: newAnalysis };
const savedCapture = await updateCapture(updatedCapture);
if (savedCapture) {
setCapture(savedCapture);
setAnalysisData(savedCapture.analysis || newAnalysis);
return savedCapture;
}
return null;
} catch (error) {
console.error('Ring detection failed:', error);
setError('Ring detection failed. Please try again.');
return null;
} finally {
setLoadingProgress(prev => ({ ...prev, ringDetection: false }));
setIsAnalyzing(false);
Expand All @@ -204,11 +215,11 @@ export default function CaptureDetails() {
router.setParams({ analyze: 'true' });

try {
await handleSegmentation();
if (capture.analysis?.segmentation) {
await handlePithDetection();
if (capture.analysis?.pith) {
await handleRingDetection();
const segmentedCapture = await handleSegmentation();
if (segmentedCapture?.analysis?.segmentation) {
const pithCapture = await handlePithDetection(segmentedCapture);
if (pithCapture?.analysis?.pith) {
await handleRingDetection(pithCapture);
}
}
} catch (error) {
Expand Down Expand Up @@ -459,7 +470,7 @@ export default function CaptureDetails() {
<Button
variant="outline"
size="sm"
onPress={handlePithDetection}
onPress={() => handlePithDetection()}
disabled={isAnalyzing}>
<Text>Retry</Text>
</Button>
Expand All @@ -474,7 +485,7 @@ export default function CaptureDetails() {
<Button
variant="outline"
size="sm"
onPress={handleRingDetection}
onPress={() => handleRingDetection()}
disabled={isAnalyzing}>
<Text>Retry</Text>
</Button>
Expand Down
Loading