File size: 2,391 Bytes
ed9f15f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
/**

 * Model Inference - Run predictions on models

 */

import { getModel } from '../state.js';
import { LABEL_MAPPINGS } from '../config.js';
import { preprocessImage } from '../processing/imagePreprocessing.js';
import { postprocessClassification, postprocessGrading } from '../processing/postprocessing.js';

/**

 * Run classification model (embryo yes/no)

 */
export async function runClassification(imageData) {
    const inputTensor = await preprocessImage(imageData, 224);
    const feeds = { pixel_values: inputTensor };
    const results = await getModel('classifier').run(feeds);
    
    return postprocessClassification(results, LABEL_MAPPINGS.classifier);
}

/**

 * Run quality check model (poor/good)

 */
export async function runQualityCheck(imageData) {
    const inputTensor = await preprocessImage(imageData, 224);
    const feeds = { pixel_values: inputTensor };
    const results = await getModel('poorGood').run(feeds);
    
    return postprocessClassification(results, LABEL_MAPPINGS.poorGood);
}

/**

 * Run grading model (Gardner scale)

 */
export async function runGrading(imageData) {
    const inputTensor = await preprocessImage(imageData, 224);
    const feeds = { pixel_values: inputTensor };
    const results = await getModel('grader').run(feeds);
    
    return postprocessGrading(results);
}

/**

 * Evaluate embryo quality and grade

 */
export async function evaluateEmbryo(imageData) {
    try {
        // Step 1: Check quality (poor/good)
        const qualityResult = await runQualityCheck(imageData);
        
        if (qualityResult.label === 'poor') {
            return {
                quality: 'poor',
                poorGoodConfidence: qualityResult.confidence,
                grade: null,
                predictions: {}
            };
        }
        
        // Step 2: Get grade
        const gradeResult = await runGrading(imageData);
        
        return {
            quality: 'good',
            poorGoodConfidence: qualityResult.confidence,
            grade: gradeResult.label,
            confidence: gradeResult.confidence,
            predictions: gradeResult.allPredictions
        };
    } catch (error) {
        console.error('Error evaluating embryo:', error);
        throw new Error(`Failed to evaluate embryo: ${error.message}`);
    }
}