import cv2
from ultralytics import YOLO
# 加载 YOLOv8 模型
# model = YOLO("ultralytics/weights/yolov8s.engine")
model = YOLO("ultralytics/weights/yolov8s.pt")
# 获取摄像头内容,参数 0 表示使用默认的摄像头
cap = cv2.VideoCapture(6)
# Loop through the video frames
while cap.isOpened():
# Read a frame from the video
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:
# Break the loop if the end of the video is reached
break
# Release the video capture object and close the display window
cap.release()
cv2.destroyAllWindows()
import cv2
from ultralytics import YOLO
# 加载 YOLOv8 模型
# model = YOLO("ultralytics/weights/yolov8s.engine")
# model = YOLO("ultralytics/weights/yolov8s.pt")
model = YOLO("ultralytics/weights/yolov8n-seg.pt")
frame_path = "images/0000.png"
results = model(frame_path,conf=0.3,classes=39)
for result in results:
boxes = result.boxes
print(boxes)
masks = result.masks.data
print(masks)
annotated_frame = results[0].plot()
# cv2.imshow("YOLOv8 Inference", annotated_frame)
cv2.imwrite("0000.png", annotated_frame)