|
| 1 | +import fs from 'fs'; |
| 2 | +import path from 'path'; |
| 3 | + |
| 4 | +import { fetchURL, write } from 'image-js'; |
| 5 | + |
| 6 | +import { defaultImages, defaultMasks } from './imageDataset.mjs'; |
| 7 | + |
| 8 | +const __dirname = import.meta.dirname; |
| 9 | + |
| 10 | +export async function imageLoader() { |
| 11 | + const demoImagesDir = 'demoImages'; |
| 12 | + const staticDir = 'static'; |
| 13 | + |
| 14 | + const imageData = { masks: [], images: [] }; |
| 15 | + try { |
| 16 | + // Create static directory if it doesn't exist |
| 17 | + const staticPath = path.join(__dirname, staticDir, demoImagesDir); |
| 18 | + |
| 19 | + if (!fs.existsSync(staticPath)) { |
| 20 | + fs.mkdirSync(staticPath, { recursive: true }); |
| 21 | + } |
| 22 | + |
| 23 | + const images = await Promise.all( |
| 24 | + defaultImages.map((imageDataUrl) => fetchURL(imageDataUrl.value)), |
| 25 | + ); |
| 26 | + |
| 27 | + for (let i = 0; i < images.length; i++) { |
| 28 | + const image = images[i]; |
| 29 | + const imageDataUrl = defaultImages[i]; |
| 30 | + const imageTitle = getFilename(imageDataUrl.value); |
| 31 | + const imagePath = path.join( |
| 32 | + __dirname, |
| 33 | + staticDir, |
| 34 | + demoImagesDir, |
| 35 | + 'images', |
| 36 | + imageTitle, |
| 37 | + ); |
| 38 | + write(imagePath, image, { recursive: true }); |
| 39 | + // Keeping object structure for compatibility |
| 40 | + imageData.images.push({ |
| 41 | + type: 'url', |
| 42 | + imageType: 'image', |
| 43 | + label: `${imageDataUrl.label} (${image.width}x${image.height})`, |
| 44 | + value: `/${demoImagesDir}/images/${imageTitle}`, |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + const masks = await Promise.all( |
| 49 | + defaultMasks.map((maskDataUrl) => fetchURL(maskDataUrl.value)), |
| 50 | + ); |
| 51 | + |
| 52 | + for (let i = 0; i < masks.length; i++) { |
| 53 | + const mask = masks[i]; |
| 54 | + const maskDataUrl = defaultMasks[i]; |
| 55 | + const maskTitle = getFilename(maskDataUrl.value); |
| 56 | + const maskPath = path.join( |
| 57 | + __dirname, |
| 58 | + staticDir, |
| 59 | + demoImagesDir, |
| 60 | + 'masks', |
| 61 | + maskTitle, |
| 62 | + ); |
| 63 | + write(maskPath, mask, { recursive: true }); |
| 64 | + // Keeping object structure for compatibility |
| 65 | + imageData.masks.push({ |
| 66 | + type: 'url', |
| 67 | + imageType: 'mask', |
| 68 | + label: `${maskDataUrl.label} (${mask.width}x${mask.height})`, |
| 69 | + value: `/${demoImagesDir}/masks/${maskTitle}`, |
| 70 | + }); |
| 71 | + } |
| 72 | + // Write data about newly created files. |
| 73 | + const outputPath = path.join(__dirname, 'src/demo/contexts/demo/generated'); |
| 74 | + if (!fs.existsSync(outputPath)) { |
| 75 | + fs.mkdirSync(outputPath, { recursive: true }); |
| 76 | + } |
| 77 | + fs.writeFileSync( |
| 78 | + `${outputPath}/imageData.json`, |
| 79 | + JSON.stringify(imageData, null, 2), |
| 80 | + ); |
| 81 | + } catch (error) { |
| 82 | + throw new Error(`Error in imageLoader: ${error.message}`); |
| 83 | + } |
| 84 | + |
| 85 | + return imageData; |
| 86 | +} |
| 87 | +// Returns only filename with extension. |
| 88 | +function getFilename(filepath) { |
| 89 | + return filepath.replace(/^.*[\\/]/, ''); |
| 90 | +} |
| 91 | +await imageLoader(); |
0 commit comments