Advertisement
Guest User

Untitled

a guest
Jan 18th, 2020
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. import time
  2. import edgeiq
  3. import RPi.GPIO as GPIO
  4. import cv2
  5. """
  6. Use object detection to detect human faces in the frame in realtime.
  7.  
  8. To change the computer vision model, follow this guide:
  9. https://dashboard.alwaysai.co/docs/application_development/changing_the_model.html
  10.  
  11. To change the engine and accelerator, follow this guide:
  12. https://dashboard.alwaysai.co/docs/application_development/changing_the_engine_and_accelerator.html
  13. """
  14.  
  15.  
  16. def main():
  17. facial_detector = edgeiq.ObjectDetection(
  18. "alwaysai/res10_300x300_ssd_iter_140000")
  19. facial_detector.load(engine=edgeiq.Engine.DNN)
  20.  
  21. print("Engine: {}".format(facial_detector.engine))
  22. print("Accelerator: {}\n".format(facial_detector.accelerator))
  23. print("Model:\n{}\n".format(facial_detector.model_id))
  24.  
  25. fps = edgeiq.FPS()
  26.  
  27. # servo trigger variables
  28. isFaceDetected = False
  29. servoPIN = 17
  30. GPIO.setmode(GPIO.BCM)
  31. GPIO.setup(servoPIN, GPIO.OUT)
  32.  
  33. try:
  34. with edgeiq.WebcamVideoStream(cam=0) as webcam, \
  35. edgeiq.Streamer() as streamer:
  36. # Allow webcam to warm up
  37. time.sleep(2.0)
  38. fps.start()
  39.  
  40. # loop detection
  41. while True:
  42. frame = webcam.read()
  43. # detect human faces
  44. results = facial_detector.detect_objects(
  45. frame, confidence_level=.5)
  46. frame = edgeiq.markup_image(
  47. frame, results.predictions, show_labels=False)
  48.  
  49. # Generate text to display on streamer
  50. text = ["Model: {}".format(facial_detector.model_id)]
  51. text.append(
  52. "Inference time: {:1.3f} s".format(results.duration))
  53. text.append("Faces:")
  54.  
  55. for prediction in results.predictions:
  56. isFaceDetected = True
  57. text.append("{:2.2f}%".format(prediction.confidence * 100))
  58. # move servo
  59. if(isFaceDetected == True):
  60. # servo code
  61. p = GPIO.PWM(servoPIN, 50) # GPIO 17 for PWM with 50Hz
  62. p.start(2.5) # Initialization
  63. try:
  64. while True:
  65. p.ChangeDutyCycle(10)
  66. except KeyboardInterrupt:
  67. p.stop()
  68. GPIO.cleanup()
  69.  
  70. streamer.send_data(frame, text)
  71.  
  72. fps.update()
  73.  
  74. if streamer.check_exit():
  75. break
  76.  
  77. finally:
  78. # stop fps counter and display information
  79. fps.stop()
  80. print("[INFO] elapsed time: {:.2f}".format(fps.get_elapsed_seconds()))
  81. print("[INFO] approx. FPS: {:.2f}".format(fps.compute_fps()))
  82.  
  83. print("Program Ending")
  84.  
  85.  
  86. if __name__ == "__main__":
  87. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement