forked from harpreetsahota204/gui_dataset_creator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.js
More file actions
164 lines (138 loc) · 5.78 KB
/
server.js
File metadata and controls
164 lines (138 loc) · 5.78 KB
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
const express = require('express');
const fs = require('fs');
const path = require('path');
const cors = require('cors');
const app = express();
app.use(cors());
app.use(express.json({ limit: '50mb' }));
app.use(express.static('.'));
// Ensure data directories exist
const dataDir = path.join(__dirname, 'data');
const sequenceDataDir = path.join(__dirname, 'sequence_data');
if (!fs.existsSync(dataDir)) {
fs.mkdirSync(dataDir);
}
if (!fs.existsSync(sequenceDataDir)) {
fs.mkdirSync(sequenceDataDir);
}
// Save image endpoint
app.post('/save-image', (req, res) => {
const { filename, data, folder = 'data' } = req.body;
const base64Data = data.replace(/^data:image\/png;base64,/, '');
// Determine target directory
const targetDir = folder === 'sequence_data' ? sequenceDataDir : dataDir;
fs.writeFile(path.join(targetDir, filename), base64Data, 'base64', (err) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ success: true, path: path.join(targetDir, filename) });
}
});
});
// Save JSON endpoint
app.post('/save-json', (req, res) => {
const { filename, data, folder = 'data' } = req.body;
// Determine target directory
const targetDir = folder === 'sequence_data' ? sequenceDataDir : dataDir;
fs.writeFile(path.join(targetDir, filename), JSON.stringify(data, null, 2), (err) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ success: true, path: path.join(targetDir, filename) });
}
});
});
// List files endpoint
app.get('/list-files', (req, res) => {
fs.readdir(dataDir, (err, files) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
res.json({ files });
}
});
});
// List folders endpoint
app.get('/list-folders', (req, res) => {
const baseDir = __dirname;
fs.readdir(baseDir, { withFileTypes: true }, (err, entries) => {
if (err) {
res.status(500).json({ error: err.message });
} else {
// Filter for directories only and check if they contain annotation files
const folders = [];
entries.forEach(entry => {
if (entry.isDirectory() && !entry.name.startsWith('.') && entry.name !== 'node_modules') {
const folderPath = path.join(baseDir, entry.name);
// Check if folder contains any .json files (potential annotation files)
try {
const folderContents = fs.readdirSync(folderPath);
const hasJsonFiles = folderContents.some(file => file.endsWith('.json'));
const hasImages = folderContents.some(file =>
file.endsWith('.png') || file.endsWith('.jpg') || file.endsWith('.jpeg')
);
if (hasJsonFiles || hasImages) {
const jsonFiles = folderContents.filter(file => file.endsWith('.json'));
const imageCount = folderContents.filter(file =>
file.endsWith('.png') || file.endsWith('.jpg') || file.endsWith('.jpeg')
).length;
folders.push({
name: entry.name,
path: folderPath,
jsonFiles: jsonFiles,
imageCount: imageCount,
hasAnnotations: hasJsonFiles,
hasImages: hasImages
});
}
} catch (folderErr) {
// Skip folders we can't read
}
}
});
res.json({ folders });
}
});
});
// Load dataset from specific folder endpoint
app.get('/load-dataset-from-folder', (req, res) => {
const { folderName, filename } = req.query;
if (!folderName || !filename) {
return res.status(400).json({ error: 'Folder name and filename are required' });
}
const folderPath = path.join(__dirname, folderName);
const annotationPath = path.join(folderPath, filename);
// Security check - ensure the folder is within our project directory
if (!annotationPath.startsWith(__dirname)) {
return res.status(403).json({ error: 'Access denied' });
}
if (fs.existsSync(annotationPath)) {
try {
const data = JSON.parse(fs.readFileSync(annotationPath, 'utf8'));
res.json(data);
} catch (parseErr) {
res.status(500).json({ error: `Failed to parse JSON file: ${parseErr.message}` });
}
} else {
res.status(404).json({ error: `No dataset found at ${annotationPath}` });
}
});
// Load dataset endpoint
app.get('/load-dataset', (req, res) => {
const { folder = 'data', filename = 'annotations_coco.json' } = req.query;
// Determine source directory
const sourceDir = folder === 'sequence_data' ? sequenceDataDir : dataDir;
const annotationPath = path.join(sourceDir, filename);
if (fs.existsSync(annotationPath)) {
const data = JSON.parse(fs.readFileSync(annotationPath, 'utf8'));
res.json(data);
} else {
res.status(404).json({ error: `No dataset found at ${annotationPath}` });
}
});
const PORT = 3000;
app.listen(PORT, () => {
console.log(`Server running on http://localhost:${PORT}`);
console.log(`Regular files will be saved to: ${dataDir}`);
console.log(`Sequence files will be saved to: ${sequenceDataDir}`);
});