python 监控摄像头 截图
#!/usr/bin/python3.6 # -*- coding: utf-8 -*- ''' @ Copyright (C) 2019 @ @ env stetup:pip3 install opencv-python @ @ 免费知识星球:[一番码客-积累交流](https://t.zsxq.com/NRVBURr) @ 微信公众号:一番码客 @ 微信:Efon-fighting @ 网站:http://efonfighting.imwork.net ''' import cv2 import numpy, os, sys def isPicChanged(dividePar, pointDelta, judgeTh): ''' 通过前后帧对比,判断画面是否改变 :param dividePar = 4 # 对比隔点,减少计算量 :param pointDelta = 50 # 像素点的差异大于该值认为是差异点 :param judgeTh = 64 # 判断变化画面大小的阈值:画面(1/judgeTh) ''' capIdx = 0 # 截图命名 camIdx = -1 while (int(camIdx) < 0 or int(camIdx) > 10) : print("enter camera index in 0 and 10:") camIdx = int(input()) if not (os.path.isdir('cap')): # 创建存放截图的文件夹 os.system('mkdir -p {}'.format("cap")) cap = cv2.VideoCapture(camIdx) #调整参数实现读取视频或调用摄像头 ret, frameBak = cap.read() for i in range(10): #刚打开相机时,曝光不稳定,清理10张 ret, frameBak = cap.read() frame = frameBak frameWidth = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) frameHeight = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) print("frameWidth:{},frameHeight:{}".format(frameWidth,frameHeight)) if frameWidth == 0: exit("camera is not available.") while True: absCnt = 0 frameBak = frame ret, frame = cap.read() for wIdx in range(int(frameWidth/dividePar)) : for hIdx in range(int(frameHeight/dividePar)) : if abs(int(frameBak[hIdx*dividePar][wIdx*dividePar][2]) - int(frame[hIdx*dividePar][wIdx*dividePar][2])) > pointDelta : absCnt += 1 cv2.imshow("cap", frame) if absCnt > ( frameWidth * frameHeight ) / (dividePar * dividePar) / (judgeTh * judgeTh) : capIdx += 2 cv2.imwrite('cap/cap_{}.jpg'.format(capIdx), frame) cv2.imwrite('cap/cap_{}.jpg'.format(capIdx+1), frameBak) print("get a pic:{}".format(capIdx/2)) if cv2.waitKey(1) & 0xff == ord('q'): break cap.release() cv2.destroyAllWindows() if __name__ == "__main__": #这里可以判断,当前文件是否是直接被python调用执行 isPicChanged(4, 50, 64)
import cv2 import time import os # 定义摄像头对象,其参数0表示第一个摄像头 camera = cv2.VideoCapture(0) # 测试用,查看视频size width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)) size = width,height #打印一下分辨率 print(repr(size)) print("ObjectTrack is running!") #设置一下帧数和前背景 fps = 5 pre_frame = None while (1): time.sleep(0.5) start = time.time() # 读取视频流 ret, frame = camera.read() # 转灰度图 gray_pic = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) if not ret: print("打开摄像头失败") break end = time.time() #查看视频窗口 #cv2.imshow("capture", frame) # 运动检测部分,看看是不是5FPS seconds = end - start if seconds < 1.0 / fps: time.sleep(1.0 / fps - seconds) gray_pic = cv2.resize(gray_pic, (480, 480)) # 用高斯滤波进行模糊处理 gray_pic = cv2.GaussianBlur(gray_pic, (21, 21), 0) # 如果没有背景图像就将当前帧当作背景图片 if pre_frame is None: pre_frame = gray_pic else: # absdiff把两幅图的差的绝对值输出到另一幅图上面来 img_delta = cv2.absdiff(pre_frame, gray_pic) # threshold阈值函数(原图像应该是灰度图,对像素值进行分类的阈值,当像素值高于(有时是小于)阈值时应该被赋予的新的像素值,阈值方法) thresh = cv2.threshold(img_delta, 30, 255, cv2.THRESH_BINARY)[1] # 用一下腐蚀与膨胀 thresh = cv2.dilate(thresh, None, iterations=2) # findContours检测物体轮廓(寻找轮廓的图像,轮廓的检索模式,轮廓的近似办法) contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in contours: # 设置敏感度 # contourArea计算轮廓面积 if cv2.contourArea(c) < 1000: continue else: print("Get It!!!") # 保存图像 TFile1 = time.strftime('%Y-%m-%d', time.localtime(time.time())) TFile2 = time.strftime('%H.%M', time.localtime(time.time())) path="G:/test/objectTrack/"+TFile1+"/"+TFile2 isExists=os.path.exists(path) if not isExists: # 如果不存在则创建目录创建目录操作函数 os.makedirs(path) print (path+' Saved!') TI = time.strftime('%m%d-%H.%M.%S', time.localtime(time.time())) cv2.imwrite(path+ "/"+TI+ '.jpg', frame) print(TI) break pre_frame = gray_pic if cv2.waitKey(1) & 0xFF == ord('q'): break # release()释放摄像头 camera.release() # destroyAllWindows()关闭所有图像窗口 cv2.destroyAllWindows()
import cv2 import time import os # 定义摄像头对象,其参数0表示第一个摄像头 camera = cv2.VideoCapture(0) # 测试用,查看视频size width = int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)) height = int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)) size = width,height #打印一下分辨率 print(repr(size)) print("ObjectTrack is running!") #设置一下帧数和前背景 fps = 5 pre_frame = None while (1): time.sleep(0.5) start = time.time() # 读取视频流 ret, frame = camera.read() # 转灰度图 gray_pic = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) if not ret: print("打开摄像头失败") break end = time.time() #查看视频窗口 #cv2.imshow("capture", frame) # 运动检测部分,看看是不是5FPS seconds = end - start if seconds < 1.0 / fps: time.sleep(1.0 / fps - seconds) gray_pic = cv2.resize(gray_pic, (480, 480)) # 用高斯滤波进行模糊处理 gray_pic = cv2.GaussianBlur(gray_pic, (21, 21), 0) # 如果没有背景图像就将当前帧当作背景图片 if pre_frame is None: pre_frame = gray_pic else: # absdiff把两幅图的差的绝对值输出到另一幅图上面来 img_delta = cv2.absdiff(pre_frame, gray_pic) # threshold阈值函数(原图像应该是灰度图,对像素值进行分类的阈值,当像素值高于(有时是小于)阈值时应该被赋予的新的像素值,阈值方法) thresh = cv2.threshold(img_delta, 30, 255, cv2.THRESH_BINARY)[1] # 用一下腐蚀与膨胀 thresh = cv2.dilate(thresh, None, iterations=2) # findContours检测物体轮廓(寻找轮廓的图像,轮廓的检索模式,轮廓的近似办法) contours, hierarchy = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) for c in contours: # 设置敏感度 # contourArea计算轮廓面积 if cv2.contourArea(c) < 1000: continue else: print("Get It!!!") # 保存图像 TFile1 = time.strftime('%Y-%m-%d', time.localtime(time.time())) TFile2 = time.strftime('%H.%M', time.localtime(time.time())) path="G:/test/objectTrack/"+TFile1+"/"+TFile2 isExists=os.path.exists(path) if not isExists: # 如果不存在则创建目录创建目录操作函数 os.makedirs(path) print (path+' Saved!') TI = time.strftime('%m%d-%H.%M.%S', time.localtime(time.time())) cv2.imwrite(path+ "/"+TI+ '.jpg', frame) print(TI) break pre_frame = gray_pic if cv2.waitKey(1) & 0xFF == ord('q'): break # release()释放摄像头 camera.release() # destroyAllWindows()关闭所有图像窗口 cv2.destroyAllWindows()