Here's a program to try. It uses the Pi camera and Opencv so you may need to install that with sudo apt-get install python-opencv
You should see 3 windows, 1 with the camera image with a red rectangle to show where it is detecting, click on the image to move this, a 2nd control window where you can set the camera, detection window size, zoom in/out etc and a 3rd window showing the detected area.
When it sees movement you will see FLAME in the Control window.
Note I don't know what this flame looks like so it may not detect movement so you chose between motion or light level.
Code: Select all
import cv2
import numpy as np
import time
import os
from decimal import *
print "Press ESC to EXIT"
print " "
print "Left Click on control buttons to Decrement, Right Click to Increment"
if os.path.exists('/dev/video0') == False:
path = 'sudo modprobe bcm2835-v4l2 max_video_width=2592 max_video_height=1944'
os.system (path)
time.sleep(1)
def onmouse(event,a,b,flags,params):
global ix,iy
if event == cv2.EVENT_LBUTTONDOWN:
if a < width - xcrop and a > xcrop:
if b < height - ycrop and b > ycrop:
ix,iy = a,b
def onmouse2(event,x,y,flags,params):
global ix2,iy2
if event == cv2.EVENT_LBUTTONDOWN:
ix2 = (int(x/64)*10) + int(y/32) + 10
iy2 = -1
if event == cv2.EVENT_RBUTTONDOWN:
ix2 = (int(x/64)*10) + int(y/32) + 10
iy2 = 1
def keys (cn,rn,bh,kr,kg,kb,kf):
kx = (cn * 64) + 1
ky = (rn * 32) + 1
cv2.rectangle(q,(kx,ky+(bh*15)),(kx+62,ky+30),(kb,kg,kr),kf)
def text (cn,rn,th,text,tf,tr,tg,tb,tw):
tx = (cn * 64) + (29 - (len(text))*3)
ty = (rn*32)+ (th*14) +12
cv2.putText(q,text,(tx,ty), font, tf,(tb,tg,tr),tw)
cv2.imshow( winName2,q)
width = 640
height = 480
cam = cv2.VideoCapture(0)
cam.set(3,width)
cam.set(4,height)
filno = 0
ix2 = 0
iy2 = 0
ix = 320
iy = 240
xcrop = 20
ycrop =40
thres = 20
trigger = 10
zoom = 0
AutoE = 0
ExpT = 100
ISO = 0
AEB = 12
FPS = 15
brightness = 50
contrast = 30
timeout = 5
detect = 0
rpiwidth = [640,800,960,1280,1920,2592]
rpiheight = [480,600,720,960,1440,1944]
width = rpiwidth[zoom]
height = rpiheight[zoom]
winName = "Flame Indicator"
winName2 = "Control"
winName3 = "Cropped"
cv2.namedWindow(winName, cv2.CV_WINDOW_AUTOSIZE)
cv2.namedWindow(winName2, cv2.CV_WINDOW_AUTOSIZE)
cv2.namedWindow(winName3, cv2.CV_WINDOW_AUTOSIZE)
q = np.zeros((320,128,3), np.uint8)
s = np.zeros((height,width,3), np.uint8)
s1 = np.zeros((height,width,3), np.uint8)
z = np.zeros((ycrop,xcrop,3), np.uint8)
font = cv2.FONT_HERSHEY_SIMPLEX
b = 0
while b < 10:
keys (0,b,0,128,128,128,-1)
b +=1
b = 0
while b < 10:
keys (1,b,0,128,128,128,-1)
b +=1
text (0,0,0,'xCrop',.4,255,255,255,1)
text (0,0,1,str(xcrop),.4,255,0,0,1)
text (0,1,0,'yCrop',.4,255,255,255,1)
text (0,1,1,str(ycrop),.4,255,0,0,1)
text (0,2,0,'Threshold',.4,255,255,255,1)
text (0,2,1,str(thres),.4,255,0,0,1)
text (0,3,0,'Trigger',.4,255,255,255,1)
text (0,3,1,str(trigger)+"%",.4,255,0,0,1)
text (1,0,0,'Zoom',.4,255,255,255,1)
text (1,0,1,str(zoom),.4,255,0,0,1)
text (1,5,0,'Brightness',.4,255,255,255,1)
text (1,5,1,str(brightness),.4,255,0,0,1)
text (1,6,0,'Contrast',.4,255,255,255,1)
text (1,6,1,str(contrast),.4,255,0,0,1)
text (1,9,0,'Timeout',.4,255,255,255,1)
text (1,9,1,str(timeout),.4,255,0,0,1)
if AutoE == 0:
text (0,4,0,'Auto Exp',.4,0,255,0,1)
else:
text (0,4,0,'Auto Exp',.4,255,255,255,1)
path = 'v4l2-ctl --set-ctrl=autoexposure=1'
os.system (path)
if AutoE == 1:
path = 'v4l2-ctl --set-ctrl=exposure_time_absolute=' + str(ExpT)
os.system (path)
if AutoE == 0:
text (0,5,0,'Exp Time',.4,255,255,255,1)
else:
text (0,5,0,'Exp Time',.4,0,255,0,1)
text (0,5,1,str(ExpT/10)+"mS",.4,255,0,0,1)
text (0,6,0,'ISO',.4,255,255,255,1)
if ISO == 0:
text (0,6,1,"Auto",.4,255,0,0,1)
if ISO == 1:
text (0,6,1,"100",.4,255,0,0,1)
if ISO == 2:
text (0,6,1,"200",.4,255,0,0,1)
if ISO == 3:
text (0,6,1,"400",.4,255,0,0,1)
if ISO == 4:
text (0,6,1,"800",.4,255,0,0,1)
text (0,7,0,'AEB',.4,255,255,255,1)
text (0,7,1,str(AEB-12),.4,255,0,0,1)
text (0,8,0,'FPS',.4,255,255,255,1)
text (0,8,1,str(FPS),.4,255,0,0,1)
if detect == 0:
text (1,4,0,'Motion',.4,0,255,0,1)
text (0,2,0,'Threshold',.4,255,255,255,1)
text (0,2,1,str(thres),.4,255,0,0,1)
else:
text (1,4,0,'Light',.4,255,255,255,1)
text (0,2,0,'Threshold',.4,50,50,50,1)
text (0,2,1,str(thres),.4,50,0,0,1)
cv2.imshow( winName2,q)
t0 = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
t = cv2.cvtColor(cam.read()[1], cv2.COLOR_RGB2GRAY)
cv2.setMouseCallback(winName,onmouse)
cv2.setMouseCallback(winName2,onmouse2)
start = time.time()
keys (0,9,0,0,0,0,-1)
while True:
if ix2 > 0:
if ix2 == 10:
if iy2 < 0 and xcrop > 10:
xcrop = xcrop - 2
if iy2 > 0 and xcrop < 100:
xcrop = xcrop + 2
keys (0,0,1,128,128,128,-1)
text (0,0,1,str(xcrop),.4,255,0,0,1)
cv2.imshow( winName2,q)
ix2 = 0
iy2 = 0
if ix2 == 11:
if iy2 < 0 and ycrop > 10:
ycrop = ycrop - 2
if iy2 > 0 and ycrop < 100:
ycrop = ycrop + 2
keys (0,1,1,128,128,128,-1)
text (0,1,1,str(ycrop),.4,255,0,0,1)
cv2.imshow( winName2,q)
ix2 = 0
iy2 = 0
if ix2 == 12:
if iy2 < 0 and thres > 2:
thres = thres - 2
if iy2 > 0 and thres < 253:
thres = thres + 2
keys (0,2,1,128,128,128,-1)
text (0,2,1,str(thres),.4,255,0,0,1)
ix2 = 0
iy2 = 0
if ix2 == 13:
if iy2 < 0 and trigger > 2:
trigger = trigger - 2
if iy2 > 0 and trigger < 97:
trigger = trigger + 2
keys (0,3,1,128,128,128,-1)
text (0,3,1,str(trigger)+"%",.4,255,0,0,1)
ix2 = 0
iy2 = 0
if ix2 == 14:
if AutoE == 0:
AutoE = 1
text (0,4,0,'Auto Exp',.4,255,255,255,1)
text (0,5,0,'Exp Time',.4,0,255,0,1)
path = 'v4l2-ctl --set-ctrl=auto_exposure=1'
os.system (path)
path = 'v4l2-ctl --set-ctrl=exposure_time_absolute=' + str(ExpT)
os.system (path)
ix2 = 0
else:
AutoE = 0
text (0,4,0,'Auto Exp',.4,0,255,0,1)
text (0,5,0,'Exp Time',.4,255,255,255,1)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
ix2 = 0
cam.set(3,width)
cam.set(4,height)
if ix2 == 15:
if iy2 < 0 and ExpT > 10:
ExpT = ExpT - 10
if iy2 > 0 and ExpT < int(Decimal(Decimal(1)/Decimal(FPS))*10000):
ExpT = ExpT + 10
keys (0,5,1,128,128,128,-1)
text (0,5,1,str(ExpT/10)+"mS",.4,255,0,0,1)
path = 'v4l2-ctl --set-ctrl=exposure_time_absolute=' + str(ExpT)
os.system (path)
path = 'v4l2-ctl --set-ctrl=auto_exposure=1'
os.system (path)
AutoE = 1
text (0,4,0,'Auto Exp',.4,255,255,255,1)
text (0,5,0,'Exp Time',.4,0,255,0,1)
ix2 = 0
iy2 = 0
if ix2 == 16:
if iy2 < 0 and ISO > 0:
ISO = ISO - 1
if iy2 > 0 and ISO < 4:
ISO = ISO + 1
keys (0,6,1,128,128,128,-1)
if ISO == 0:
ISO2 = 0
text (0,6,1,"Auto",.4,255,0,0,1)
if ISO == 1:
ISO2 = 100
text (0,6,1,"100",.4,255,0,0,1)
if ISO == 2:
ISO2 = 200
text (0,6,1,"200",.4,255,0,0,1)
if ISO == 3:
ISO2 = 400
text (0,6,1,"400",.4,255,0,0,1)
if ISO == 4:
ISO2 = 800
text (0,6,1,"800",.4,255,0,0,1)
path = 'v4l2-ctl --set-ctrl=iso_sensitivity=' + str(ISO)
os.system (path)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
ix2 = 0
iy2 = 0
if ix2 == 17:
if iy2 < 0 and AEB > 0:
AEB = AEB - 1
if iy2 > 0 and AEB < 24:
AEB = AEB + 1
keys (0,7,1,128,128,128,-1)
text (0,7,1,str(AEB-12),.4,255,0,0,1)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
path = 'v4l2-ctl --set-ctrl=auto_exposure_bias=' + str(AEB)
os.system (path)
AutoE = 0
text (0,4,0,'Auto Exp',.4,0,255,0,1)
text (0,5,0,'Exp Time',.4,255,255,255,1)
ix2 = 0
iy2 = 0
if ix2 == 18:
if iy2 < 0 and FPS > 3:
FPS = FPS - 1
if iy2 > 0 and FPS < 60:
FPS = FPS + 1
keys (0,8,1,128,128,128,-1)
text (0,8,1,str(FPS),.4,255,0,0,1)
path = 'v4l2-ctl -p ' + str(FPS)
os.system (path)
if ExpT > int(Decimal(Decimal(1)/Decimal(FPS))*10000):
ExpT = 10*((int(Decimal(Decimal(1)/Decimal(FPS))*10000))/10)
keys (0,5,1,128,128,128,-1)
text (0,5,1,str(ExpT/10)+"mS",.4,255,0,0,1)
if AutoE == 1:
path = 'v4l2-ctl --set-ctrl=exposure_time_absolute=' + str(ExpT)
os.system (path)
ix2 = 0
iy2 = 0
if ix2 == 20:
if iy2 < 0 and zoom > 0:
zoom = zoom - 1
if iy2 > 0 and zoom < 5:
zoom = zoom + 1
keys (1,0,1,128,128,128,-1)
text (1,0,1,str(zoom),.4,255,0,0,1)
cv2.imshow( winName2,q)
width = rpiwidth[zoom]
height = rpiheight[zoom]
cam.set(3,width)
cam.set(4,height)
path = 'v4l2-ctl --set-ctrl=auto_exposure=0'
os.system (path)
AutoE = 0
text (0,4,0,'Auto Exp',.4,0,255,0,1)
text (0,5,0,'Exp Time',.4,255,255,255,1)
s = np.zeros((height,width,3), np.uint8)
s1 = np.zeros((height,width,3), np.uint8)
ix2 = 0
iy2 = 0
if ix2 == 24:
if detect == 0:
detect = 1
keys (1,4,0,128,128,128,-1)
text (1,4,0,'Light',.4,0,255,0,1)
text (0,2,0,'Threshold',.4,50,50,50,1)
text (0,2,1,str(thres),.4,50,0,0,1)
ix2 = 0
else:
detect = 0
keys (1,4,0,128,128,128,-1)
text (1,4,0,'Motion',.4,0,255,0,1)
text (0,2,0,'Threshold',.4,255,255,255,1)
text (0,2,1,str(thres),.4,255,0,0,1)
ix2 = 0
iy2 = 0
if ix2 == 25:
if iy2 < 0 and brightness > 0:
brightness = brightness - 2
if iy2 > 0 and brightness < 100:
brightness = brightness + 2
keys (1,5,1,128,128,128,-1)
text (1,5,1,str(brightness),.4,255,0,0,1)
cv2.imshow( winName2,q)
path = 'v4l2-ctl --set-ctrl=brightness=' + str(brightness)
os.system (path)
ix2 = 0
iy2 = 0
if ix2 == 26:
if iy2 < 0 and contrast > -100:
contrast = contrast - 5
if iy2 > 0 and contrast < 100:
contrast = contrast + 5
keys (1,6,1,128,128,128,-1)
text (1,6,1,str(contrast),.4,255,0,0,1)
cv2.imshow( winName2,q)
path = 'v4l2-ctl --set-ctrl=contrast=' + str(contrast)
os.system (path)
ix2 = 0
iy2 = 0
if ix2 == 29:
if iy2 < 0 and timeout > 1:
timeout = timeout - 1
if iy2 > 0 and timeout < 100:
timeout = timeout + 1
keys (1,9,1,128,128,128,-1)
text (1,9,1,str(timeout),.4,255,0,0,1)
cv2.imshow( winName2,q)
ix2 = 0
iy2 = 0
s = cam.read()[1]
s1 = s[(height/2)-240:(height/2)+240,(width/2)-320:(width/2)+320]
cv2.rectangle(s1,(ix-(xcrop+1),iy-(ycrop+1)),(ix+(xcrop+1),iy+(ycrop+1)),(0,0,255),1)
cv2.imshow( winName,s1)
t = cv2.cvtColor(s1,cv2.COLOR_RGB2GRAY)
av = t[iy-ycrop:iy+ycrop,ix-xcrop:ix+xcrop]
now = time.time()
if now < start:
start = time.time()
if now > start + timeout:
keys (0,9,0,0,0,0,-1)
if len(t) == len(t0):
l = t[iy-ycrop:iy+ycrop,ix-xcrop:ix+xcrop]
im = cv2.absdiff(t, t0)
c = im[iy-ycrop:iy+ycrop,ix-xcrop:ix+xcrop]
c[c < thres] = 0
c[c >= thres] = 200
cv2.imshow( winName3,c)
total = np.sum(c)/200
trig =(xcrop*ycrop*4)/(100/trigger)
trigl = np.average(av)
cv2.imshow( winName2,q)
keys (0,9,1,0,0,0,-1)
#print trigl , ((Decimal(trigger)/Decimal(100))*255)
if (total > trig and detect == 0) or (trigl > ((Decimal(trigger)/Decimal(100))*255) and detect == 1):
text (0,9,1,'FLAME',.4,0,255,0,1)
text (0,9,0,'FLAME',.4,0,255,0,1)
start = time.time()
t0 = t
key = cv2.waitKey(10)
if key == 27:
cv2.destroyWindow(winName)
break
print "Goodbye"