-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcamera_code.py
More file actions
43 lines (31 loc) · 1.13 KB
/
camera_code.py
File metadata and controls
43 lines (31 loc) · 1.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# This is a python code for using the model on vs code in a local pc using the laptop camera "live detecting"
import cv2
from ultralytics import YOLO
# Load the YOLOv8 model
model = YOLO('E:\\SEMESTER 8\\Neural Networks\\best (2).pt')
# Open the default camera (camera index 0)
cap = cv2.VideoCapture(0)
# Check if the camera opened successfully
if not cap.isOpened():
print("Error: Could not open camera.")
exit()
# Loop through the camera frames
while True:
# Read a frame from the camera
success, frame = cap.read()
if success:
# Run YOLOv8 inference on the frame
results = model(frame)
# Visualize the results on the frame
annotated_frame = results[0].plot()
# Display the annotated frame
cv2.imshow("YOLOv8 Inference", annotated_frame)
# Break the loop if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord("q"):
break
else:
print("Error: Could not read frame from camera.")
break
# Release the camera capture object and close the display window
cap.release()
cv2.destroyAllWindows()