使用CodeProject.AI Server对视频文件执行对象检测

目录

介绍

设置

代码

总结


介绍

在物体检测或面部识别领域处理实时网络摄像头馈送给予了大量关注。有一些用例用于处理已保存或下载的视频中的视频,因此让我们快速演练一下如何使用CodeProject.AI Server处理视频剪辑。

为此,我们将使用Python,特别是OpenCV库,因为它内置了对许多视频内容的支持。目标是加载一个视频文件,针对对象检测器运行它,并生成一个包含对象和时间戳的文件,就像我们在查看视频本身时覆盖了检测边界框一样。

设置

这将是最基本的,因此我们可以专注于使用CodeProject.AI Server,而不是专注于繁琐的设置步骤。我们将在CodeProject.AI Server中运行YOLOv5 6.2对象检测模块。该模块提供了不错的性能,但最方便的是,它在runtimes/ 文件夹中的共享Python虚拟环境设置中运行。我们自己会无耻地使用同样的venv。

我们所有的代码都将在CodeProject.AI Server代码库中运行,我们的演示位于video_process.py文件中的/demos/clients/Python/ObjectDetect 文件夹下。

要运行此代码,请转到/demos/clients/Python/ObjectDetect 文件夹并运行

# For Windows
 ..\..\..\..\src\runtimes\bin\windows\python39\venv\Scripts\python video_process.py

# for Linux/macOS
 ../../../../src/runtimes/bin/macos/python38/venv/bin/python video_process.py 

若要停止程序,请在启动文件的终端中键入“q”。

代码

那么我们是怎么做到的呢?

以下是用于打开视频文件并发送到CodeProject.AI Server的代码的最小版本

vs = FileVideoStream(file_path).start()

with open("results,txt", 'w') as log_file:
    while True:
        if not vs.more():
            break

        frame = vs.read()
        if frame is None:
            break

        image = Image.fromarray(frame)
        image = do_detection(image, log_file)
        frame = np.asarray(image)

        if frame is not None:
            frame = imutils.resize(frame, width = 640)
            cv2.imshow("Movie File", frame)

vs.stop()
cv2.destroyAllWindows()

我们使用FileVideoStream打开视频文件,然后遍历流对象,直到帧数用完。每个帧都被发送到一个do_detection方法,该方法执行实际的对象检测。我们还打开了一个名为“results.txt”的日志文件,我们将其传递给do_detection,该文件会将图像中检测到的项目和位置记录到此文件中。

def do_detection(image, log_file):
   
    # Convert to format suitable for a POST to CodeProject.AI Server
    buf = io.BytesIO()
    image.save(buf, format='PNG')
    buf.seek(0)

    # Send to CodeProject.AI Server for object detection. It's 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(server_url + "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 = None
    if response is not None and "predictions" in response:
       predictions = response["predictions"]

    if predictions is not None:
        # Draw each bounding box and label onto the image we based in
        font = ImageFont.truetype("Arial.ttf", font_size)
        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"])

            if y_max < y_min:
                temp = y_max
                y_max = y_min
                y_min = temp

            if x_max < x_min:
                temp = x_max
                x_max = x_min
                x_min = temp

            draw.rectangle([(x_min, y_min), (x_max, y_max)], outline="red", width=line_width)
            draw.text((x_min + padding, y_min - padding - font_size), f"{label} {round(conf*100.0,0)}%", font=font)

            log_file.write(f"{object_info}: ({x_min}, {y_min}), ({x_max}, {y_max})\n")

    # Return our (now labelled) image
    return image

唯一棘手的部分是

  1. 从视频文件中提取帧
  2. 正确编码每个帧,以便将其作为HTTP POST发送到CodeProject.AI Server API
  3. 将检测到的对象的边界框和标签绘制到框架上,并依次显示每个框架

总而言之,CodeProject.AI Server在检测每一帧中的对象方面已经完成了真正的繁重工作

总结

这里介绍的技术可以转移到CodeProject.AI Server中的许多模块:获取一些数据,转换为适合HTTP POST的形式,进行API调用,然后显示结果。只要你有数据来发送一个满足你需求的模块,你就可以开始了。

https://www.codeproject.com/Articles/5380346/Performing-Object-Detection-on-a-video-file-using

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值