使用Python和CodeProject.AI服务器的IP摄像机进行对象检测

目录

介绍

设置Wyze cam以提供RTSP视频流

使用Python处理RTSP视频流

流的位置

查看流

处理流

结论


介绍

我们中的许多人使用IP摄像机进行监控。有数百台相机可用,从便宜的(不是那么好)到功能齐全的惊人,当然,价格昂贵。我很便宜,所以我使用Wyze cam。它们是$US 30,防水且无线。

我的目标——我的需要,真的——是有一个系统,可以检测浣熊何时在我的阳台上,这样我就可以把火和硫磺倾泻到破坏和毁灭的小毛茸茸的带来者身上。

我和其中一个驼背的恶魔摊牌过。他盯着我,嘶嘶作响,退到角落里。我盯着他,拿着一大块木头,不知道我要用它做什么。我们在澳大利亚没有浣熊。我知道这件事很麻烦,但我只是不确定有多少。

微风轻轻拂过。一只黄蜂飞了过去。然后飞了回来,落在了那个生物奇怪的孩子般的手上。哦,这会很有趣,我想。它从不眨眼。相反,它甩了甩长胡须的鼻子,用泛黄的牙齿从手上拔下黄蜂,开始咀嚼。自始至终,他的眼睛从未从我身上移开。你觉得幸运吗,朋克?嗯,你呢?

本文将为我们提供使用CodeProject.AI服务器检测浣熊的基础知识。我们将设置一个带有测试版固件的Wyze cam来公开RTSP流,使用少量Python吸收该流,然后将帧从流发送到 CodeProject.AI服务器以执行对象检测。第二部分将处理专门训练一个专门用于检测浣熊的模型。

设置Wyze cam以提供RTSP视频流

Wyze不提供从他们的相机开箱即用的视频流访问,但他们确实提供了启用RTSP(实时流协议)的测试版固件RTSP 是一种流媒体协议,Wyze cams的实现最初作为测试版发布,但后来由于稳定性问题而被删除,然后重新发布、更新、针对v3发布,然后删除。它现在处于奇怪的不确定状态,但仍然可以使用以下链接从Wyze下载固件。

要更新相机,请下载相应的固件并按照说明进行操作。只需记住将bin文件重命名为demo.binV2)或demo_wcv3.binV3),并将文件放在SD卡的根目录中。

刷新更新固件并重新启动相机后,您将在Wyze应用程序中看到RTSP选项。只需选择相机,转到设置”→高级设置RTSP设置位于底部。

使用Python处理RTSP视频流

流的位置

RTSP流的位置是使用表单的URL指定的

rtsp://<username>:<password>@<ip address>:<port>/<endpoint>

Wyze应用程序的相机设置的高级设置中选择“RTSP”,系统将提示您生成URL。选择一个用户名和密码,您的URL将显示类似于

rtsp://user:pass@192.168.0.189/live

在这里,我选择了用户通行证作为我的超安全凭据。cam位于IP地址192.160.0.189上,它将使用默认端口。

查看流

我们使用imutils.video抓取流并且OpenCV显示每一帧。这是令人尴尬的简单代码:

import cv2
import imutils
from imutils.video import VideoStream

rtsp_url = "rtsp://user:pass@192.168.0.189/live"

def main():

    vs = VideoStream(rtsp_url).start()    # Open the RTSP stream

    while True:

        # Grab a frame at a time
        frame = vs.read()
        if frame is None:
            continue

        # Resize and display the frame on the screen
        frame = imutils.resize(frame, width = 1200)
        cv2.imshow('WyzeCam', frame)
    
        # Wait for the user to hit 'q' for quit
        key = cv2.waitKey(1) & 0xFF
        if key == ord('q'):
            break

    # Clean up and we're outta here.
    cv2.destroyAllWindows()
    vs.stop()

if __name__ == "__main__":
    main()

处理流

查看是一回事,但让我们让它做一些有用的事情:添加对象检测。

  • 第 1 步。安装 CodeProject.AI服务器
  • 第 2 步。将视频中的每一帧发送到CodeProject.AI服务器进行处理。
  • 第 3 步。显示结果

首先,在代码中添加一个'do_detection'方法。此方法将获取一个帧,将其转换为适合发送到CodeProject.AI服务器的形式,执行检测,然后使用检测到的项目的标签和边界框注释框架。

import io
import requests
import numpy as np
from PIL import Image, ImageDraw

def do_detection(image):
   
    # Convert to format suitable for a POST
    buf = io.BytesIO()
    image.save(buf, format='PNG')
    buf.seek(0)
    
    # Send the image to CodeProject.AI Server and do some object detection.
    # Better to have a session object created once at the start and closed at
    # the end, but we keep the code simpler here for demo purposes    
    with requests.Session() as session:
        response = session.post(opts.endpoint("vision/detection"),
                                files={"image": ('image.png', buf, 'image/png') },
                                data={"min_confidence": 0.5}).json()

    # Get the predictions (but be careful of a null return)
    predictions = response["predictions"]
    if (predictions is None):
        predictions = []

    # Draw each bounding box that was returned by the AI engine
    draw = ImageDraw.Draw(image)
    for object in predictions:
        label = object["label"]
        conf  = object["confidence"]
        y_max = int(object["y_max"])
        y_min = int(object["y_min"])
        x_max = int(object["x_max"])
        x_min = int(object["x_min"])

        draw.rectangle([(x_min, y_min), (x_max, y_max)], outline="red", width=5)
        draw.text((x_min, y_min), f"{label}")
        draw.text((x_min, y_min - 10), f"{round(conf*100.0,0)}")

    # ...and we're done
    return image

接下来,我们将获取从RTSP流中检索到的每个图像,将其转换为我们可以POSTCodeProject.AI Server检测API的格式,然后将结果转换回我们最初接收帧的格式。

我们的main成为:

def main():

   # Open the RTSP stream
   vs = VideoStream(opts.rtsp_url).start() 

   while True:

       # Grab a frame at a time
       frame = vs.read()
       if frame is None:
           continue

       # Convert the frame to an image, pass to the detector, then convert back
       # to the original format so we can draw it
       image = Image.fromarray(frame)
       image = do_detection(image)
       frame = np.asarray(image)

       # Resize and display the frame on the screen
       frame = imutils.resize(frame, width = 1200)
       cv2.imshow('WyzeCam', frame)
   
       # Wait for the user to hit 'q' for quit
       key = cv2.waitKey(1) & 0xFF
       if key == ord('q'):
           break

   # Clean up and we're outta here.
   cv2.destroyAllWindows()
   vs.stop()

结论

通过获取库存Wyze cam并更新其固件,我们能够访问RTSP流进行处理。从此流中提取帧的少量Python代码允许我们将帧发送到CodeProject.AI服务器进行对象检测。

该代码包含在CodeProject.AI服务器源代码中(在Demos/Python/ObjectDetect下)。整个文件的长度不到100行。

我们编写CodeProject.AI服务器是为了消除设置AI系统和项目的痛苦。我们处理运行时,打包并准备好所有部分,以便我们可以直接跳过有趣的部分,例如检测垃圾熊猫。

下载CodeProject.AI 并试一试。添加您自己的模块,将其与您的应用程序集成,训练一些自定义模型并使用它来学习一些有关人工智能的知识。

https://www.codeproject.com/Articles/5344693/Object-Detection-with-an-IP-camera-using-Python-an

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值