在日常生活,總是離不開火,有時候我們需要預防火災發生,但是我們又不可能一直盯著,這時候我們就需要一款程序幫我們盯著,一旦發生火災從而告知我們,今天就帶大家編寫這么一款應用。
安裝需要的庫
pip install opencv-python
代碼實現
import cv2 # Library for openCV# To access xml file which includes positive and negative images of fire.
# (Trained images) File is also provided with the code.
fire_cascade = cv2.CascadeClassifier('fire_detection.xml') vid = cv2.VideoCapture(0)while(True):# Value in ret is True # To read video frameret, frame = vid.read() # To convert frame into gray colorgray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) # to provide frame resolutionfire = fire_cascade.detectMultiScale(frame, 1.2, 5) ## to highlight fire with square for (x,y,w,h) in fire:cv2.rectangle(frame,(x-20,y-20),(x+w+20,y+h+20),(255,0,0),2)roi_