/** * QUIXZOOM AI Detection API * * Endpoint för objektdetektering i bilder och video. * Stödjer: YOLO, Cloud Vision, Custom Models */ const express = require('express'); const router = express.Router(); const YOLODetector = require('../video-to-observation/yolo-detector'); const CloudVisionDetector = require('../video-to-observation/cloud-vision'); // Initiera detektorer const yolo = new YOLODetector(); const cloudVision = new CloudVisionDetector(); /** * POST /api/detect * Detektera objekt i bild */ router.post('/detect', async (req, res) => { try { const { imageUrl, model = 'yolo', confidence = 0.5 } = req.body; if (!imageUrl) { return res.status(400).json({ error: 'imageUrl required' }); } let detections; switch (model) { case 'yolo': detections = await yolo.detect(imageUrl); break; case 'cloud-vision': detections = await cloudVision.detect(imageUrl); break; default: return res.status(400).json({ error: 'Unknown model' }); } // Filtrera på confidence const filtered = detections.filter(d => d.confidence >= confidence); res.json({ success: true, model, detections: filtered, count: filtered.length, }); } catch (error) { res.status(500).json({ success: false, error: error.message, }); } }); /** * POST /api/detect/batch * Batch-detektion på flera bilder */ router.post('/detect/batch', async (req, res) => { try { const { imageUrls, model = 'yolo' } = req.body; if (!imageUrls || !Array.isArray(imageUrls)) { return res.status(400).json({ error: 'imageUrls array required' }); } const detector = model === 'cloud-vision' ? cloudVision : yolo; const results = await detector.detectBatch(imageUrls); res.json({ success: true, model, results, totalDetections: results.reduce((sum, r) => sum + r.objects.length, 0), }); } catch (error) { res.status(500).json({ success: false, error: error.message, }); } }); /** * GET /api/models * Lista tillgängliga modeller */ router.get('/models', (req, res) => { res.json({ models: [ { id: 'yolo', name: 'YOLOv8', description: 'Fast local detection', available: yolo.available, classes: ['traffic_sign', 'tree', 'bench'], }, { id: 'cloud-vision', name: 'Google Cloud Vision', description: 'High accuracy cloud detection', available: cloudVision.available, classes: ['street_lamp', 'traffic_sign', 'tree', 'manhole', 'utility_box', 'bench'], }, { id: 'custom-yolo', name: 'Custom YOLO (QUIXZOOM)', description: 'Trained on infrastructure objects', available: false, classes: ['street_lamp', 'traffic_sign', 'tree', 'manhole', 'utility_box', 'bench'], }, ], }); }); module.exports = router;