You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何存储Darknet YOLOv2实时检测输出的预测类别名称?

How to Store YOLOv2 Detection Class Names for Further Development

Got it, let's break down how you can access and store those class names from your YOLOv2 real-time detection setup—this is super straightforward once you know where to look!

1. The Source: .names Configuration File

First off, YOLOv2 defines all detection class names in a .names text file, which is linked to your dataset's .data config (in your case, coco.data).

  • For the COCO dataset you're using with tiny-yolo.cfg, this file lives at cfg/coco.names in your Darknet directory.
  • The file structure is dead simple: each line contains exactly one class name (e.g., line 1 is person, line 2 is bicycle, etc.).

This is the most reliable method, as it pulls class names straight from the source YOLO uses. Here's how to implement it in common languages:

Python Example

If you're building your follow-up features in Python, just read the file into a list for easy access:

# Path to your .names file
names_path = "cfg/coco.names"

# Load class names into a reusable list
with open(names_path, "r") as f:
    class_names = [line.strip() for line in f.readlines()]

# Example usage: get the name for class ID 0
print(class_names[0])  # Output: person

C++ Example (If Modifying Darknet Source)

If you're extending the Darknet C++ code directly, the class names are already loaded when you run the demo. Here's how to reuse them:

  • In detector.c or demo.c, the code uses load_data_cfg("cfg/coco.data") to read the dataset config, which includes the path to the .names file.
  • The load_class_names function loads all names into a char **class_names array. You can save this array to a file or pass it to your custom modules:
    // Assuming you have the loaded class_names array and num_classes variable
    FILE *fp = fopen("saved_classes.txt", "w");
    for (int i = 0; i < num_classes; i++) {
        fprintf(fp, "%s\n", class_names[i]);
    }
    fclose(fp);
    

3. Temporary Workaround: Capture Terminal Output

If you just need a quick dump of detected classes (not recommended for long-term development), redirect the demo's terminal output to a text file:

./darknet detector demo cfg/coco.data cfg/tiny-yolo.cfg tiny-yolo.weights > detected_classes.txt

Note: This file will include extra logging info, so you'll need to parse out only the class names (look for lines starting with Detected or similar).

Final Tip

Stick with the .names file method—it's the single source of truth for YOLO's class labels, so you'll avoid mismatches between your stored names and what the detector actually outputs.

内容的提问来源于stack exchange,提问作者Shamveel Ahammed

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:33:39