shkumar156 commited on
Commit
5cd81f1
·
verified ·
1 Parent(s): 534d2e1

Update server.js

Browse files
Files changed (1) hide show
  1. server.js +14 -48
server.js CHANGED
@@ -187,77 +187,43 @@ app.post('/upload', upload.single('image'), async (req, res) => {
187
  }
188
  });
189
 
190
- // Function to run YOLOv5 detection
191
  async function runYOLOv5Detection(imagePath) {
192
  return new Promise((resolve) => {
193
- const yolov5Path = path.join(__dirname, 'detect.py');
194
  const weightsPath = path.join(__dirname, 'yolov5s.pt');
195
-
196
- console.log('Running YOLOv5 detection...');
197
- console.log('Image path:', imagePath);
198
- console.log('YOLOv5 script path:', yolov5Path);
199
- console.log('Weights path:', weightsPath);
200
 
201
  const pythonProcess = spawn('python', [
202
  yolov5Path,
203
- '--weights', weightsPath,
204
- '--source', imagePath,
205
- '--project', path.join(__dirname, '../runs/detect'),
206
- '--name', 'exp',
207
- '--exist-ok'
208
  ]);
209
 
210
- let stdout = '';
211
- let stderr = '';
212
 
213
  pythonProcess.stdout.on('data', (data) => {
214
- stdout += data.toString();
215
- console.log('YOLOv5 output:', data.toString());
216
  });
217
 
218
  pythonProcess.stderr.on('data', (data) => {
219
- stderr += data.toString();
220
- console.error('YOLOv5 error:', data.toString());
221
  });
222
 
223
  pythonProcess.on('close', (code) => {
224
- console.log('YOLOv5 process exited with code:', code);
225
-
226
  if (code === 0) {
227
- // Find the detected image in the runs/detect/exp directory
228
- const expDir = path.join(__dirname, '../runs/detect/exp');
229
- const detectedImagePath = findDetectedImage(expDir, path.basename(imagePath));
230
-
231
- if (detectedImagePath) {
232
- // Get just the filename for the URL
233
- const detectedImageFilename = path.basename(detectedImagePath);
234
- console.log('Detected image filename:', detectedImageFilename);
235
-
236
- resolve({
237
- success: true,
238
- detectedImagePath: `/detected/exp/${detectedImageFilename}`
239
- });
240
- } else {
241
- resolve({
242
- success: false,
243
- error: 'Detection completed but result image not found'
244
- });
245
- }
246
  } else {
247
  resolve({
248
  success: false,
249
- error: `YOLOv5 detection failed with code ${code}: ${stderr}`
250
  });
251
  }
252
  });
253
-
254
- pythonProcess.on('error', (error) => {
255
- console.error('Failed to start YOLOv5 process:', error);
256
- resolve({
257
- success: false,
258
- error: `Failed to start YOLOv5 process: ${error.message}`
259
- });
260
- });
261
  });
262
  }
263
 
 
187
  }
188
  });
189
 
190
+ // Function to run YOLOv5 detection using Ultralytics API
191
  async function runYOLOv5Detection(imagePath) {
192
  return new Promise((resolve) => {
193
+ const yolov5Path = path.join(__dirname, 'yolo_detect.py');
194
  const weightsPath = path.join(__dirname, 'yolov5s.pt');
 
 
 
 
 
195
 
196
  const pythonProcess = spawn('python', [
197
  yolov5Path,
198
+ weightsPath,
199
+ imagePath,
 
 
 
200
  ]);
201
 
202
+ let output = '';
203
+ let errorOutput = '';
204
 
205
  pythonProcess.stdout.on('data', (data) => {
206
+ output += data.toString();
 
207
  });
208
 
209
  pythonProcess.stderr.on('data', (data) => {
210
+ errorOutput += data.toString();
 
211
  });
212
 
213
  pythonProcess.on('close', (code) => {
 
 
214
  if (code === 0) {
215
+ // Output should be the path to the detected image
216
+ resolve({
217
+ success: true,
218
+ detectedImagePath: output.trim(),
219
+ });
 
 
 
 
 
 
 
 
 
 
 
 
 
 
220
  } else {
221
  resolve({
222
  success: false,
223
+ error: errorOutput || `YOLOv5 detection failed with code ${code}`,
224
  });
225
  }
226
  });
 
 
 
 
 
 
 
 
227
  });
228
  }
229