当前位置 : 主页 > 网页制作 > html >

OpenVINO 对象识别 real_time_object_detection Movidius

来源:互联网 收集:自由互联 发布时间:2021-06-12
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

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()
网友评论