python opencv 背景减除法 跌倒检测

python opencv 背景减除法 跌倒检测

效果

摔倒检测

原视频数据下载

点击下载

代码

#!/usr/bin/ python
# -*- encoding: utf-8 -*-
'''
@文件    :1.py
@说明    :
@时间    :2022/03/14 09:50:29
@作者    :刘子沫
@邮箱    :spiritai@qq.com
@版本    :1.0
'''


import cv2
import numpy as np
import time
from PIL import Image, ImageDraw, ImageFont

def cv2ImgAddText(img, text, left, top, textColor=(0, 255, 0), textSize=20):
    if (isinstance(img, np.ndarray)):  # 判断是否OpenCV图片类型
        img = Image.fromarray(cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
    # 创建一个可以在给定图像上绘图的对象
    draw = ImageDraw.Draw(img)
    # 字体的格式
    fontStyle = ImageFont.truetype(
        "font/simsun.ttc", textSize, encoding="utf-8")
    # 绘制文本
    draw.text((left, top), text, textColor, font=fontStyle)
    # 转换回OpenCV格式
    return cv2.cvtColor(np.asarray(img), cv2.COLOR_RGB2BGR)

cam = cv2.VideoCapture('1.avi') #读取摄像头
cam.set(3, 640) # set video widht
cam.set(4, 480) # set video height
scale=0

#背景减除
# kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE,(1,1))
fg = cv2.createBackgroundSubtractorMOG2()  
# fgbg = cv2.createBackgroundSubtractorKNN(detectShadows = True)
# fg = cv2.bgsegm.createBackgroundSubtractorGMG()
# fg =  cv2.createBackgroundSubtractorKNN()
# history = 5
# fgbg.setHistory(history)
while True:
    time.sleep(0.02)
    ret,img = cam.read()
    if not ret:break

    #canny 边缘检测
    image= img.copy()
    blurred = cv2.GaussianBlur(image, (3, 3), 0)
    gray = cv2.cvtColor(blurred, cv2.COLOR_RGB2GRAY)
    xgrad = cv2.Sobel(gray, cv2.CV_16SC1, 1, 0) #x方向梯度
    ygrad = cv2.Sobel(gray, cv2.CV_16SC1, 0, 1) #y方向梯度
    edge_output = cv2.Canny(xgrad, ygrad, 50, 150)
    # edge_output = cv2.Canny(gray, 50, 150)
    cv2.imshow("Canny Edge", edge_output)

    # edge_output = cv2.dilate(edge_output,cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (8,3)),iterations=1) 
    #背景减除
    fgmask = fg.apply(edge_output)
    # cv2.imshow("fgmask", fgmask)
    
    #闭运算
    hline = cv2.getStructuringElement(cv2.MORPH_RECT, (1, 4), (-1, -1)) #定义结构元素,卷积核
    vline = cv2.getStructuringElement(cv2.MORPH_RECT, (4, 1), (-1, -1))
    result = cv2.morphologyEx(fgmask,cv2.MORPH_CLOSE,hline)#水平方向
    result = cv2.morphologyEx(result,cv2.MORPH_CLOSE,vline)#垂直方向
    cv2.imshow("result", result)


    # erodeim = cv2.erode(th,cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (3,3)),iterations=1)  # 腐蚀 
    dilateim = cv2.dilate(result,cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (4,4)),iterations=1) #膨胀
    # cv2.imshow("dilateimfgmask", dilateim)
    # dst = cv2.bitwise_and(image, image, mask= fgmask)
    # cv2.imshow("Color Edge", dst)
    #查找轮廓
    contours, hier = cv2.findContours(dilateim, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_NONE) 
    for c in contours:
        if cv2.contourArea(c) > 1200:
            (x,y,w,h) = cv2.boundingRect(c)
            if scale==0:scale=-1;break
            scale = w/h
            cv2.putText(image, "scale:{:.3f}".format(scale), (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
            cv2.drawContours(image, [c], -1, (255, 0, 0), 1)
            cv2.rectangle(image,(x,y),(x+w,y+h),(0,255,0),1)
            image = cv2.fillPoly(image, [c], (255, 255, 255)) #填充

    #根据人体比例判断       
    if scale >0 and scale <1:
        img = cv2ImgAddText(img, "Walking 行走中", 10, 20, (255, 0 , 0), 30)#行走中
        # cv2.putText(img, "Walking 行走中", (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)#行走中
    if scale >0.9 and scale <2:
        img = cv2ImgAddText(img, "Falling 中间过程", 10, 20, (255, 0 , 0), 30)#跌倒中
        # cv2.putText(img, "Falling 跌倒中", (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)#跌倒中
    if scale >2:
        img = cv2ImgAddText(img, "Falled 跌倒了", 10, 20, (255, 0 , 0), 30)#跌倒了
        # cv2.putText(img, "Falled 跌倒了", (10, 30),cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)#跌倒了

    cv2.imshow('test',image)
    cv2.imshow('image',img)



    k=cv2.waitKey(1)&0xFF
    if k==27:
        break

cv2.destroyAllWindows()

yolov3+lenet-cnn跌倒检测

yolov3+lenet-cnn跌倒检测 地址点击

如有问题咨询QQ群686070107

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

图像处理大大大大大牛啊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值