SHARE
TWEET

Untitled

a guest Apr 20th, 2019 71 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import time
  2.  import RPi.GPIO
  3.  import numpy
  4.  import os
  5.  import pygame.camera
  6.  import pygame
  7.  
  8.  #####GPIO#####
  9.  # Initializing the GPIO bins. The numbering using is board.
  10.  RPi.GPIO.setmode(RPi.GPIO.BOARD)
  11.  
  12.  # Configuring the GPIO bin number 22 to be an output bin.
  13.  RPi.GPIO.setup(8, RPi.GPIO.OUT)
  14.  
  15.  #####PyGame#####
  16.  # Initializing PyGame and the camera.
  17.  pygame.init()
  18.  pygame.camera.init()
  19.  
  20.  # Captured image dimensions. It should be less than or equal to the maximum dimensions acceptable by the camera.
  21.  width = 320
  22.  height = 240
  23.  
  24.  # Preparing a resizable window of the specified size for displaying the captured images.
  25.  window = pygame.display.set_mode((width, height), pygame.RESIZABLE)
  26.  
  27.  # Specifying the camera source and the image dimensions.
  28.  cam = pygame.camera.Camera("/dev/video0", (width, height))
  29.  cam.start()
  30.  
  31.  for im_num in range(0, 2000):
  32.      print("Image : ", im_num)
  33.  
  34.      im = cam.get_image()
  35.  
  36.      # Displaying the image on the window starting from the top-left corner.
  37.      window.blit(im, (0, 0))
  38.  
  39.      # Refreshing the window.
  40.      pygame.display.update()
  41.  
  42.      im = pygame.surfarray.array3d(window)
  43.      r = numpy.mean(im[:, :, 0])
  44.      g = numpy.mean(im[:, :, 1])
  45.      b = numpy.mean(im[:, :, 2])
  46.  
  47.      if ((r - g) > 30 and (r - b) > 30):
  48.          print("Red - Stop")
  49.          try:
  50.              RPi.GPIO.output(8, RPi.GPIO.LOW)
  51.          except KeyboardInterrupt:  # CTRL+C
  52.              print("Keyboard Interrupt.")
  53.          except:
  54.              print("Error occurred.")
  55.      elif ((g - r) > 30 and (g - b) > 30):
  56.          print("Green - Move")
  57.          try:
  58.              RPi.GPIO.output(8, RPi.GPIO.HIGH)
  59.          except KeyboardInterrupt:  # CTRL+C
  60.              print("Keyboard Interrupt.")
  61.          except:
  62.              print("Error occurred.")
  63.  
  64.  # Stopping the camera.
  65.  cam.stop()
  66.  
  67.  # cleanup all GPIO bins.
  68.  print("Clean Up GPIO.")
  69.  RPi.GPIO.cleanup()
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top