File size: 8,848 Bytes
b61399b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a8ae5
b61399b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a8ae5
b61399b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import React, { useState, useEffect } from 'react';
import { Box, Paper, Typography, Tooltip } from '@mui/material';
import { styled } from '@mui/system';
import axios from 'axios';

const ImageContainer = styled(Paper)({
  width: '40vw',
  height: '40vw',
  display: 'flex',
  justifyContent: 'center',
  alignItems: 'center',
  position: 'relative',
  border: '3px dashed #ccc',
  margin: '10px',
});

const ScrollableList = styled('div')({
  width: '20vw',
  height: '17vw',
  overflowY: 'scroll',
  display: 'flex',
  flexDirection: 'column',
  // alignItems: 'center',
  border: '2px solid #ccc',
  margin: '10px',
});

const TextItem = styled(Typography)({
  margin: '5px 0',
  cursor: 'pointer',
  transition: 'color 0.3s ease',
  '&:hover': {
    color: '#1976d2',
  },
});

const TextItemPlain = styled(Typography)({
  margin: '5px 0',
});

const DotContainer = styled('div')({
  display: 'flex',
  flexDirection: 'column',
  justifyContent: 'center', // Center vertically
  // alignItems: 'center', // Center horizontally
  margin: '0 10px',
  // height: '100%', // Ensure it takes up the full height of the parent container
  height: '40vw',
});


const DotWrapper = styled('div')({
  display: 'flex',
  alignItems: 'center',
  margin: '10px 0',
});

const Dot = styled('div')(({ selected }) => ({
  width: '20px',
  height: '20px',
  borderRadius: '50%',
  backgroundColor: selected ? '#1976d2' : '#ccc',
  marginRight: '10px',
  cursor: 'pointer',
  transition: 'background-color 0.3s ease',
}));

const LevelLabel = styled(Typography)({
  fontSize: '14px',
  color: '#666',
  fontWeight: '500',
});

const ImageGrid = styled('div')({
  display: 'flex',
  justifyContent: 'flex-start',
  alignItems: 'center',
  marginTop: '20px',
  width: '80vw',
  overflowX: 'scroll',
  whiteSpace: 'nowrap',
});

const Thumbnail = styled('img')({
  width: '10vw',
  height: '10vw',
  margin: '5px',
  cursor: 'pointer',
  objectFit: 'cover',
  border: '2px solid transparent',
  transition: 'border-color 0.3s ease',
  '&:hover': {
    borderColor: '#1976d2',
  },
});

const CloseButton = styled('div')({
  position: 'absolute',
  top: '10px',
  right: '10px',
  cursor: 'pointer',
  backgroundColor: '#f5f5f5',
  borderRadius: '50%',
  padding: '5px',
  display: 'flex',
  justifyContent: 'center',
  alignItems: 'center',
  '&:hover': {
    backgroundColor: '#e0e0e0',
  },
});

const App = () => {
  const [mainImage, setMainImage] = useState(null);
  const [selectedIdx, setSelectedIdx] = useState(-1);
  const [thumbnails, setThumbnails] = useState([]); 
  const [selectedDot, setSelectedDot] = useState(-1);
  const [selectedValidIndices, setSelectedValidIndices] = useState([]);
  const [validItems, setValidItems] = useState([]);
  const [invalidItems, setInvalidItems] = useState([]);

  // Fetch thumbnails from backend
  useEffect(() => {
    const fetchThumbnails = async () => {
      try {
        const response = await axios.get('/api/return_thumbnails');
        const { thumbnails } = response.data;
        setThumbnails(thumbnails.map((base64) => `data:image/png;base64,${base64}`)); // Decode Base64
      } catch (error) {
        console.error('Error fetching thumbnails:', error);
        // retry in 1 second
        console.log('Retrying in 1 second...');
        setTimeout(fetchThumbnails, 1000);
      }
    };

    fetchThumbnails();
  }, []);

  const fetchStateData = async () => {
    try {
      console.log('Fetching state data:', selectedIdx, selectedDot, selectedValidIndices);
  
      // join the valid indices with commas
      const validIndicesStr = selectedValidIndices.length === 0 ? 'None' : selectedValidIndices.join(',');
      
      const response = await axios.get('/api/return_state_data', {
        params: {
          image_index: selectedIdx,
          detail_level: selectedDot,
          object_list: validIndicesStr,
        },
      });
      const { mask_overlayed_image, valid_object_color_tuples, invalid_objects } = response.data;
      setMainImage(`data:image/png;base64,${mask_overlayed_image}`);
      setValidItems(valid_object_color_tuples);
      setInvalidItems(invalid_objects);
    } catch (error) {
      console.error('Error fetching state data:', error);
    }
  };
  

  // UseEffect to fetch data after state changes
  useEffect(() => {
    if (selectedIdx !== -1 && selectedDot !== -1) {
      fetchStateData();
    }
  }, [selectedIdx, selectedDot, selectedValidIndices]);

  const handleThumbnailClick = (index) => {
    console.log('Selected image:', index);
    setSelectedIdx(index); // Updates state
    setSelectedDot(2);     // Updates state
    setSelectedValidIndices([]); // Updates state
    // fetchStateData will run automatically after state updates
  };

  const handleCloseImage = () => {
    setMainImage(null);
    setSelectedIdx(-1);
    setSelectedDot(-1);
    setSelectedValidIndices([]);
    setValidItems([]);
    setInvalidItems([]);
    // No need to fetch state data since the image is closed
  };

  const handleDotClick = (index) => {
    if (selectedIdx === -1) {
      console.log('Please select an image first');
      return;
    }
    setSelectedDot(index);
    setSelectedValidIndices([]);
    // fetchStateData will run automatically after state updates
  };

  const handleItemClick = (index) => {
    setSelectedValidIndices((prevSelected) =>
      prevSelected.includes(index)
        ? prevSelected.filter((item) => item !== index) // Remove if already selected
        : [...prevSelected, index] // Add to selected items
    );
    console.log('Toggled item:', index);
    // fetchStateData will run automatically after state updates
  };

  const levels = ['Coarse', 'Mid', 'Fine'];

  return (
    <Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center', p: 2 }}>
      <Box sx={{ display: 'flex', justifyContent: 'center', width: '100%' }}>
        <ImageContainer>
          {mainImage ? (
            <>
              <img src={mainImage} alt="Selected" style={{ width: '100%', height: '100%', objectFit: 'cover' }} />
              <CloseButton onClick={handleCloseImage}>X</CloseButton>
            </>
          ) : (
            <Typography variant="h6" color="textSecondary">Please Select an Image Below</Typography>
          )}
        </ImageContainer>
        <Box sx={{ display: 'flex', flexDirection: 'column', alignItems: 'center' }}>
          <Typography variant="h6">
            Segmented
          </Typography>
          <ScrollableList>
          {validItems.map((tuple, index) => (
            <TextItem
              key={index}
              onClick={() => handleItemClick(index)}
              style={{
                color: selectedValidIndices.includes(index) ? '#1976d2' : '#000', // Change text color if selected
                borderLeft: `10px solid ${tuple[1]}`, // Add colored line
                paddingLeft: '10px', // Adjust padding to make space for the line
                // make the text bold if selected
                fontWeight: selectedValidIndices.includes(index) ? 'bold' : 'normal',
                // add margin to the left
                marginLeft: '1vw',
                marginTop: '0.75vh',
              }}
            >
              {tuple[0]}
            </TextItem>
          ))}
          </ScrollableList>
          <Typography variant="h6">
            Detected (but not Segmented)
          </Typography>
          <ScrollableList>
          {invalidItems.map((text, index) => (
            <TextItemPlain
              key={index}
              style={{
                color: '#000', // Change text color if selected
                // borderLeft: `10px solid ${segmentationColors[index % segmentationColors.length]}`, // Add colored line
                paddingLeft: '10px', // Adjust padding to make space for the line
                // make the text bold if selected
                fontWeight: 'normal',
                // add margin to the left
                marginLeft: '1vw',
                marginTop: '0.75vh',
              }}
            >
              {text}
            </TextItemPlain>
          ))}
          </ScrollableList>
        </Box>

        <DotContainer>
          {levels.map((label, index) => (
            <DotWrapper key={index}>
              <Dot
                selected={selectedDot === index}
                onClick={() => handleDotClick(index)}
              />
              <LevelLabel>{label}</LevelLabel>
            </DotWrapper>
          ))}
        </DotContainer>
      </Box>

      <ImageGrid>
        {thumbnails.map((image, index) => (
          <Thumbnail
            key={index}
            src={image}
            alt={`Thumbnail ${index}`}
            onClick={() => handleThumbnailClick(index)}
          />
        ))}
      </ImageGrid>
    </Box>
  );
};

export default App;