Skip to content

Commit 5342e1b

Browse files
authored
Update app.py
1 parent 28ce359 commit 5342e1b

File tree

1 file changed

+26
-59
lines changed

1 file changed

+26
-59
lines changed

app.py

Lines changed: 26 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,44 @@
1-
import cv2
2-
import numpy as np
3-
import os
4-
from datetime import datetime
5-
from ultralytics import YOLO
6-
71
# Created by Coder Shiyar | https://github.com/codershiyar | https://codershiyar.com
82

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
375

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')
458

46-
# Detect objects in the frame
47-
results = detect_objects(yolo_model, frame)
9+
# Initialize webcam
10+
webcam = cv2.VideoCapture(0)
4811

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()
5215

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
5520

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)
6223

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())
6628

67-
if __name__ == "__main__":
68-
main()
29+
# Exit loop if ESC key is pressed
30+
if cv2.waitKey(1) == 27:
31+
break
6932

33+
# Release webcam and close OpenCV windows
34+
webcam.release()
35+
cv2.destroyAllWindows()
7036

7137

7238

7339

7440

41+
# For Realsense camera
7542
# def initialize_realsense():
7643
# import pyrealsense2 as rs
7744
# pipeline = rs.pipeline()

0 commit comments

Comments
 (0)