From f67d647b1a2009f903da5df7aef11efe98da2f79 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 26 Jun 2023 17:18:00 +0800 Subject: [PATCH] Mon Jun 26 05:18:00 PM CST 2023 --- example/demo.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++ example/train.py | 8 ++++++++ train.py | 0 3 files changed, 61 insertions(+) create mode 100644 example/demo.py create mode 100644 example/train.py delete mode 100644 train.py diff --git a/example/demo.py b/example/demo.py new file mode 100644 index 0000000..a954e20 --- /dev/null +++ b/example/demo.py @@ -0,0 +1,53 @@ +from ultralytics import YOLO +# from ultralytics.yolo.utils.benchmarks import benchmark +import cv2 + +# Load a model +# model = YOLO("yolov8n.yaml") # build a new model from scratch +model = YOLO("yolov8n.pt") # load a pretrained model (recommended for training) + +# Use the model +# model.train(data="coco128.yaml", epochs=3,workers=0) # train the model,workers=0 if windows +# metrics = model.val() # evaluate model performance on the validation set +''' +results = model("bus.jpg") # predict on an image +print(results) +# ''' +img_path = "./image/" +# results = model.predict(img_path, save=True,conf=0.5) # device=0 by default, conf:置信度阈值 +# results = model.predict(img_path,save=True,classes=[0,2],conf=0.5) # i.e. classes=0,classes=[0,3,4] + +# save detection results * +# results = model.predict(img_path,save=True,save_txt=True,classes=0,conf=0.4) + + + +# predict video +video_path = "./video/1.mp4" +cap = cv2.VideoCapture(0) + +# 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() diff --git a/example/train.py b/example/train.py new file mode 100644 index 0000000..8635350 --- /dev/null +++ b/example/train.py @@ -0,0 +1,8 @@ +from ultralytics import YOLO + +# Load a model +model = YOLO('yolov8n.pt') # load a pretrained model (recommended for training) + +# Train the model +model.train(data='coco128.yaml', epochs=100, imgsz=640) + diff --git a/train.py b/train.py deleted file mode 100644 index e69de29..0000000