当前位置:   article > 正文

YOLOv8检测图片和视频_yolov8视频检测

yolov8视频检测

一、检测图片

Python

  1. import cv2
  2. from ultralytics import YOLO
  3. import torch
  4. model_path = 'object_detection/best.pt' # Change this to your YOLOv8 model's path
  5. image_path = 'object_detection/32.jpg' # Change this to your video's path
  6. # Load the trained YOLOv8 model
  7. model = YOLO(model_path)
  8. device = 'cuda' if torch.cuda.is_available() else 'cpu'
  9. print("Using device: %s" % device)
  10. model.to(device)
  11. # Process video frames
  12. image = cv2.imread(image_path)
  13. width, height, _ = image.shape
  14. new_shape = [32*int(height/128), 32*int(width/128)]
  15. image = cv2.resize(image, new_shape)
  16. with torch.no_grad():
  17. results = model.predict(image)
  18. for result in results:
  19. for box in result.boxes:
  20. x1, y1, x2, y2 = box.xyxy[0]
  21. # Draw the bounding box on the BGR frame
  22. cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
  23. # Add a label above the box
  24. cv2.putText(image, result.names[int(box.cls)], (int(x1) - 30, int(y1) + 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (255, 0, 0), 2)
  25. cv2.imshow('Video', image)
  26. cv2.waitKey(0)
  27. cv2.destroyAllWindows()

二、检测视频

Python

  1. import cv2
  2. from ultralytics import YOLO
  3. import torch
  4. model_path = 'object_detection/best.pt' # Change this to your YOLOv8 model's path
  5. video_path = 'object_detection/物块.mp4' # Change this to your video's path
  6. # Load the trained YOLOv8 model
  7. model = YOLO(model_path)
  8. device = 'cuda' if torch.cuda.is_available() else 'cpu'
  9. print("Using device: %s" % device)
  10. model.to(device)
  11. batch_size = 8
  12. frames_rgb = []
  13. frames = []
  14. cap = cv2.VideoCapture(video_path)
  15. if not cap.isOpened():
  16. print("Error: Could not open video.")
  17. exit()
  18. # Process video frames
  19. while True:
  20. ret, frame = cap.read()
  21. if not ret:
  22. print("Finished processing video.")
  23. break
  24. width, height, _ = frame.shape
  25. new_shape = [32*int(height/64), 32*int(width/64)]
  26. frame = cv2.resize(frame, new_shape)
  27. frames.append(frame)
  28. # YOLOv8 expects RGB images
  29. if len(frames) == batch_size:
  30. with torch.no_grad():
  31. results = model.predict(frames)
  32. # Process each detection
  33. for i, result in enumerate(results):
  34. for box in result.boxes:
  35. print(box.conf)
  36. if float(box.conf) > 0.9:
  37. x1, y1, x2, y2 = box.xyxy[0]
  38. # Draw the bounding box on the BGR frame
  39. cv2.rectangle(frames[i], (int(x1), int(y1)), (int(x2), int(y2)), (0, 255, 0), 2)
  40. # Add a label above the box
  41. cv2.putText(frames[i], result.names[int(box.cls)], (int(x1), int(y1) - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)
  42. cv2.imshow('Video', frames[i])
  43. if cv2.waitKey(1) & 0xFF == ord('q'):
  44. cap.release()
  45. cv2.destroyAllWindows()
  46. exit()
  47. frames.clear()
  48. frames_rgb.clear()
  49. cap.release()
  50. cv2.destroyAllWindows()

使用了sahi的视频检测

  1. import argparse
  2. import sys
  3. import cv2
  4. from sahi import AutoDetectionModel
  5. from sahi.predict import get_sliced_prediction
  6. import imageio
  7. import numpy as np
  8. def run(weights="yolov8n.pt", source="test.mp4", view_img=False):
  9. """
  10. Run object detection on a video using YOLOv8 and SAHI.
  11. Args:
  12. weights (str): Model weights path.
  13. source (str): Video file path.
  14. view_img (bool): Show results.
  15. """
  16. yolov8_model_path = weights
  17. detection_model = AutoDetectionModel.from_pretrained(
  18. model_type="yolov8", model_path=yolov8_model_path, confidence_threshold=0.3, device="cuda:0"
  19. )
  20. videocapture = cv2.VideoCapture(0)
  21. new_shape = 32 * int(videocapture.get(3) / 64), 32 * int(videocapture.get(4) / 64)
  22. writer = imageio.get_writer("object_detection/object_detection.mp4", fps=1 / 0.025)
  23. while videocapture.isOpened():
  24. success, frame = videocapture.read()
  25. if not success:
  26. break
  27. frame = cv2.resize(frame, new_shape)
  28. image = frame.copy()
  29. frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
  30. results = get_sliced_prediction(
  31. frame, detection_model, slice_height=512, slice_width=512, overlap_height_ratio=0.2, overlap_width_ratio=0.2
  32. )
  33. object_prediction_list = results.object_prediction_list
  34. boxes_list = []
  35. clss_list = []
  36. for ind, _ in enumerate(object_prediction_list):
  37. print(object_prediction_list[ind].score.value)
  38. if float(object_prediction_list[ind].score.value) > 0.85:
  39. boxes = (
  40. object_prediction_list[ind].bbox.minx,
  41. object_prediction_list[ind].bbox.miny,
  42. object_prediction_list[ind].bbox.maxx,
  43. object_prediction_list[ind].bbox.maxy,
  44. )
  45. clss = object_prediction_list[ind].category.name
  46. boxes_list.append(boxes)
  47. clss_list.append(clss)
  48. for box, cls in zip(boxes_list, clss_list):
  49. x1, y1, x2, y2 = box
  50. cv2.rectangle(image, (int(x1), int(y1)), (int(x2), int(y2)), (56, 56, 255), 2)
  51. label = str(cls)
  52. t_size = cv2.getTextSize(label, 0, fontScale=0.6, thickness=1)[0]
  53. cv2.rectangle(
  54. image, (int(x1), int(y1) - t_size[1] - 3), (int(x1) + t_size[0], int(y1) + 3), (56, 56, 255), -1
  55. )
  56. cv2.putText(
  57. image, label, (int(x1), int(y1) - 2), 0, 0.6, [255, 255, 255], thickness=1, lineType=cv2.LINE_AA
  58. )
  59. if view_img:
  60. cv2.imshow("result", image)
  61. frame = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
  62. writer.append_data((np.asarray(frame)).astype(np.uint8))
  63. if cv2.waitKey(1) == ord("q"):
  64. videocapture.release()
  65. cv2.destroyAllWindows()
  66. sys.exit()
  67. writer.close()
  68. def parse_opt():
  69. """Parse command line arguments."""
  70. parser = argparse.ArgumentParser()
  71. parser.add_argument("--weights", type=str, default="object_detection/best.pt", help="initial weights path")
  72. parser.add_argument("--source", type=str, default="object_detection/物块.mp4", help="video file path")
  73. parser.add_argument("--view-img", type=bool, default=True, help="show results")
  74. return parser.parse_args()
  75. def main(options):
  76. """Main function."""
  77. run(**vars(options))
  78. if __name__ == "__main__":
  79. opt = parse_opt()
  80. main(opt)

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/1015326
推荐阅读
相关标签
  

闽ICP备14008679号