import cv2
import os
import numpy as np
import glob
import json
file = open("result.txt", "a+")
nfile = open("nresult.txt", "a+")
resultpath = "./detection-result/"
nresultpath = "./detection-nresult/"
minScore = 0.8
num = 0
def isexsist(path):
isExists = os.path.exists(path)
if not isExists:
os.makedirs(path)
fileaddress = []
def walkFile(file):
for root, dirs, files in os.walk(file):
for f in files:
fileaddress.append(os.path.join(root, f))
return files, fileaddress
def all_detect(img_path):
files, fileaddress = walkFile(img_path)
global file
global nfile
for index, f in enumerate(fileaddress):
if str(f).find('.jpg') != -1:
print(f)
filepre = f[:f.find('_')]
origimg = cv2.imdecode(np.fromfile(f, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
resultimage, isshow, rows, cols = detect(origimg, minScore)
CLASSES = ('background', 'fire','smoke')
def area(left, top, right, bottom):
return (right - left) * (bottom - top)
def detect(img, minScore):
isshow = 0
im_tensor = cv2.dnn.blobFromImage(img, 0.007843, (300, 300), (127.5, 127.5, 127.5), False, False)
net.setInput(im_tensor)
rows, cols, chinals = img.shape
print(rows, cols)
cvOut = net.forward()
for detection in cvOut[0, 0, :, :]:
score = float(detection[2])
if score > minScore:
left = int(detection[3] * cols)
top = int(detection[4] * rows)
right = int(detection[5] * cols)
bottom = int(detection[6] * rows)
weight = int(right - left)
height = int(bottom - top)
cv2.rectangle(img, (left, top), (right, bottom), (0, 255, 0), 2)
cv2.putText(img, str(CLASSES[int(detection[1])]) + " score is" + str(score), (left, top + 50), 1, 2,(255, 255, 0), 2)
print("find fire ",score, detection[3], detection[4], detection[5], detection[6])
cv2.namedWindow("result",0)
cv2.resizeWindow("result",1280,720)
cv2.imshow("result",img)
cv2.waitKey(10)
return img, isshow, rows, cols
def video_detect(path):
cap=cv2.VideoCapture(path)
while(1):
ret,frame=cap.read()
detect(frame,minScore)
if 0xFF == ord('q'):
break
if __name__ == "__main__":
net = cv2.dnn.readNetFromCaffe("fire_deploy.prototxt",
"models/mobilenet_iter_25000.caffemodel")
video_detect("F:/work/测试集/火焰/视频/8.flv")