yolov7-pose检测到摔倒结果后,利用巴法云将报警信息发送到微信

本文是利用巴法云提供的API,将检测到的摔倒信息发送到手机微信上面,接收到的信息是显示在巴法云公众号上面,这点自己做一下就知道了。
信息的显示分两种,一种是单纯显示报警文字,另一种是文字和图像都显示。这两种我都分享一下。
摔倒检测判断内容我是抄B站“革命的草鞋”视频教程《yolov7-pose跌倒检测》内容。
代码分三个部分:
1、判断部分加在主代码的位置
2、仅显示文字报警程序
3、同时显示文字和图片的报警程序

1、主程序判断部分

主程序是依据yolov7-pose的detect.py文件修改的。找不到对应程序放在哪里的,可以参考源文件

import alarm2wechat
import alarm_picture2wechat

def detect(opt):
    start_time = 0
    count = 0
    flag = 0
    sentedTimes = 0  # 已发送次数
    wait_time = 0  # 发送一次后的冷静时间
    #内容略
    for path, img, im0s, vid_cap in dataset:
        # 内容略
        for i, det in enumerate(pred):  # detections per image
            if webcam:
                p, s, im0, frame = path[i], '%g: ' % i, im0s[i].copy(), dataset.count
            else:
                p, s, im0, frame = path, '', im0s.copy(), getattr(dataset, 'frame', 0)
            #内容略
                for det_index, (*xyxy, conf, cls) in enumerate(reversed(det[:, :6])):
                    #内容略
                    if save_img or view_img:  # Add bbox to image
                        status = plot_skeleton_kpts_selfedit(im0, kpts=kpts, steps=3, orig_shape=im0.shape[:2])
                        #我的plot_skeleton_kpts_selfedit函数返回的是一个'FALL'或者'NOT FALL'的判断结果
                        #判断内容可以参考B站“革命的草鞋”视频教程《yolov7-pose跌倒检测》
                        if status == 'FALL':#如果摔倒了就绘制框
                            flag = 1
                            #内容略
                        else:
                            flag = 0
	         #以下部分是发送部分相关代码
            if flag == 1 and count == 0:
                start_time = time.time()
                count = count + 1
            elif flag == 1:
                count = count + 1

            if time.time() - start_time >= 3:
                if count >= 35:#在3秒内,检测到35次跌倒,那么就发送警报
                    if sentedTimes == 0:
                        # alarm2wechat.toweChat()#发送警报
                        alarm_picture2wechat.picture2weChat(im0)#发送警报和图
                        sentedTimes = 1#发送次数置1
                        wait_time = time.time()#等待时间开始计时
                    elif time.time() - wait_time >= 50:#距离上次发送过了50秒
                        # alarm2wechat.toweChat()#发送警报
                        alarm_picture2wechat.picture2weChat(im0)#发送警报和图
                        wait_time = time.time()  # 再次等待时间开始计时,避免发送频率过高

                count = 0
                start_time = time.time()

在这段代码里面,我把跟判断不相关的都删掉了,在“#以下部分是发送部分相关代码”这个注释往上,除了定义变量部分,仅仅是为了表示这部分内容的位置应该在哪里。
这部分代码里面,alarm_picture2wechat.picture2weChat(im0)函数的作用,是同时发送文字信息和图像报警,alarm2wechat.toweChat()就是仅仅发送文字信息。注意这个im0参数,这个才是图像帧。

2、仅发送文字信息的alarm2wechat函数

import requests
import time
import json
def toweChat():
    # API地址
    api_url = 'https://apis.bemfa.com/va/postJsonMsg'

    # 构造JSON数据
    data = {
        "uid": "XXXX",
        "topic": "xxxx",
        "type": 3,
        "msg": "FALL alarm",
        "wemsg":f'有人摔倒. ({time.time():.3f}s)'
    }

    # 发起POST请求
    headers = {
        "Content-Type": "application/json; charset=utf-8"
    }

    try:
        response = requests.post(api_url, json=data, headers=headers)
        # print("状态码:", response.status_code)
        # print("响应头:", response.headers)
        # print("响应内容:", response.text)

        # 如果需要,可以检查响应内容是否包含错误消息
        if 'error' in response.json():
            print("错误消息:", response.json()['error'])

    except requests.exceptions.RequestException as e:
        print("发送报警信息时出错:", e)

if __name__ == '__main__':
    toweChat()

3、下面是同时向手机发送图片和文字信息的程序

'''
下面这段代码,是用来配合发送视频帧的
'''
import requests
import cv2

def picture2weChat(frame):

    # 巴法云API地址
    api_url = 'http://images.bemfa.com/upload/v1/upimages.php'


    # 设置HTTP头部字段
    headers = {
        'Content-Type': 'image/jpeg',  # 根据API要求设置为image/jpeg
        'Authorization': 'xxxxx',
        'Authtopic': 'xxxxx',
        'wechatmsg': 'danger'  # 注意:这里不能用汉字,奇怪
    }

    _, jpeg_frame = cv2.imencode('.jpg', frame)
    image_data = jpeg_frame.tobytes()

    # 发送POST请求
    response = requests.post(api_url, data=image_data, headers=headers)

    # 检查响应
    if response.status_code == 200:
        print("图片上传成功")
        print(response.text)  # 打印服务器返回的响应内容
    else:
        print("图片上传失败")
        print(f"错误代码: {response.status_code}")
        print(response.text)  # 打印服务器返回的错误信息

if __name__ == '__main__':
    picture2weChat()

对于白嫖的东西,我表示感谢,感谢yolov7、革命的草鞋及巴法云

  • 3
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值