# _*_ coding:UTF-8 _*_
#开发作者 : ZhangRong z00520111
#开发时间 : 2020/5/13 10:09
#文件名称 : firetest.py
#开发工具 : PyCharm
#Description:
#Copyright @ Huawei Technologies Co., Ltd. 2019-2020. All rights reserved.
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):
# root 表示当前正在访问的文件夹路径
# dirs 表示该文件夹下的子目录名list
# files 表示该文件夹下的文件list
# 遍历文件
for f in files:
fileaddress.append(os.path.join(root, f))
# # 遍历文件
# for f in files:
# print(os.path.join(root, f))
#
# # 遍历所有的文件夹
# for d in dirs:
# print(os.path.join(root, d))
return files, fileaddress
def all_detect(img_path):
files, fileaddress = walkFile(img_path)
# print(fileaddress)
global file
global nfile
for index, f in enumerate(fileaddress):
if str(f).find('.jpg') != -1:
# isexsist(resultpath)
# isexsist(nresultpath)
print(f)
filepre = f[:f.find('_')]
# print(f[:f.find('.')])
# print( files[index][:files[index].find('.')] )
# file = open(resultpath + files[index][:files[index].find('.')] + ".txt", "a+")
# nfile = open(nresultpath + "n" + files[index][:files[index].find('.')] + ".txt", "a+")
origimg = cv2.imdecode(np.fromfile(f, dtype=np.uint8), cv2.IMREAD_UNCHANGED)
resultimage, isshow, rows, cols = detect(origimg, minScore)
# cv2.namedWindow("resized", 0);
# cv2.resizeWindow("resized", cols, rows);
# if isshow == 1:
# cv2.imshow("resized", resultimage)
# cv2.waitKey(0)
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, (int(brandleft), int(brandtop)), (int(brandright), int(brandbottom)), (255, 0, 0), 2)
# cv2.rectangle(img, (left-int(weight/4), top-int(height/3)-200), (right+int(weight/4), bottom+int(height/3*2)-200), (255, 0, 0), 2)
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")
# all_detect("F:/work/测试集/fire/")
video_detect("F:/work/测试集/火焰/视频/8.flv")
Caffe-ssd测试模型代码
最新推荐文章于 2024-01-20 16:39:29 发布