Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.38 KB | None | 0 0
  1. # import the necessary packages
  2. from imutils.video import VideoStream
  3. from imutils.video import FPS
  4. import numpy as np
  5. import argparse
  6. import imutils
  7. import time
  8. import cv2
  9.  
  10. # construct the argument parse and parse the arguments
  11. ap = argparse.ArgumentParser()
  12. ap.add_argument("-p", "--prototxt", required=True,
  13. help="path to Caffe 'deploy' prototxt file")
  14. ap.add_argument("-m", "--model", required=True,
  15. help="path to Caffe pre-trained model")
  16. ap.add_argument("-c", "--confidence", type=float, default=0.2,
  17. help="minimum probability to filter weak detections")
  18. ap.add_argument("-u", "--movidius", type=bool, default=0,
  19. help="boolean indicating if the Movidius should be used")
  20. args = vars(ap.parse_args())
  21.  
  22. # initialize the list of class labels MobileNet SSD was trained to
  23. # detect, then generate a set of bounding box colors for each class
  24. CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
  25. "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
  26. "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
  27. "sofa", "train", "tvmonitor"]
  28. COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
  29.  
  30. # load our serialized model from disk
  31. print("[INFO] loading model...")
  32. net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
  33.  
  34. # specify the target device as the Myriad processor on the NCS
  35. net.setPreferableTarget(cv2.dnn.DNN_TARGET_MYRIAD)
  36.  
  37. # initialize the video stream, allow the cammera sensor to warmup,
  38. # and initialize the FPS counter
  39. print("[INFO] starting video stream...")
  40.  
  41. cap = cv2.VideoCapture(0)
  42. time.sleep(2.0)
  43. fps = FPS().start()
  44.  
  45. # loop over the frames from the video stream
  46. while True:
  47. # grab the frame from the threaded video stream and resize it
  48. # to have a maximum width of 400 pixels
  49.  
  50. cv2.imshow("frame", frame)
  51. # grab the frame dimensions and convert it to a blob
  52. (h, w) = frame.shape[:2]
  53. blob = cv2.dnn.blobFromImage(frame, 0.007843, (300, 300), 127.5)
  54.  
  55. # pass the blob through the network and obtain the detections and
  56. # predictions
  57. net.setInput(blob)
  58. detections = net.forward()
  59.  
  60. # loop over the detections
  61. for i in np.arange(0, detections.shape[2]):
  62. # extract the confidence (i.e., probability) associated with
  63. # the prediction
  64. confidence = detections[0, 0, i, 2]
  65.  
  66. # filter out weak detections by ensuring the `confidence` is
  67. # greater than the minimum confidence
  68. if confidence > args["confidence"]:
  69. # extract the index of the class label from the
  70. # `detections`, then compute the (x, y)-coordinates of
  71. # the bounding box for the object
  72. idx = int(detections[0, 0, i, 1])
  73. box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
  74. (startX, startY, endX, endY) = box.astype("int")
  75.  
  76. # draw the prediction on the frame
  77. label = "{}: {:.2f}%".format(CLASSES[idx],
  78. confidence * 100)
  79. cv2.rectangle(frame, (startX, startY), (endX, endY),
  80. COLORS[idx], 2)
  81. y = startY - 15 if startY - 15 > 15 else startY + 15
  82. cv2.putText(frame, label, (startX, y),
  83. cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
  84.  
  85. # show the output frame
  86. cv2.imshow("Frame", frame)
  87. key = cv2.waitKey(1) & 0xFF
  88.  
  89. # if the `q` key was pressed, break from the loop
  90. if key == ord("q"):
  91. break
  92.  
  93. # update the FPS counter
  94. fps.update()
  95.  
  96. # stop the timer and display FPS information
  97. fps.stop()
  98. print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
  99. print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
  100.  
  101. # do a bit of cleanup
  102. cv2.destroyAllWindows()
  103. vs.stop()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement