基于Opencv实现入侵检测,加快视频处理速度
# -*- coding: utf-8 -*-
import cv2
import time
import datetime
cap = cv2.VideoCapture(0)
avg = None
lastUploaded = datetime.datetime.now()
motionCounter = 0
time.sleep(10)
while(True):
# 逐帧获取图像
tiestamp = datetime.datetime.now()
ret, frame = cap.read()
text = "unoccupied"
if not ret:
break
# 对每帧图像进行操作
gray = cv2.resize(frame,(500,500))#调整大小
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#变成灰色图像
gray = cv2.GaussianBlur(gray,(21,21),0)#高斯滤波
if avg is None:
avg = gray.copy().astype("float")
continue
cv2.accumulateWeighted(gray,avg,0.5)
# 显示处理后的图像
cv2.imshow('frame',gray)
#计算当前帧与第一帧的区别
frameDelta = cv2.absdiff(gray,cv2.convertScaleAbs(avg))
# cv2.imshow('first2',frameDelta)
#填充孔洞
thresh = cv2.threshold(frameDelta, 45, 255, cv2.THRESH_BIN