πΎ YOLO v7 vs YOLO v8
YOLO V7 vs V8 Comparison
Comparison Videos
Video 1: Performance Comparison
Video 2: Detection Accuracy Test
Video 3: Real-world Scenarios
Video 4: Speed Benchmark
Overview
This is a comprehensive comparison between YOLO v7 and v8, testing performance, accuracy, and speed across multiple real-world scenarios.
Model Information
π· YOLO V7
- GitHub: https://github.com/WongKinYiu/yolov7
- Model: yolov7x.pt
- Release: 2022
πΆ YOLO V8
- GitHub: https://github.com/ultralytics/ultralytics
- Model: yolov8x.pt
- Release: 2023
Testing Environment
| Component | Specification |
|---|---|
| CPU | Intel Core i7-9800X @ 3.80GHz |
| GPU | NVIDIA RTX 4090 |
π» Source Code
π Example 1: YOLOv8 Video Processing with Real-time Detection and FPS Display
π yolov8_video_processor.py
"""
YOLOv8 Video Processing Script
Process videos with object detection and save annotated results
"""
import cv2
import time
from ultralytics import YOLO
def process_video(model, video_path, output_path):
"""
Process video with YOLOv8 detection and save annotated results
Args:
model (YOLO): YOLO model instance
video_path (str): Path to input video
output_path (str): Path to save output video
"""
# Open video capture
cap = cv2.VideoCapture(video_path)
# Get video properties
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap.get(cv2.CAP_PROP_FPS))
print(f"πΉ Video: {video_path}")
print(f" Resolution: {width}x{height}")
print(f" FPS: {fps}")
# Create VideoWriter object to save annotated video
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
frame_count = 0
total_processing_time = 0
while cap.isOpened():
success, frame = cap.read()
if success:
# Measure processing time
start_time = time.time()
results = model(frame)
end_time = time.time()
processing_time = end_time - start_time
fps_realtime = 1 / processing_time
# Accumulate statistics
frame_count += 1
total_processing_time += processing_time
# Visualize results on frame
annotated_frame = results[0].plot()
# Display metrics on frame
text = f"Time: {processing_time:.3f}s | FPS: {fps_realtime:.1f}"
cv2.putText(
annotated_frame,
text,
(10, 30),
cv2.FONT_HERSHEY_SIMPLEX,
0.8,
(0, 255, 0),
2,
cv2.LINE_AA
)
# Write annotated frame to output
out.write(annotated_frame)
# Optional: Display real-time preview
# cv2.imshow("YOLOv8 Detection", annotated_frame)
# if cv2.waitKey(1) & 0xFF == ord("q"):
# break
else:
break
# Cleanup
cap.release()
out.release()
# Print summary statistics
avg_fps = frame_count / total_processing_time if total_processing_time > 0 else 0
print(f"β
Processed {frame_count} frames")
print(f" Average FPS: {avg_fps:.2f}")
print(f" Saved to: {output_path}\n")
def main():
"""Main function to process multiple videos"""
print("=" * 60)
print("π YOLOv8 Video Processing Pipeline")
print("=" * 60 + "\n")
# Load YOLOv8 model
print("π¦ Loading YOLOv8 model...")
model = YOLO('yolov8x.pt')
print("β
Model loaded successfully!\n")
# List of video files to process
video_paths = [
"../video/videoplayback-1.mp4",
"../video/videoplayback-2.mp4",
"../video/videoplayback-3.mp4",
"../video/videoplayback-4.mp4",
]
# Process each video
total_videos = len(video_paths)
for i, video_path in enumerate(video_paths, 1):
output_path = f"../video/yolo_v8_output_{i}.mp4"
print(f"[{i}/{total_videos}] Processing video...")
process_video(model, video_path, output_path)
cv2.destroyAllWindows()
print("=" * 60)
print("π All videos processed successfully!")
print("=" * 60)
if __name__ == '__main__':
main()
π Example 2: Combine Two Videos Side-by-Side for V7 vs V8 Comparison
π combine_videos_comparison.py
"""
Side-by-Side Video Comparison Tool
Combine YOLOv7 and YOLOv8 results for visual comparison
"""
import cv2
import numpy as np
def combine_videos_side_by_side(video1_path, video2_path, output_path,
label1="YOLOv7", label2="YOLOv8"):
"""
Combine two videos side by side for comparison
Args:
video1_path (str): Path to first video (e.g., YOLOv7 results)
video2_path (str): Path to second video (e.g., YOLOv8 results)
output_path (str): Path to save combined video
label1 (str): Label for first video
label2 (str): Label for second video
"""
# Open video captures
cap1 = cv2.VideoCapture(video1_path)
cap2 = cv2.VideoCapture(video2_path)
# Get video properties
width = int(cap1.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap1.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = int(cap1.get(cv2.CAP_PROP_FPS))
print(f"πΉ Combining videos:")
print(f" Video 1: {video1_path}")
print(f" Video 2: {video2_path}")
print(f" Output size: {width * 2}x{height}")
# Create VideoWriter for combined output (double width)
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
out = cv2.VideoWriter(output_path, fourcc, fps, (width * 2, height))
frame_count = 0
while True:
ret1, frame1 = cap1.read()
ret2, frame2 = cap2.read()
if not ret1 or not ret2:
break
# Combine frames horizontally
combined_frame = np.hstack((frame1, frame2))
# Add labels with background for better visibility
# Label 1 (left side)
cv2.rectangle(combined_frame, (5, 5), (200, 45), (0, 0, 0), -1)
cv2.putText(
combined_frame,
label1,
(10, 35),
cv2.FONT_HERSHEY_SIMPLEX,
1.2,
(0, 255, 0),
2,
cv2.LINE_AA
)
# Label 2 (right side)
cv2.rectangle(combined_frame, (width + 5, 5), (width + 200, 45), (0, 0, 0), -1)
cv2.putText(
combined_frame,
label2,
(width + 10, 35),
cv2.FONT_HERSHEY_SIMPLEX,
1.2,
(255, 0, 0),
2,
cv2.LINE_AA
)
# Add divider line
cv2.line(
combined_frame,
(width, 0),
(width, height),
(255, 255, 255),
2
)
out.write(combined_frame)
frame_count += 1
# Optional: show progress every 100 frames
if frame_count % 100 == 0:
print(f" Processed {frame_count} frames...")
# Cleanup
cap1.release()
cap2.release()
out.release()
print(f"β
Combined {frame_count} frames")
print(f" Saved to: {output_path}\n")
def main():
"""Main function to create comparison videos"""
print("=" * 60)
print("π¬ Video Comparison Tool - YOLOv7 vs YOLOv8")
print("=" * 60 + "\n")
# Define video pairs to compare
comparisons = [
{
'v7': "../video/yolo_v7_output_1.mp4",
'v8': "../video/yolo_v8_output_1.mp4",
'output': "../video/comparison_v7_vs_v8_test1.mp4"
},
{
'v7': "../video/yolo_v7_output_2.mp4",
'v8': "../video/yolo_v8_output_2.mp4",
'output': "../video/comparison_v7_vs_v8_test2.mp4"
},
]
# Process each comparison
for i, comp in enumerate(comparisons, 1):
print(f"[{i}/{len(comparisons)}] Creating comparison video...")
combine_videos_side_by_side(
comp['v7'],
comp['v8'],
comp['output'],
label1="YOLOv7",
label2="YOLOv8"
)
print("=" * 60)
print("π All comparison videos created successfully!")
print("=" * 60)
if __name__ == '__main__':
main()
Key Features
- β Real-time FPS display on processed videos
- β Batch processing for multiple videos
- β Side-by-side comparison visualization
- β Progress tracking and statistics
- β Clean, well-documented code
Comparison Metrics
- π Speed: Inference time and FPS performance
- π― Accuracy: Detection precision and recall
- π¦ Model Size: yolov7x.pt vs yolov8x.pt
- π‘ Ease of Use: API and implementation differences
- π Real-world Performance: Various scenarios tested
Resources
- π YOLO V7 Official Repository
- π YOLO V8 Official Repository (Ultralytics)
- π Video Combination Tutorial
Thank you! πΊ
Β
Main picture by Matheo JBT