[Python Study Notes]物体运动检测

Posted 刘六六

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了[Python Study Notes]物体运动检测相关的知识,希望对你有一定的参考价值。

基于opencv的cv2模块实现

\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'
>>文件: iot_client.py
>>作者: liu yang
>>邮箱: liuyang0001@outlook.com
>>博客: www.cnblogs.com/liu66blog
>>博客: liuyang1.club (抱歉,域名备案中,稍后恢复访问)

\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'\'

#!/usr/bin/env python
# -*- coding: utf-8 -*-

import cv2
import numpy as np
import easygui
import datetime
from twilio.rest import Client

# 打开摄像头
camera= cv2.VideoCapture(0)
# 如果摄像头打开失败
if camera.isOpened() == False:
    # 给与友好性提示
    easygui.msgbox("\\n\\n\\n\\n\\n\\n                 请保证摄像头可以正常被打开,请检查硬件后重新运行",title=\'提示框\',ok_button=\'确定\')
# 得到摄像头的图像尺寸
size = (int(camera.get(cv2.CAP_PROP_FRAME_WIDTH)),
        int(camera.get(cv2.CAP_PROP_FRAME_HEIGHT)))
# 打印尺寸
print(\'size:\'+repr(size))
es = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (5,4))
kernel = np.ones((5,5),np.uint8)
background = None
flag = 0
while True:
    # 有没有检测到的文本
    text = "Undetected"
    # 读取摄像头的参数
    grabbed , frame_lwpCV=camera.read()
    try:
        # 将图像转换为RGB
        gray_lwpCV = cv2.cvtColor(frame_lwpCV,cv2.COLOR_RGB2GRAY)
        # 将图像进行高斯滤波,去除噪点
        gray_lwpCV = cv2.GaussianBlur(gray_lwpCV,(25,25),3)
    except cv2.error:
        break

    # 判断是否有标准的背景图,如果没有就将上面摄像头采集的第一帧的图像作为背景图
    if background is None:
        background = gray_lwpCV
        continue
    # 将两个图像进行比较
    diff = cv2.absdiff(background,gray_lwpCV)
    diff = cv2.threshold(diff,50,255,cv2.THRESH_BINARY)[1]
    # 进行3次膨胀
    diff = cv2.dilate(diff,es,iterations=3)

    # 忽略掉一些很小的因素
    image , contours , hierarchy = cv2.findContours(diff.copy(),cv2.RETR_TREE,cv2.CHAIN_APPROX_SIMPLE)
    for c in contours:
        # 如果变化的狂过小,则忽略
        if cv2.contourArea(c) < 2000:
            continue
        (x, y, w, h) = cv2.boundingRect(c)
        cv2.rectangle(frame_lwpCV, (x, y), (x + w, y + h), (0, 255, 0), 2)
        # 有物体闯入到背景中,以文本标记
        text = "Detected"

    # 如果文本标记为无
    if text == "Undetected" :
        # 在图像上标出
        cv2.putText(frame_lwpCV,"Motion: {}".format(text),(10,20),
                           cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,255,0),2)
        # 放置时间戳
        cv2.putText(frame_lwpCV,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        (10,frame_lwpCV.shape[0]-10),cv2.FONT_HERSHEY_SIMPLEX,0.35,(0,255,0),2)

    # 如果检测到
    if text == "Detected" :
        cv2.putText(frame_lwpCV,"Motion: {}".format(text),(10,20),
                           cv2.FONT_HERSHEY_SIMPLEX,0.5,(0,0,255),2)
        cv2.putText(frame_lwpCV,datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S"),
        (10,frame_lwpCV.shape[0]-10),cv2.FONT_HERSHEY_SIMPLEX,0.35,(0,255,0),2)
        # 蒋告警标志位置为1
        flag=1

    # 判断告警标志位
    if flag == 1:
        # 接入一些接口,进行对用户的警示,微信,丁丁,短信 ...等等
        # 然后将标志位置为0
        pass

    # 显示图像
    cv2.imshow(\'contours\',frame_lwpCV)
    # 灰度图像的显示
    # cv2.imshow(\'dis\',diff)

    # 添加退出键--q
    # 按下退出本次监测
    key = cv2.waitKey(1) & 0xff
    if key == ord(\'q\'):
        break

# 退出后释放摄像头
camera.release()
cv2.destroyAllWindows()


# 声明:该代码源于腾讯课堂-动脑学院-Python公开课,并加以适当修改

以上是关于[Python Study Notes]物体运动检测的主要内容,如果未能解决你的问题,请参考以下文章

[Python Study Notes]内存信息

[Python Study Notes]cpu信息

[Python Study Notes]计算器

[Python Study Notes]折线图绘制

[Python Study Notes]实现对鼠标控制

[Python Study Notes]水平直方图