最近做了一个关于火灾烟雾检测,简单的做成了4分类,然后输入视频,输出是否失火。
因为做的是分类,没做检测,直接分类判断是否失火,于是将视频切成单帧进行识别来判断是否失火,从网上找了一下这方面的代码。
# -*- coding: utf-8 -*- from PIL import Image import cv2 import os #将视频按帧切成图片 input_path = '100.mp4' frame_path = 'save' if not os.path.exists(frame_path): os.mkdir(frame_path) cap = cv2.VideoCapture(input_path) n_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) for i in range(n_frames): ret, frame = cap.read() imgname ='{}_{}_{:0>6d}.jpg'.format(100, 100, i) imgpath = os.sep.join([frame_path, imgname]) print('exported {}!'.format(imgpath)) #画图, cv2.line(frame,(300,300),(600,600),(255,0,0),50) cv2.imwrite(imgpath, frame) cap.release() # 执行结束释放资源 #将图片合并成视频 img_root = 'save/' fps = 30 #保存视频的FPS, fourcc = cv2.VideoWriter_fourcc('D', 'I', 'V', 'X') videoWriter = cv2.VideoWriter('E:\Personal\Desktop\test\pill\saveVideo.avi',fourcc,fps,(1920,1080))#最后一个是保存图片的尺寸 names = os.listdir(img_root) print (names) for name in names: frame = cv2.imread('E:\Personal\Desktop\test\pill\save\' + name ) videoWriter.write(frame) videoWriter.release()