Guest User

Untitled

a guest
Feb 22nd, 2018
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.26 KB | None | 0 0
  1. #!usr/bin/python
  2. ##################import the necessary packages###########################
  3.  
  4. from imutils.video import VideoStream;from imutils.video import FPS
  5. import numpy as np;import argparse;import imutils;import time;import cv2
  6. import I2C_LCD_driver
  7. import RPi.GPIO as GPIO
  8. import motors
  9.  
  10. ##########################Set up LCD screen################################
  11.  
  12. mylcd = I2C_LCD_driver.lcd()
  13. mylcd.backlight(1)
  14. mylcd.lcd_display_string("Initiating", 1, 0)
  15. mylcd.lcd_display_string("Program", 2, 0)
  16.  
  17. #####################Set up object recognition stuff#######################
  18.  
  19. # construct the argument parse and parse the arguments
  20.  
  21. ap = argparse.ArgumentParser()
  22. #ap.add_argument("-p", "--prototxt", required=True,
  23. # help="path to Caffe 'deploy' prototxt file")
  24. #ap.add_argument("-m", "--model", required=True,
  25. # help="path to Caffe pre-trained model")
  26. ap.add_argument("-c", "--confidence", type=float, default=0.2,
  27. help="minimum probability to filter weak detections")
  28. args = vars(ap.parse_args())
  29.  
  30. # initialize the list of class labels MobileNet SSD was trained to
  31. # detect, then generate a set of bounding box colors for each class
  32.  
  33. CLASSES = ["background", "aeroplane", "bicycle", "bird", "boat",
  34. "bottle", "bus", "car", "cat", "chair", "cow", "diningtable",
  35. "dog", "horse", "motorbike", "person", "pottedplant", "sheep",
  36. "sofa", "train", "tvmonitor"]
  37. COLORS = np.random.uniform(0, 255, size=(len(CLASSES), 3))
  38.  
  39. # load our serialized model from disk
  40.  
  41. print("[INFO] loading model...")
  42. #net = cv2.dnn.readNetFromCaffe(args["prototxt"], args["model"])
  43. net = cv2.dnn.readNetFromCaffe('/home/pi/google_home_starter/public/python/MobileNetSSD_deploy.prototxt.txt','/home/pi/google_home_starter/public/python/MobileNetSSD_deploy.caffemodel')
  44.  
  45. # initialize the video stream, allow the cammera sensor to warmup,
  46. # and initialize the FPS counter
  47.  
  48. print("[INFO] starting video stream...")
  49.  
  50. # vs = VideoStream(src=0).start() #For USB camera
  51.  
  52. vs = VideoStream(usePiCamera=True).start() #For Rpi camera
  53.  
  54. time.sleep(2.0)
  55. #fps = FPS().start()
  56. mylcd.lcd_clear()
  57.  
  58. ##############################BEGIN#######################################
  59.  
  60. a = 0
  61. # loop over the frames from the video stream
  62. while a != 1:
  63. # grab the frame from the threaded video stream and resize it
  64. # to have a maximum width of 400 pixels
  65. frame = vs.read()
  66. if frame is None:
  67. continue
  68.  
  69. frame = imutils.resize(frame, width=400)
  70.  
  71. # grab the frame dimensions and convert it to a blob
  72. (h, w) = frame.shape[:2]
  73. blob = cv2.dnn.blobFromImage(cv2.resize(frame, (300, 300)),
  74. 0.007843, (300, 300), 127.5)
  75.  
  76. # pass the blob through the network and obtain the detections and
  77. # predictions
  78. net.setInput(blob)
  79. detections = net.forward()
  80.  
  81. # loop over the detections
  82. for i in np.arange(0, detections.shape[2]):
  83. # extract the confidence (i.e., probability) associated with
  84. # the prediction
  85. confidence = detections[0, 0, i, 2]
  86.  
  87. # filter out weak detections by ensuring the `confidence` is
  88. # greater than the minimum confidence
  89. if confidence > args["confidence"]:
  90. # extract the index of the class label from the
  91. # `detections`, then compute the (x, y)-coordinates of
  92. # the bounding box for the object
  93. idx = int(detections[0, 0, i, 1])
  94. #box = detections[0, 0, i, 3:7] * np.array([w, h, w, h])
  95. #(startX, startY, endX, endY) = box.astype("int")
  96.  
  97. # draw the prediction on the frame
  98. #label = "{}: {:.2f}%".format(CLASSES[idx],
  99. # confidence * 100)
  100. #cv2.rectangle(frame, (startX, startY), (endX, endY),
  101. # COLORS[idx], 2)
  102. #y = startY - 15 if startY - 15 > 15 else startY + 15
  103. #cv2.putText(frame, label, (startX, y),
  104. # cv2.FONT_HERSHEY_SIMPLEX, 0.5, COLORS[idx], 2)
  105. if confidence * 100 > 75:
  106. mylcd.lcd_clear()
  107. mylcd.lcd_display_string(CLASSES[idx], 1, 0)
  108. confi = int(np.floor(confidence * 100))
  109. mylcd.lcd_display_string("{:d}".format(confi), 2, 1)
  110. mylcd.lcd_display_string("%", 2, 3)
  111. mylcd.lcd_display_string("confidence", 2, 5)
  112. time.sleep(1)
  113.  
  114.  
  115. if CLASSES[idx] == "person" and confidence * 100 > 80:
  116. a = 1
  117.  
  118. else:
  119. mylcd.lcd_clear()
  120. mylcd.lcd_display_string("I dont recognize", 1, 0)
  121. mylcd.lcd_display_string("anything!", 2, 0)
  122.  
  123. # show the output frame
  124. #cv2.imshow("Frame", frame)
  125. key = cv2.waitKey(1) & 0xFF
  126.  
  127. # if the `q` key was pressed, break from the loop
  128. if key == ord("q"):
  129. break
  130. #fps.update()
  131.  
  132. motors.stepper()
  133.  
  134. # do a bit of cleanup
  135. cv2.destroyAllWindows()
  136. vs.stop()
  137. GPIO.cleanup()
  138. mylcd.lcd_clear()
  139. mylcd.backlight(0)
Add Comment
Please, Sign In to add comment