From 04add754b30c4f546d53453057b2f7719f8272f7 Mon Sep 17 00:00:00 2001 From: Ryan Date: Mon, 26 Jun 2023 16:32:02 +0800 Subject: [PATCH] Mon Jun 26 04:32:02 PM CST 2023 --- .gitignore | 1 + demo.py | 53 ++++++++++++++++++++++++++++++++++++++++++++++++++ torch_check.py | 3 +++ 3 files changed, 57 insertions(+) create mode 100644 .gitignore create mode 100644 demo.py create mode 100644 torch_check.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..b46291c --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +virtualenv/ diff --git a/demo.py b/demo.py new file mode 100644 index 0000000..a954e20 --- /dev/null +++ b/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/torch_check.py b/torch_check.py new file mode 100644 index 0000000..8aa6f3d --- /dev/null +++ b/torch_check.py @@ -0,0 +1,3 @@ +import torch +print(torch.__version__) +print(torch.cuda.is_available()) \ No newline at end of file