|
1 | | -import cv2 |
2 | | -import numpy as np |
3 | | -import os |
4 | | -from datetime import datetime |
5 | | -from ultralytics import YOLO |
6 | | - |
7 | 1 | # Created by Coder Shiyar | https://github.com/codershiyar | https://codershiyar.com |
8 | 2 |
|
9 | | -# Function to detect objects using YOLOv8 |
10 | | -def detect_objects(model, image): |
11 | | - # Example options (adjust as needed) |
12 | | - options = { |
13 | | - 'conf': 0.5, # Confidence threshold |
14 | | - 'iou': 0.4, # NMS threshold |
15 | | - 'imgsz': 640, # Model input resolution |
16 | | - } |
17 | | - results = model.track(image, **options) |
18 | | - |
19 | | - return results |
20 | | - |
21 | | -# Function to save image |
22 | | -def save_image(image, directory='captures'): |
23 | | - if not os.path.exists(directory): |
24 | | - os.makedirs(directory) |
25 | | - filename = datetime.now().strftime('%Y-%m-%d_%H-%M-%S') + '_detected.jpg' |
26 | | - filepath = os.path.join(directory, filename) |
27 | | - cv2.imwrite(filepath, image) |
28 | | - print("Image saved as:", filepath) |
29 | | - |
30 | | -# Main function |
31 | | -def main(): |
32 | | - # Load YOLOv8 model |
33 | | - yolo_model = return YOLO('selectyourmodelhere.pt') |
34 | | - |
35 | | - # Initialize webcam |
36 | | - webcam = cv2.VideoCapture(0) |
| 3 | +import cv2 |
| 4 | +from ultralytics import YOLO |
37 | 5 |
|
38 | | - # Main loop |
39 | | - while True: |
40 | | - # Capture frame from webcam |
41 | | - ret, frame = webcam.read() |
42 | | - if not ret: |
43 | | - print("Failed to capture frame") |
44 | | - break |
| 6 | +# Load YOLO model |
| 7 | +model = YOLO('yolov8n.pt') |
45 | 8 |
|
46 | | - # Detect objects in the frame |
47 | | - results = detect_objects(yolo_model, frame) |
| 9 | +# Initialize webcam |
| 10 | +webcam = cv2.VideoCapture(0) |
48 | 11 |
|
49 | | - # Draw bounding boxes and labels |
50 | | - for result in results: |
51 | | - cv2.putText(frame, f"Total:{len(result.boxes)}" , (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) |
| 12 | +while True: |
| 13 | + # Capture frame from webcam |
| 14 | + ret, frame = webcam.read() |
52 | 15 |
|
53 | | - # Display the frame |
54 | | - cv2.imshow('YOLOv8 Object Detection', results[0].plot()) |
| 16 | + # Check if frame is captured successfully |
| 17 | + if not ret: |
| 18 | + print("Failed to capture frame") |
| 19 | + break |
55 | 20 |
|
56 | | - # Check for user input |
57 | | - key = cv2.waitKey(1) |
58 | | - if key == ord('s'): |
59 | | - save_image(frame) # Save image when 's' key is pressed |
60 | | - elif key == 27: # ESC key |
61 | | - break |
| 21 | + # Detect objects in the frame |
| 22 | + results = model.track(frame, conf=0.5, imgsz=480, classes=0) |
62 | 23 |
|
63 | | - # Release resources |
64 | | - webcam.release() |
65 | | - cv2.destroyAllWindows() |
| 24 | + # Display total number of detected objects and bounding boxes |
| 25 | + for result in results: |
| 26 | + cv2.putText(frame, f"Total: {len(result.boxes)}", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA) |
| 27 | + cv2.imshow('YOLOv8 Object Detection', result.plot()) |
66 | 28 |
|
67 | | -if __name__ == "__main__": |
68 | | - main() |
| 29 | + # Exit loop if ESC key is pressed |
| 30 | + if cv2.waitKey(1) == 27: |
| 31 | + break |
69 | 32 |
|
| 33 | +# Release webcam and close OpenCV windows |
| 34 | +webcam.release() |
| 35 | +cv2.destroyAllWindows() |
70 | 36 |
|
71 | 37 |
|
72 | 38 |
|
73 | 39 |
|
74 | 40 |
|
| 41 | +# For Realsense camera |
75 | 42 | # def initialize_realsense(): |
76 | 43 | # import pyrealsense2 as rs |
77 | 44 | # pipeline = rs.pipeline() |
|
0 commit comments