[Python Study Notes]物体运动检测

基于opencv的cv2模块实现

‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘‘
>>文件: iot_client.py
>>作者: liu yang
>>邮箱: [email protected]
>>博客: 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公开课,并加以适当修改

原文地址:https://www.cnblogs.com/liu66blog/p/8503224.html

时间: 2024-11-08 19:31:26

[Python Study Notes]物体运动检测的相关文章

[Python Study Notes]正则表达式

正则表达式 正则表达式是一个特殊的字符序列,它能帮助你方便的检查一个字符串是否与某种模式匹配. Python 自1.5版本起增加了re 模块,它提供 Perl 风格的正则表达式模式. re 模块使 Python 语言拥有全部的正则表达式功能. compile 函数根据一个模式字符串和可选的标志参数生成一个正则表达式对象.该对象拥有一系列方法用于正则表达式匹配和替换. re 模块也提供了与这些方法功能完全一致的函数,这些函数使用一个模式字符串做为它们的第一个参数. 本章节主要介绍Python中常用

[Python Study Notes]计算cpu使用率

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu使用率.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]计算cpu使用率v0.1

V0.1 更新日志: 1.加入平台判断,支持windows与linux 2.自动清屏显示,显示更加直观 ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu使用率.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''

[Python Study Notes]磁盘分区和io性能

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 磁盘信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]内存信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 内存信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]电池信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 电池信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]cpu信息

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: cpu信息.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]进程信息(丁丁软件监控进程,http-post)

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: 进程信息.py >>作者: liu yang >>邮箱: [email protected] ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

[Python Study Notes]CS架构远程访问获取信息--Client端

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' >>文件: ps_client.py >>作者: liu yang >>邮箱: [email protected] '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''