File size: 6,082 Bytes
9aee46b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React from 'react';
import { 
  Paper, 
  Typography, 
  Box, 
  List, 
  ListItem, 
  ListItemText, 
  Divider,
  Grid,
  Chip
} from '@material-ui/core';
import VectorDBActions from './VectorDBActions';
import { makeStyles } from '@material-ui/core/styles';

const useStyles = makeStyles((theme) => ({
  paper: {
    padding: theme.spacing(2)
  },
  marginBottom: {
    marginBottom: theme.spacing(2)
  },
  resultImage: {
    maxWidth: '100%',
    maxHeight: '400px',
    objectFit: 'contain'
  },
  dividerMargin: {
    margin: `${theme.spacing(2)}px 0`
  },
  chipContainer: {
    display: 'flex',
    gap: theme.spacing(1),
    flexWrap: 'wrap'
  }
}));

const ResultDisplay = ({ results }) => {
  const classes = useStyles();
  if (!results) return null;
  
  const { model, data } = results;
  
  // Helper to format times nicely
  const formatTime = (ms) => {
    if (ms === undefined || ms === null || isNaN(ms)) return '-';
    const num = Number(ms);
    if (num < 1000) return `${num.toFixed(2)} ms`;
    return `${(num / 1000).toFixed(2)} s`;
  };
  
  // Check if there's an error
  if (data.error) {
    return (
      <Paper sx={{ p: 2, bgcolor: '#ffebee' }}>
        <Typography color="error">{data.error}</Typography>
      </Paper>
    );
  }

  // Display performance info
  const renderPerformanceInfo = () => {
    if (!data.performance) return null;
    
    return (
      <Box className="performance-info">
        <Divider className={classes.dividerMargin} />
        <Typography variant="body2">
          Inference time: {formatTime(data.performance.inference_time)} on {data.performance.device}
        </Typography>
      </Box>
    );
  };

  // Render for YOLO and DETR (object detection)
  if (model === 'yolo' || model === 'detr') {
    return (
      <Paper className={classes.paper}>
        <Typography variant="h6" gutterBottom>
          {model === 'yolo' ? 'YOLOv8' : 'DETR'} Detection Results
        </Typography>
        
        <Grid container spacing={3}>
          <Grid item xs={12} md={6}>
            {data.image && (
              <Box className={classes.marginBottom}>
                <Typography variant="subtitle1" gutterBottom>
                  Detection Result
                </Typography>
                <img 
                  src={`data:image/png;base64,${data.image}`} 
                  alt="Detection Result" 
                  className={classes.resultImage}
                />
              </Box>
            )}
          </Grid>
          
          <Grid item xs={12} md={6}>
            <Box className={classes.marginBottom}>
              <Typography variant="subtitle1" gutterBottom>
                Detected Objects:
              </Typography>
              
              {data.detections && data.detections.length > 0 ? (
                <List>
                  {data.detections.map((detection, index) => (
                    <React.Fragment key={index}>
                      <ListItem>
                        <ListItemText 
                          primary={
                            <Box style={{ display: 'flex', alignItems: 'center' }}>
                              <Typography variant="body1" component="span">
                                {detection.class}
                              </Typography>
                              <Chip 
                                label={`${(detection.confidence * 100).toFixed(0)}%`}
                                size="small"
                                color="primary"
                                style={{ marginLeft: 8 }}
                              />
                            </Box>
                          } 
                          secondary={`Bounding Box: [${detection.bbox.join(', ')}]`} 
                        />
                      </ListItem>
                      {index < data.detections.length - 1 && <Divider />}
                    </React.Fragment>
                  ))}
                </List>
              ) : (
                <Typography variant="body1">No objects detected</Typography>
              )}
            </Box>
          </Grid>
        </Grid>
        
        {renderPerformanceInfo()}
        
        {/* Vector DB Actions for Object Detection */}
        <VectorDBActions results={results} />
      </Paper>
    );
  }
  
  // Render for ViT (classification)
  if (model === 'vit') {
    return (
      <Paper className={classes.paper}>
        <Typography variant="h6" gutterBottom>
          ViT Classification Results
        </Typography>
        
        <Typography variant="subtitle1" gutterBottom>
          Top Predictions:
        </Typography>
        
        {data.top_predictions && data.top_predictions.length > 0 ? (
          <List>
            {data.top_predictions.map((prediction, index) => (
              <React.Fragment key={index}>
                <ListItem>
                  <ListItemText 
                    primary={
                      <Box style={{ display: 'flex', alignItems: 'center' }}>
                        <Typography variant="body1" component="span">
                          {prediction.rank}. {prediction.class}
                        </Typography>
                        <Chip 
                          label={`${(prediction.probability * 100).toFixed(1)}%`}
                          size="small"
                          color={index === 0 ? "primary" : "default"}
                          style={{ marginLeft: 8 }}
                        />
                      </Box>
                    } 
                  />
                </ListItem>
                {index < data.top_predictions.length - 1 && <Divider />}
              </React.Fragment>
            ))}
          </List>
        ) : (
          <Typography variant="body1">No classifications available</Typography>
        )}
        
        {renderPerformanceInfo()}
        
        {/* Vector DB Actions for ViT Classification */}
        <VectorDBActions results={results} />
      </Paper>
    );
  }
  
  return null;
};

export default ResultDisplay;