OpenVINO 对象识别 real_time_object_detection Movidius

本文介绍了一种使用Python和OpenCV进行MP4视频实时对象识别和标注的方法。通过解析命令行参数来指定Caffe网络配置文件、训练模型及视频源,实现对视频中各类对象的实时检测与标注。涉及的技术包括imutils、numpy、argparse等库的运用,以及如何利用MobileNet SSD模型进行目标检测。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

MP4 识别结果

https://v.youku.com/v_show/id_XNDM3MTEyNDY2OA==.html

 

from imutils.video import VideoStream
import numpy as np
import argparse
import imutils
import time
import cv2


# python3 mp4-video-realtime-label.py --config MobileNetSSD_deploy.prototxt --model MobileNetSSD_deploy.caffemodel --video pexels-video2.mp4
ap = argparse.ArgumentParser()
ap.add_argument("-c", "--config", required=True, help="filename of caffe network configuration")
ap.add_argument("-m", "--model", required=True, help="filename of trained caffe model")
ap.add_argument("-v", "--video", help="filename of the video (optional)")
args = vars(ap.parse_args())


use_camera = False
if not args.get("video", False):
    use_camera = True


CLASSES = ("background","warcraft", "bicycle", "bird", "boat","bottle", "bus", "car", "cat", "chair","cow", 
        "diningtable", "dog", "horse","motorbike", "person", "pottedplant","sheep", "sofa", "train", "tvmonitor")
COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))

net = cv2.dnn.readNetFromCaffe(args["config"], args["model"])

net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)


if use_camera:
    print("Camera...")
    vs = VideoStream(src=0).start()
    time.sleep(2.0)
else:
    vs = cv2.VideoCapture(args["video"])


while True:
    frame = vs.read()
    frame = frame if use_camera else frame[1]
    if frame is None:
        break
    frame = imutils.resize(frame, width=400)

    blob = cv2.dnn.blobFromImage(frame, 0.007843, (512, 393), 127.5)


    net.setInput(blob)
    detections = net.forward()

    for detection in detections.reshape(-1, 7):
        index = int(detection[1])
        confidence = float(detection[2])


        if confidence > 0.5:
            xmin = int(detection[3] * frame.shape[1])
            ymin = int(detection[4] * frame.shape[0])
            xmax = int(detection[5] * frame.shape[1])
            ymax = int(detection[6] * frame.shape[0])
            label = "{}: {:.2f}%".format(CLASSES[index], confidence * 100)
            cv2.rectangle(frame, (xmin, ymin), (xmax, ymax), COLORS[index], 1)
            # Label
            cv2.rectangle(frame, (xmin-1, ymin-1),(xmin+70, ymin-10), COLORS[index], -1)
            # Labeltext
            cv2.putText(frame, label, (xmin, ymin -2), cv2.FONT_HERSHEY_SIMPLEX, 0.3, (0,0,0),1)

    cv2.imshow("RaspBerry with Movidius", frame)

    key = cv2.waitKey(1) & 0xFF
    if key == ord('q'):
        break

cv2.destroyAllWindows()
if use_camera:
    vs.stop()
else:
    vs.release()

 

转载于:https://www.cnblogs.com/cloudrivers/p/11567012.html

### Real-time processing in computer systems Real-time processing refers to the ability of a computing system to respond to external events or inputs within a guaranteed time frame. This is crucial for applications where timing constraints are critical, such as industrial control systems, robotics, telecommunications, and multimedia streaming services. In real-time systems, tasks must be completed before their deadlines; otherwise, the consequences can range from degraded performance to catastrophic failure. These systems are typically categorized into hard real-time and soft real-time systems. Hard real-time systems have strict timing requirements that must always be met, whereas soft real-time systems allow some flexibility with occasional deadline misses being acceptable [^1]. Technologies like YOLO (You Only Look Once) demonstrate real-time capabilities by processing images individually but functioning effectively as tracking systems when connected to a webcam, detecting objects as they move around and change in appearance [^1]. Similarly, frameworks like Apache Spark's Structured Streaming provide declarative APIs for real-time data processing, supporting features like windowing and event time through existing aggregation operators [^2]. For real-time object detection, architectures like R-CNN and its variants use region proposals instead of sliding windows to find objects in images, although this approach can be computationally intensive and slow compared to modern techniques [^3]. On the other hand, mainstream real-time object detectors designed for GPUs often utilize optimized architectures like ResNet, DarkNet, or DLA combined with strategies like CSPNet to enhance performance while focusing on both architectural and training process optimizations [^4]. Here is a simple example of how one might implement a basic real-time processing loop in Python using OpenCV for video capture: ```python import cv2 # Initialize the webcam cap = cv2.VideoCapture(0) while True: # Capture frame-by-frame ret, frame = cap.read() # Our operations on the frame come here gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # Display the resulting frame cv2.imshow('frame', gray) # Press 'q' to quit if cv2.waitKey(1) & 0xFF == ord('q'): break # When everything done, release the capture cap.release() cv2.destroyAllWindows() ``` This script initializes a webcam, captures frames in a loop, converts them to grayscale, displays the output, and allows exiting the loop by pressing the 'q' key.
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值