spyder python下载opencv+python检测⼊侵物体
opencv+python检测⼊侵物体
去年暑假在家的时候,参考别⼈的博客,编写了这个程序,本想着继续完善⼀下 ,却⼀直没时间做。偶然翻到了这个程序,决定放上来。 因为时间太久,我也没办法把当初参考的博客放上来(其实是因为懒得),如果⼤家需要,我会放上链接。
环境:opencv2.4+python2.7
编辑器:spyder
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
import cv2
import time
import datetime
cap = cv2.VideoCapture(0)
avg = None
lastUploaded = w()
motionCounter = 0
time.sleep(10)
while(True):
# 逐帧获取图像
tiestamp = w()
ret, frame = ad()
text = "unoccupied"
if not ret:
break
# 对每帧图像进⾏操作
gray = size(frame,width=500)#调整⼤⼩
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)#变成灰⾊图像
gray = cv2.GaussianBlur(gray,(21,21),0)#⾼斯滤波
if avg is None:
avg = py().astype("float")
continue
cv2.accumulateWeighted(gray,avg,0.5)
# 显⽰处理后的图像
cv2.imshow('frame',gray)
#计算当前帧与第⼀帧的区别
frameDelta = cv2.absdiff(vertScaleAbs(avg))
# cv2.imshow('first2',frameDelta)
#填充孔洞
thresh = cv2.threshold(frameDelta, 45, 255, cv2.THRESH_BINARY)[1]
thresh = cv2.dilate(thresh, None, iterations=2)
cv2.imshow('thresh',thresh)
#查轮廓
contours, hierarchy = cv2.py(), cv2.RETR_EXTERNAL,cv2.CHAIN_APPROX_SIMPLE)[-2:]
# cv2.imshow('thresh2',py())
for c in contours:
# if the contour is too small, ignore it
urArea(c) < 500:
continue
# 计算轮廓的边界框,在当前帧中画出该框
(x, y, w, h) = cv2.boundingRect(c)
cv2.imshow('found',frame)
cv2.imshow('found',frame)
text = "Occupied"
cv2.putText(frame, "Room Status: {}".format(text), (10, 20),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
cv2.putText(frame, w().strftime("%A %d %B %Y %I:%M:%S%p"),
(10, frame.shape[0] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.35, (0, 0, 255), 1)
#if text == "Occupied":
# if (timestamp-lastUploaded).second>=2:
# motionCounter+=1
#if motionCounter>=23:
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# When everything done, release the capture
cv2.destroyAllWindows()
⾥⾯调⽤的是笔记本⾃带的摄像头来获取图像,如果想要采⽤别的⽅式,需要更改⼀下。如果效果要想好⼀点⼉,可能还需要调整⼀下参数。程序已经测过,没有问题。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论