跳转至

爬行图像目录

引言

在其他教程中,我们展示了如何启动视频流, 但有时你可能想在 这本教程将展示如何使用 BrainFrame

我们想在所有情况下重复 图像在目录中找到那些有猫的图像. 在我们的 [GitHub存储库]上完成脚本和样本图像 现在,我们可以在一个小时内找到一个信息.

设置环境

在一个 上一个教程现在我们安装了 BrainFrame 我们将使用的API功能 在本教程中,

  • api.get_plugins(...)
  • api.process_image(...)

在本教程中,我们将使用我们公开可用的算法胶囊之一: detector_people_and_vehicles_fast你可以从我们的 下载页面.

在我们开始之前,你应该已经拥有了BrainFrame服务器和客户端 子下载了.

检查现有算法胶囊

像往常一样,让我们先进进口依赖性:

from pathlib import Path

import cv2

from brainframe.api import BrainFrameAPI

连接到服务器:

api = BrainFrameAPI("http://localhost")

在我们开始处理图像之前,我们想检查现有算法胶囊 检查 detector_people_and_vehicles_fast 装满:

# Get the names of existing capsules
loaded_capsules = api.get_plugins()
loaded_capsules_names = [capsule.name for capsule in loaded_capsules]

# Print out the capsules names
print(f"Loaded Capsules: {loaded_capsules_names}")

确保 detector_people_and_vehicles_fast 现在,

Loaded Capsules: ['detector_people_and_vehicles_fast']

您还可以使用客户端检查装载的算法胶囊.

通过图像目录进行反复查看

随着算法胶囊加载,我们可以反复在目录中的所有图像, 现在我们可以在线观看,然后我们可以在线观看, 随着 class_name == "cat".

# Root directory containing the images.
IMAGE_ARCHIVE = Path("../images")

# Iterate through all images in the directory
for image_path in IMAGE_ARCHIVE.iterdir():
    # Use only PNGs and JPGs
    if image_path.suffix not in [".png", ".jpg"]:
        continue

    # Get the image array
    image_array = cv2.imread(str(image_path))

    # Perform inference on the image and get the results
    detections = api.process_image(
        # Image array
        img_bgr=image_array,
        # The names of capsules to enable while processing the image
        plugin_names=["detector_people_and_vehicles_fast"],
        # The capsule options you want to set. You can check the available
        # capsule options with the client. Or in the code snippet above that
        # printed capsule names, also print the capsule metadata.
        option_vals={
            "detector_people_and_vehicles_fast": {
                # This capsule is able to detect people, vehicles, and animals.
                # In this example we want to filter out detections that are not
                # animals.
                "filter_mode": "only_animals",
                "threshold": 0.9,
            }
        }
    )

    print()
    print(f"Processed image {image_path.name} and got {detections}")

    # Filter the cat detections using the class name
    cat_detections = [detection for detection in detections
                      if detection.class_name == "cat"]

    if len(cat_detections) > 0:
        print(f"This image contains {len(cat_detections)} cat(s)")

现在脚本会告诉你这些图片中有猫:

Processed image one-person.jpg and got []

Processed image no_people.jpg and got []

Processed image one-person-png.png and got []

Processed image one_cat.jpg and got [Detection(class_name='cat', coords=[[800, 0], [1566, 0], [1566, 850], [800, 850]], children=[], attributes={}, with_identity=None, extra_data={'detection_confidence': 0.9875224233}, track_id=None)]
This image contains 1 cat(s)

Processed image two_people_and_dtag.png and got []

Processed image two_people.jpg and got []