使用 YOLO 和 Webcam 实现实时目标检测

2024-11-19 08:55   重庆  

点击下方卡片,关注“机器视觉与AI深度学习

视觉/图像重干货,第一时间送达!

    对象检测已成为计算机视觉中越来越受欢迎的领域,YOLO(You Only Look Once)是使用最广泛的算法之一。在这篇博文中,我们将探讨如何使用 YOLO 和网络摄像头来开始使用实时对象检测系统。
    与需要对图像进行多次传递的传统对象检测算法不同,YOLO 在一次传递中处理整个图像,使其更快、更高效。
    YOLO 已用于各种应用,包括自动驾驶汽车、安全系统以及图像和视频分析。
首先,我们需要设置您的环境。我们需要一个安装了 OpenCV(一种流行的计算机视觉库)和 YOLO 的 Python 环境。

🌎 设置环境

    我们在其中创建一个文件main.py,用于导入初始要求(例如 Opencv),并准备好通过网络摄像头进行检测的基础。
    安装所有必要的依赖项,例如ultralytics,opencv-python,以及我们将在本博客后面介绍的其他依赖项。
pip install opencv-python
📷 从网络摄像头开始捕获
    我们将使用OpenCV从网络摄像头捕获帧。这可以使用 OpenCV 中的 VideoCapture 函数来完成。
import cv2
cap = cv2.VideoCapture(0)cap.set(3, 640)cap.set(4, 480)
while True: ret, img= cap.read() cv2.imshow('Webcam', img)
if cv2.waitKey(1) == ord('q'): break
cap.release()cv2.destroyAllWindows()
    在上面的代码中,我们创建了一个 VideoCapture 对象,并将其设置为从默认相机 (0) 捕获帧。它将网络摄像头的分辨率设置为 640x480。然后,我们遍历这些帧并将其显示在窗口中,直到用户按 'q' 退出。

使用ultralytics YOLO

    我们安装的ultralytics库使使用 YOLO 变得非常简单和轻松。
pip install ultralytics
  使用库加载 YOLO 模型,并指定 YOLO 权重文件在ultralyticsyolo-Weights/yolov8n.pt.
from ultralytics import YOLOmodel = YOLO("yolo-Weights/yolov8n.pt")
    我们实例化一个 classNames 变量,其中包含 YOLO 模型经过训练以检测的对象类列表。
classNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat",              "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat",              "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella",              "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat",              "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup",              "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",              "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed",              "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone",              "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors",              "teddy bear", "hair drier", "toothbrush"              ]
    while 循环启动,并使用cap.read()从网络摄像头读取每一帧。然后,它将帧传递给 YOLO 模型进行对象检测。对象检测的结果存储在 'results' 变量中。
import cv2
cap = cv2.VideoCapture(0)cap.set(3, 640)cap.set(4, 480)
while True: ret, img= cap.read() results = model(img, stream=True)
cv2.imshow('Webcam', frame)
if cv2.waitKey(1) == ord('q'): break
cap.release()cv2.destroyAllWindows()
    对于每个结果,代码会提取检测到的对象的边界框坐标,并使用cv2.rectangle()它还会在控制台上打印检测到的对象的置信度分数和类名称。
📃 完整代码 — 使用 YOLO 和网络摄像头进行对象检测
from ultralytics import YOLOimport cv2import math # start webcamcap = cv2.VideoCapture(0)cap.set(3, 640)cap.set(4, 480)
# modelmodel = YOLO("yolo-Weights/yolov8n.pt")
# object classesclassNames = ["person", "bicycle", "car", "motorbike", "aeroplane", "bus", "train", "truck", "boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench", "bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra", "giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee", "skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove", "skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork", "knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli", "carrot", "hot dog", "pizza", "donut", "cake", "chair", "sofa", "pottedplant", "bed", "diningtable", "toilet", "tvmonitor", "laptop", "mouse", "remote", "keyboard", "cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book", "clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush" ]

while True: success, img = cap.read() results = model(img, stream=True)
# coordinates for r in results: boxes = r.boxes
for box in boxes: # bounding box x1, y1, x2, y2 = box.xyxy[0] x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2) # convert to int values
# put box in cam cv2.rectangle(img, (x1, y1), (x2, y2), (255, 0, 255), 3)
# confidence confidence = math.ceil((box.conf[0]*100))/100 print("Confidence --->",confidence)
# class name cls = int(box.cls[0]) print("Class name -->", classNames[cls])
# object details org = [x1, y1] font = cv2.FONT_HERSHEY_SIMPLEX fontScale = 1 color = (255, 0, 0) thickness = 2
cv2.putText(img, classNames[cls], org, font, fontScale, color, thickness)
cv2.imshow('Webcam', img) if cv2.waitKey(1) == ord('q'): break
cap.release()cv2.destroyAllWindows()
    运行结果:

—THE END—

觉得有用,麻烦给个赞和在看 

机器视觉与AI深度学习
专注于机器视觉、AI、深度学习等技术最新资讯、实战内容及应用案例的分享,交流!
 最新文章