Created
April 10, 2019 22:13
-
-
Save ahmedfgad/35065ce482c94476807efefc581a8c85 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import time | |
| import RPi.GPIO | |
| import numpy | |
| import os | |
| import pygame.camera | |
| import pygame | |
| #####GPIO##### | |
| # Initializing the GPIO bins. The numbering using is board. | |
| RPi.GPIO.setmode(RPi.GPIO.BOARD) | |
| # Configuring the GPIO bin number 22 to be an output bin. | |
| RPi.GPIO.setup(8, RPi.GPIO.OUT) | |
| #####PyGame##### | |
| # Initializing PyGame and the camera. | |
| pygame.init() | |
| pygame.camera.init() | |
| # Captured image dimensions. It should be less than or equal to the maximum dimensions acceptable by the camera. | |
| width = 320 | |
| height = 240 | |
| # Preparing a resizable window of the specified size for displaying the captured images. | |
| window = pygame.display.set_mode((width, height), pygame.RESIZABLE) | |
| # Specifying the camera source and the image dimensions. | |
| cam = pygame.camera.Camera("/dev/video0", (width, height)) | |
| cam.start() | |
| for im_num in range(0, 2000): | |
| print("Image : ", im_num) | |
| im = cam.get_image() | |
| # Displaying the image on the window starting from the top-left corner. | |
| window.blit(im, (0, 0)) | |
| # Refreshing the window. | |
| pygame.display.update() | |
| im = pygame.surfarray.array3d(window) | |
| r = numpy.mean(im[:, :, 0]) | |
| g = numpy.mean(im[:, :, 1]) | |
| b = numpy.mean(im[:, :, 2]) | |
| if ((r - g) > 30 and (r - b) > 30): | |
| print("Red - Stop") | |
| try: | |
| RPi.GPIO.output(8, RPi.GPIO.LOW) | |
| except KeyboardInterrupt: # CTRL+C | |
| print("Keyboard Interrupt.") | |
| except: | |
| print("Error occurred.") | |
| elif ((g - r) > 30 and (g - b) > 30): | |
| print("Green - Move") | |
| try: | |
| RPi.GPIO.output(8, RPi.GPIO.HIGH) | |
| except KeyboardInterrupt: # CTRL+C | |
| print("Keyboard Interrupt.") | |
| except: | |
| print("Error occurred.") | |
| # Stopping the camera. | |
| cam.stop() | |
| # cleanup all GPIO bins. | |
| print("Clean Up GPIO.") | |
| RPi.GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment