bichuche0705 commited on
Commit
836049a
·
verified ·
1 Parent(s): 498fcaf

Upload example_usage.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. example_usage.py +98 -0
example_usage.py ADDED
@@ -0,0 +1,98 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/env python3
2
+ """
3
+ Simple example script for using the Highway Vehicle Detection model.
4
+ This script demonstrates basic usage of the trained YOLOv8 model.
5
+ """
6
+
7
+ from ultralytics import YOLO
8
+ import cv2
9
+ import os
10
+
11
+ def detect_vehicles(image_path, model_path="models/yolov8m_stage2_improved_best.pt"):
12
+ """
13
+ Detect vehicles in an image using the trained model.
14
+
15
+ Args:
16
+ image_path (str): Path to the input image
17
+ model_path (str): Path to the trained model
18
+
19
+ Returns:
20
+ results: YOLO detection results
21
+ """
22
+ # Load the trained model
23
+ model = YOLO(model_path)
24
+
25
+ # Run inference
26
+ results = model(image_path)
27
+
28
+ # Print detection results
29
+ for result in results:
30
+ boxes = result.boxes
31
+ if boxes is not None:
32
+ print(f"\nDetected {len(boxes)} vehicles:")
33
+ for i, box in enumerate(boxes):
34
+ x1, y1, x2, y2 = box.xyxy[0]
35
+ conf = box.conf[0]
36
+ cls = int(box.cls[0])
37
+ class_name = model.names[cls]
38
+ print(f" {i+1}. {class_name} (confidence: {conf:.2f})")
39
+ else:
40
+ print("No vehicles detected.")
41
+
42
+ return results
43
+
44
+ def process_video(video_path, output_path=None, model_path="models/yolov8m_stage2_improved_best.pt"):
45
+ """
46
+ Process a video file and save results.
47
+
48
+ Args:
49
+ video_path (str): Path to the input video
50
+ output_path (str): Path to save the output video (optional)
51
+ model_path (str): Path to the trained model
52
+ """
53
+ # Load the trained model
54
+ model = YOLO(model_path)
55
+
56
+ # Process video
57
+ if output_path:
58
+ results = model(video_path, save=True, project="output", name="detection")
59
+ print(f"Results saved to: output/detection/")
60
+ else:
61
+ results = model(video_path)
62
+
63
+ return results
64
+
65
+ def main():
66
+ """Main function with example usage."""
67
+ print("Highway Vehicle Detection - Example Usage")
68
+ print("=" * 50)
69
+
70
+ # Check if model exists
71
+ model_path = "models/yolov8m_stage2_improved_best.pt"
72
+ if not os.path.exists(model_path):
73
+ print(f"Error: Model file not found at {model_path}")
74
+ print("Please make sure you've downloaded the model files.")
75
+ return
76
+
77
+ # Example 1: Process an image
78
+ print("\n1. Image Detection Example:")
79
+ print(" To detect vehicles in an image:")
80
+ print(" results = detect_vehicles('path/to/image.jpg')")
81
+
82
+ # Example 2: Process a video
83
+ print("\n2. Video Processing Example:")
84
+ print(" To process a video:")
85
+ print(" results = process_video('path/to/video.mp4', 'output.mp4')")
86
+
87
+ # Example 3: Direct model usage
88
+ print("\n3. Direct Model Usage:")
89
+ print(" from ultralytics import YOLO")
90
+ print(" model = YOLO('models/yolov8m_stage2_improved_best.pt')")
91
+ print(" results = model('path/to/image.jpg')")
92
+ print(" results[0].show()")
93
+
94
+ print("\n" + "=" * 50)
95
+ print("For more advanced features, use main.py")
96
+
97
+ if __name__ == "__main__":
98
+ main()