const express = require('express'); const multer = require('multer'); const cors = require('cors'); const path = require('path'); const fs = require('fs'); const { spawn } = require('child_process'); const app = express(); const PORT = process.env.PORT || 7860; // Middleware const corsOptions = { origin: 'https://image-object-detection-using-yolov5.vercel.app', credentials: true, }; app.use(cors(corsOptions)); app.use(express.json()); app.use(express.static(path.join(__dirname, 'uploads'))); app.use('/detected', express.static(path.join(__dirname, '../runs/detect'))); // Serve detection results as static files app.use('/results', express.static('/data/runs/detect')); // Configure multer for file uploads const uploadDir = '/data/uploads'; const storage = multer.diskStorage({ destination: function (req, file, cb) { if (!fs.existsSync(uploadDir)) { fs.mkdirSync(uploadDir, { recursive: true }); } cb(null, uploadDir); }, filename: function (req, file, cb) { const uniqueSuffix = Date.now() + '-' + Math.round(Math.random() * 1E9); cb(null, file.fieldname + '-' + uniqueSuffix + path.extname(file.originalname)); } }); const upload = multer({ storage: storage, fileFilter: function (req, file, cb) { // Accept only image files if (file.mimetype.startsWith('image/')) { cb(null, true); } else { cb(new Error('Only image files are allowed!'), false); } }, limits: { fileSize: 10 * 1024 * 1024 // 10MB limit } }); // Routes app.get('/', (req, res) => { // Check if the request wants HTML or JSON const acceptHeader = req.headers.accept || ''; if (acceptHeader.includes('text/html')) { // Send HTML response for browser viewing res.send(` Backend Status

🚀 Backend Running!

✅ Object Detection API is Active

Port: ${PORT}

Environment: ${process.env.NODE_ENV || 'development'}

Upload Endpoint: POST /upload

Test Endpoint: GET /test-detected

Started at: ${new Date().toLocaleString()}
`); } else { // Send JSON response for API calls res.json({ message: 'Backend Running!', status: 'active', port: PORT, environment: process.env.NODE_ENV || 'development', timestamp: new Date().toISOString() }); } }); // Test endpoint to check if detected images are accessible app.get('/test-detected', (req, res) => { const expDir = path.join(__dirname, '../runs/detect/exp'); try { if (fs.existsSync(expDir)) { const files = fs.readdirSync(expDir); const imageFiles = files.filter(file => /\.(jpg|jpeg|png)$/i.test(file)); res.json({ expDir, totalFiles: files.length, imageFiles: imageFiles, staticPath: '/detected/exp/' }); } else { res.json({ error: 'Exp directory does not exist', expDir }); } } catch (error) { res.json({ error: error.message, expDir }); } }); app.post('/upload', upload.single('image'), async (req, res) => { try { if (!req.file) { return res.status(400).json({ error: 'No image file uploaded' }); } const uploadedImagePath = req.file.path; console.log('Image uploaded:', uploadedImagePath); // Run YOLOv5 detection const detectionResult = await runYOLOv5Detection(uploadedImagePath); if (detectionResult.success) { res.json({ success: true, originalImage: `/uploads/${path.basename(uploadedImagePath)}`, detectedImage: detectionResult.detectedImagePath, message: 'Object detection completed successfully' }); } else { res.status(500).json({ success: false, error: detectionResult.error }); } } catch (error) { console.error('Error processing upload:', error); res.status(500).json({ success: false, error: 'Error processing image upload' }); } }); // Function to run YOLOv5 detection using Ultralytics API async function runYOLOv5Detection(imagePath) { return new Promise((resolve) => { const yolov5Path = path.join(__dirname, 'yolo_detect.py'); const weightsPath = '/tmp/yolov8s.pt'; console.log('Calling yolo_detect.py with weights:', weightsPath, 'and image:', imagePath); const pythonProcess = spawn('python', [ yolov5Path, weightsPath, imagePath, ]); let output = ''; let errorOutput = ''; pythonProcess.stdout.on('data', (data) => { output += data.toString(); console.log('PYTHON STDOUT:', data.toString()); }); pythonProcess.stderr.on('data', (data) => { errorOutput += data.toString(); console.error('PYTHON STDERR:', data.toString()); }); pythonProcess.on('close', (code) => { console.log('Python process exited with code:', code); if (code === 0) { // Extract the last non-empty line from output as the detected image path const lines = output.trim().split('\n'); const lastLine = lines.reverse().find(line => line.trim().endsWith('.jpg')); let detectedImageUrl = null; if (lastLine) { const detectedImageFilename = path.basename(lastLine); detectedImageUrl = `/results/predict/${detectedImageFilename}`; } resolve({ success: true, detectedImagePath: detectedImageUrl, }); } else { resolve({ success: false, error: errorOutput || `YOLOv5 detection failed with code ${code}`, }); } }); }); } // Function to find the detected image function findDetectedImage(expDir, originalImageName) { try { if (!fs.existsSync(expDir)) { console.log('Exp directory does not exist:', expDir); return null; } const files = fs.readdirSync(expDir); console.log('Files in exp directory:', files); // Look for image files (jpg, jpeg, png) const imageFiles = files.filter(file => /\.(jpg|jpeg|png)$/i.test(file)); // Try to find the detected image with the same name as the uploaded file const detectedFile = imageFiles.find(file => file === originalImageName); if (detectedFile) { const detectedImagePath = path.join(expDir, detectedFile); console.log('Found detected image:', detectedImagePath); return detectedImagePath; } // Fallback: return the first image if not found (optional) if (imageFiles.length > 0) { const detectedImagePath = path.join(expDir, imageFiles[0]); console.log('Fallback detected image:', detectedImagePath); return detectedImagePath; } return null; } catch (error) { console.error('Error finding detected image:', error); return null; } } // Error handling middleware app.use((error, req, res, next) => { if (error instanceof multer.MulterError) { if (error.code === 'LIMIT_FILE_SIZE') { return res.status(400).json({ error: 'File too large. Maximum size is 10MB.' }); } } console.error('Server error:', error); res.status(500).json({ error: 'Internal server error' }); }); // Start server app.listen(PORT, () => { console.log(`Server running on port ${PORT}`); console.log(`Upload directory: ${path.join(__dirname, 'uploads')}`); console.log(`Detected images served from: ${path.join(__dirname, '../runs/detect')}`); console.log(`Static file serving configured for /detected -> ${path.join(__dirname, '../runs/detect')}`); });