基于python和CV2的视频活动检测

一、简介

调用摄像头获取实时视频,当视频内有运动目标时将当前帧保存到本地。每天在本地新建一个文件夹用于存取当天的内容。

二、代码

 1.安装python,导入opencv库

pip install opencv-python

 

2.详细代码

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()

 

posted @ 2022-07-30 08:30  StarZhai  阅读(573)  评论(0编辑  收藏  举报