You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何基于Darkflow Yolov2模型提取预测框参数并计算视频中框重叠度?

Hey there! Let's break down how to tackle your Darkflow YOLOv2 post-training tasks step by step. I’ve worked through similar workflows before, so here’s a practical, actionable guide:

1. Load Your Trained Model & Set Confidence Threshold

First, you’ll need to initialize Darkflow with your trained weights and configure the 0.5 confidence threshold directly in the setup. Here’s a quick snippet:

from darkflow.net.build import TFNet

# Configure paths and parameters to match your setup
options = {
    'model': 'cfg/yolov2.cfg',  # Path to your YOLOv2 config file
    'load': 'ckpt/yolov2-7-class.ckpt',  # Path to your trained weights
    'threshold': 0.5,  # Your desired confidence threshold
    'gpu': 0.5  # Adjust based on your GPU availability (set to 0 if no GPU)
}

# Initialize the TFNet object (your loaded model)
tfnet = TFNet(options)
2. Extract Detection Box Coordinates

Once the model is loaded, running detection on an image/frame will return a list of result dictionaries. Each dict contains all the info you need—including the bounding box coordinates.

Here’s how to pull out the x/y values:

import cv2

# Example: Read a single image (for video, we'll loop through frames later)
img = cv2.imread('test_image.jpg')
results = tfnet.return_predict(img)

# Iterate through all detected objects
for result in results:
    # Extract category and confidence score
    label = result['label']
    confidence = result['confidence']
    
    # Pull bounding box coordinates: top-left and bottom-right corners
    x1, y1 = result['topleft']['x'], result['topleft']['y']
    x2, y2 = result['bottomright']['x'], result['bottomright']['y']
    
    # Print or store the data as needed
    print(f"Class: {label}, Confidence: {confidence:.2f}, Bounds: ({x1},{y1}) -> ({x2},{y2})")
3. Calculate Overlap (IOU) Between Detection Boxes

To measure overlap between boxes, we use the Intersection over Union (IOU) metric. It’s the standard for this task—here’s a reusable function to compute it:

def calculate_iou(box1, box2):
    # Both boxes should be in (x1, y1, x2, y2) format
    x1_inter = max(box1[0], box2[0])
    y1_inter = max(box1[1], box2[1])
    x2_inter = min(box1[2], box2[2])
    y2_inter = min(box1[3], box2[3])
    
    # Calculate intersection area (handle cases where boxes don't overlap)
    inter_area = max(0, x2_inter - x1_inter) * max(0, y2_inter - y1_inter)
    
    # Calculate area of each box
    box1_area = (box1[2] - box1[0]) * (box1[3] - box1[1])
    box2_area = (box2[2] - box2[0]) * (box2[3] - box2[1])
    
    # Calculate union area
    union_area = box1_area + box2_area - inter_area
    
    # Compute IOU (avoid division by zero)
    iou = inter_area / union_area if union_area != 0 else 0.0
    return iou
4. Full Workflow for Video Processing

To apply this to a video, loop through each frame with OpenCV, run detection, extract boxes, and compute overlaps:

# Open your video file (use 0 instead of a path to use a webcam)
cap = cv2.VideoCapture('your_video.mp4')

while cap.isOpened():
    ret, frame = cap.read()
    if not ret:
        break  # Exit loop when video ends
    
    # Run detection on the current frame
    results = tfnet.return_predict(frame)
    
    # Convert results to a list of (x1,y1,x2,y2, label) tuples
    detected_boxes = []
    for res in results:
        x1 = res['topleft']['x']
        y1 = res['topleft']['y']
        x2 = res['bottomright']['x']
        y2 = res['bottomright']['y']
        detected_boxes.append((x1, y1, x2, y2, res['label']))
    
    # Calculate IOU for every pair of detected boxes
    for i in range(len(detected_boxes)):
        box1 = detected_boxes[i][:4]
        label1 = detected_boxes[i][4]
        for j in range(i + 1, len(detected_boxes)):
            box2 = detected_boxes[j][:4]
            label2 = detected_boxes[j][4]
            iou_score = calculate_iou(box1, box2)
            print(f"Overlap between {label1} and {label2}: {iou_score:.2f}")
    
    # Optional: Draw boxes and labels on the frame for visualization
    for res in results:
        x1, y1 = res['topleft']['x'], res['topleft']['y']
        x2, y2 = res['bottomright']['x'], res['bottomright']['y']
        cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
        cv2.putText(frame, f"{res['label']}: {res['confidence']:.2f}", 
                    (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
    
    # Display the frame
    cv2.imshow('YOLOv2 Detection', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break  # Press 'q' to quit early

# Clean up resources
cap.release()
cv2.destroyAllWindows()

Hope this helps you get up and running smoothly! If you hit snags like path errors or unexpected IOU values, double-check your config/weight paths or tweak the IOU function to fit edge cases in your data.

内容的提问来源于stack exchange,提问作者Brandon Speedster Loo

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 09:02:20