当前位置:   article > 正文

使用opencv\yolov10读取摄像头视频流,跳帧处理多目标识别_yolov10 opencv

yolov10 opencv

yolov10 模型下载

git clone https://www.modelscope.cn/THU-MIG/Yolov10.git

opencv 读取摄像头视频流

  • 直接读取电脑上插的摄像头
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
print(cap.get(5), '<---------视频帧率')
  • 1
  • 2
  • 3
  • 读取网络摄像头视频流
cap = cv2.VideoCapture('https://gbs.liveqing.com:10010/sms/34020000002020000001/hls/34020000001180000187_34020000001320000005/live.m3u8?token=y0Xj9Ad74-XnmgpFB6sV8sYiefo2')
ret, frame = cap.read()
print(cap.get(5), '<---------视频帧率')
  • 1
  • 2
  • 3

跳帧: 视频处理需要大量的计算资源,尤其是在进行编码、解码、图像处理等复杂操作时。如果处理器的计算能力有限,可能无法实时处理每一帧,因此需要跳过一些帧以保证视频播放的流畅性

ret, frame = cap.read()
print(cap.get(5), '<---------视频帧率')
time_c = 0
while ret:
    # 读取视频帧
    time_c += 1
    # 设置每 10 帧输出一次
    if (time_c % 10) != 0:
        ret, frame = cap.read()
        yolo_img = frame
    else:
        ret, frame = cap.read()
        yolo_img = yolo_deal(frame)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

yolo处理

# box_annotator = sv.BoxAnnotator() # 由于sv 0.22版本将取消BoxAnnotator,所以会提示警告 提示警告
box_annotator = sv.LabelAnnotator() # 没有边框
def yolo_deal(image):
    results = model(source=image, conf=0.5, verbose=False)[0]
    detections = sv.Detections.from_ultralytics(results)


    category_dict = {
        0: 'person', 1: 'bicycle', 2: 'car', 3: 'motorcycle', 4: 'airplane', 5: 'bus',
        6: 'train', 7: 'truck', 8: 'boat', 9: 'traffic light', 10: 'fire hydrant',
        11: 'stop sign', 12: 'parking meter', 13: 'bench', 14: 'bird', 15: 'cat',
        16: 'dog', 17: 'horse', 18: 'sheep', 19: 'cow', 20: 'elephant', 21: 'bear',
        22: 'zebra', 23: 'giraffe', 24: 'backpack', 25: 'umbrella', 26: 'handbag',
        27: 'tie', 28: 'suitcase', 29: 'frisbee', 30: 'skis', 31: 'snowboard',
        32: 'sports ball', 33: 'kite', 34: 'baseball bat', 35: 'baseball glove',
        36: 'skateboard', 37: 'surfboard', 38: 'tennis racket', 39: 'bottle',
        40: 'wine glass', 41: 'cup', 42: 'fork', 43: 'knife', 44: 'spoon', 45: 'bowl',
        46: 'banana', 47: 'apple', 48: 'sandwich', 49: 'orange', 50: 'broccoli',
        51: 'carrot', 52: 'hot dog', 53: 'pizza', 54: 'donut', 55: 'cake',
        56: 'chair', 57: 'couch', 58: 'potted plant', 59: 'bed', 60: 'dining table',
        61: 'toilet', 62: 'tv', 63: 'laptop', 64: 'mouse', 65: 'remote', 66: 'keyboard',
        67: 'cell phone', 68: 'microwave', 69: 'oven', 70: 'toaster', 71: 'sink',
        72: 'refrigerator', 73: 'book', 74: 'clock', 75: 'vase', 76: 'scissors',
        77: 'teddy bear', 78: 'hair drier', 79: 'toothbrush'
    }

    labels = [
        f"{category_dict[class_id]} {confidence:.2f}"
        for class_id, confidence in zip(detections.class_id, detections.confidence)
    ]
    annotated_image = box_annotator.annotate(
        image.copy(), detections=detections, labels=labels
    )
    return annotated_image
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

完整代码链接:

demo

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

闽ICP备14008679号