Last active
July 20, 2020 17:25
-
-
Save james/b6687dda1a1e9131349b75812ad25e59 to your computer and use it in GitHub Desktop.
Zonal flame detector
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
# Install raspbian lite | |
# run `sudo raspi-config` and enable camera | |
# sudo apt-get update | |
# sudo apt-get upgrade | |
# sudo apt-get install python3-picamera | |
# sudo apt-get install python3-rpi.gpio | |
from picamera.array import PiYUVArray | |
from picamera import PiCamera | |
from time import sleep | |
from fractions import Fraction | |
import numpy as np | |
import RPi.GPIO as GPIO | |
GPIO.setmode(GPIO.BCM) | |
GPIO.setup(4, GPIO.OUT) | |
camera = PiCamera() | |
camera.resolution = (320, 240) | |
camera.framerate = 12 | |
camera.iso = 100 | |
camera.shutter_speed = 50000 | |
camera.exposure_mode = 'off' | |
camera.awb_gains = (Fraction(309, 256), Fraction(365, 128)) | |
rawCapture = PiYUVArray(camera, size=(320, 240)) | |
def flame_detected(arr): | |
return round(np.mean(arr)) > 20 | |
for frame in camera.capture_continuous(rawCapture, format="yuv", use_video_port=True): | |
top_left = frame.array[:120,:160,0].flatten() | |
top_right = frame.array[:120,160:,0].flatten() | |
bottom_left = frame.array[120:,:160,0].flatten() | |
bottom_right = frame.array[120:,160:,0].flatten() | |
ascii = "" | |
if flame_detected(top_left): | |
ascii += "X" | |
GPIO.output(4, GPIO.HIGH) | |
sleep(0.05) | |
GPIO.output(4, GPIO.LOW) | |
else: | |
ascii += "O" | |
if flame_detected(top_right): | |
ascii += "X" | |
else: | |
ascii += "O" | |
ascii += "\n" | |
if flame_detected(bottom_left): | |
ascii += "X" | |
else: | |
ascii += "O" | |
if flame_detected(bottom_right): | |
ascii += "X" | |
else: | |
ascii += "O" | |
ascii += "\n" | |
ascii += "\n" | |
print(ascii) | |
rawCapture.truncate(0) | |
GPIO.cleanup() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment