Skip to content
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 35 additions & 0 deletions android/src/main/kotlin/com/ultralytics/yolo/YOLOView.kt
Original file line number Diff line number Diff line change
Expand Up @@ -1712,6 +1712,41 @@ class YOLOView @JvmOverloads constructor(
map["detections"] = detections
Log.d(TAG, "✅ Total detections in stream: ${detections.size} (boxes: ${result.boxes.size}, obb: ${result.obb.size})")
}

// Add classification results (if available and enabled for CLASSIFY task)
if (config.includeClassifications && result.probs != null) {
val probs = result.probs!!
Log.d(TAG, "🎯 Processing CLASSIFY result: top1Label=${probs.top1Label}, conf=${probs.top1Conf}, index=${probs.top1Index}")

// Add classification result to detections array (for compatibility with YOLOResult.fromMap)
val detections = map["detections"] as? ArrayList<Map<String, Any>> ?: ArrayList()

val classificationDetection = HashMap<String, Any>()
classificationDetection["classIndex"] = probs.top1Index
classificationDetection["className"] = probs.top1Label
classificationDetection["confidence"] = probs.top1Conf.toDouble()

// Full image bounding box for classification
val boundingBox = HashMap<String, Any>()
boundingBox["left"] = 0.0
boundingBox["top"] = 0.0
boundingBox["right"] = result.origShape.width.toDouble()
boundingBox["bottom"] = result.origShape.height.toDouble()
classificationDetection["boundingBox"] = boundingBox

// Normalized bounding box (full image)
val normalizedBox = HashMap<String, Any>()
normalizedBox["left"] = 0.0
normalizedBox["top"] = 0.0
normalizedBox["right"] = 1.0
normalizedBox["bottom"] = 1.0
classificationDetection["normalizedBox"] = normalizedBox

detections.add(classificationDetection)
map["detections"] = detections

Log.d(TAG, "✅ Added classification result: ${probs.top1Label} (conf=${probs.top1Conf}, index=${probs.top1Index})")
}

// Add performance metrics (if enabled)
if (config.includeProcessingTimeMs) {
Expand Down
49 changes: 49 additions & 0 deletions ios/Classes/YOLOView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -1768,6 +1768,55 @@ extension YOLOView: AVCapturePhotoCaptureDelegate {
map["detections"] = detections
}

// Add classification results (if available and enabled for CLASSIFY task)
if config.includeClassifications, let probs = result.probs, result.boxes.isEmpty {
print("🎯 YOLOView: Processing CLASSIFY result - returning top 5 predictions")

// Get or create detections array (for compatibility with YOLOResult deserialization)
var detections = map["detections"] as? [[String: Any]] ?? []

// For classification models, create detections from top5 predictions
let top5Labels = probs.top5Labels
let top5Confs = probs.top5Confs

for i in 0..<min(top5Labels.count, top5Confs.count) {
var detection: [String: Any] = [:]

// Find class index from names array
var classIndex = i
if let index = result.names.firstIndex(of: top5Labels[i]) {
classIndex = index
}

detection["classIndex"] = classIndex
detection["className"] = top5Labels[i]
detection["confidence"] = Double(top5Confs[i])

// Classification doesn't have bounding boxes, use full image bounds
let boundingBox: [String: Any] = [
"left": 0.0,
"top": 0.0,
"right": Double(result.orig_shape.width),
"bottom": Double(result.orig_shape.height)
]
detection["boundingBox"] = boundingBox

// Normalized bounding box (full image)
let normalizedBox: [String: Any] = [
"left": 0.0,
"top": 0.0,
"right": 1.0,
"bottom": 1.0
]
detection["normalizedBox"] = normalizedBox

detections.append(detection)
}

map["detections"] = detections
print("✅ YOLOView: Added \(detections.count) classification results (top5)")
}

// Add performance metrics (if enabled)
if config.includeProcessingTimeMs {
map["processingTimeMs"] = result.speed * 1000
Expand Down
Loading