通过阿里云平台视频上传代码

from ultralytics import YOLO
import cv2
import time
import paho.mqtt.client as mqtt
import base64
import json

SERVER_IP = "iot-06z00h8fcx1brqy.mqtt.iothub.aliyuncs.com"  # 服务器IP
SERVER_PORT = 1883  # 端口号
ClientID = "k0slmhwja7J.imageA|securemode=2,signmethod=hmacsha256,timestamp=1706672089714|"
Username = "imageA&k0slmhwja7J"
Password = "308a32ed45e2b4a387afe4729d7337b35f91c73c66d61b91d557de536f33c2f7"  # 密文
POST_TOPIC = "/k0slmhwja7J/imageA/user/image"  # 发布
MODEL_TOPIC = "/sys/k0slmhwja7J/imageA/thing/event/property/post" #物模型主题
source = "sheeps.mp4"

# Load a model
model = YOLO("yolov8n.pt")  # load an official model


# MQTT连接回调函数
def on_connect(client, userdata, flags, rc):
    if rc == 0:
        print("Connected to MQTT broker")
    else:
        print("Failed to connect, return code", rc)


def frame_process():
    cap = cv2.VideoCapture(source)
    client = mqtt.Client(ClientID)  # 创建MQTT客户端
    client.username_pw_set(Username, Password)  # 设置用户名和密码
    client.connect(SERVER_IP, SERVER_PORT, 60)  # 连接MQTT服务器
    client.on_connect = on_connect  # 设置连接回调函数
    client.loop_start()  # 启动循环线程,处理网络通信

    while cap.isOpened():
        # 逐帧读取视频
        ret, frame = cap.read()
        if not ret:
            break  # 如果没有读取到帧,表示视频已经结束,退出循环

        # 在这里对每一帧进行目标检测
        results = model.predict(frame)  # 使用模型进行目标检测,返回检测结果
        # 处理检测结果,统计各类目标的数量等操作
        counts = {}
        for result in results:
            class_id = result.boxes.cls
            class_id = class_id.flatten().tolist()  # 转换为一维数组
            class_name = [result.names[c] for c in class_id]  # 获取对应的物体类别名称
            for name in class_name:
                if name not in counts:
                    counts[name] = 0
                counts[name] += 1
            for name, bbox in zip(class_name, result.boxes.xyxy):
                x1, y1, x2, y2 = map(int, bbox)
                cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)  # 绘制目标框
                cv2.putText(frame, name, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.9, (0, 255, 0), 2)  # 显示类别
        # 显示目标数量
        text = ', '.join([f'{name}: {count}' for name, count in counts.items()])
        cv2.putText(frame, text, (10, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)
        # 显示当前时间
        timestamp = time.strftime('%Y-%m-%d %H:%M:%S', time.localtime())
        cv2.putText(frame, timestamp, (frame.shape[1] - 400, 50), cv2.FONT_HERSHEY_SIMPLEX, 1.0, (0, 255, 0), 2)

        # 输出每个类别目标的数量
        # for name, count in counts.items():
        #     print(f'{name}: {count}')
        # 显示处理后的帧(可选)
        cv2.imshow('Frame', frame)
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
        _, img_encoded = cv2.imencode('.jpg', frame)
        # 将字节流进行base16编码
        img_base64 = base64.b16encode(img_encoded).decode()
        # 构建消息报文
        message = {
            "image": img_base64,
            "object_counts": counts  # 将类别及其数量信息添加到消息报文中
            # 其他相关信息
        }
        # 将消息报文转换为JSON字符串
        json_message = json.dumps(message)
        # 发布base16编码后的图像数据到MQTT主题
        client.publish(POST_TOPIC, json_message, qos=1)
        payload = {
            "method": "thing.event.property.post",
            "params": {
                "object_counts": text
            }
        }
        client.publish(MODEL_TOPIC, json.dumps(payload), qos=1)
    cap.release()
    cv2.destroyAllWindows()

frame_process()

  • 14
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值