【问题标题】:How to save output of darknet YOLOv4 video in a txt file for each frame?如何将暗网 YOLOv4 视频的输出保存在每帧的 txt 文件中?
【发布时间】:2021-01-08 16:00:40
【问题描述】:

我正在使用 darknet 在我的自定义数据集上检测带有 YOLOv4 的对象。对于我使用的视频的这种检测:

./darknet detector demo data/obj.data yolo-obj.cfg yolo-obj_best.weights -ext_output video.mp4 -out-filename video_results.mp4

这为我的视频提供了为每次检测打印的边界框。但是,我想创建一个 .txt(或 .csv)文件,其中每个帧号都有预测。

我确实找到了this answer,但这会在 json 文件中提供输出,我需要一个 .txt 或 .csv 文件。我对 C 不是很熟悉,所以我发现很难将这个答案修改为我需要的格式。

【问题讨论】:

  • 你熟悉python吗?也许您可以输出 json 并对其进行迭代以保存 txt 或 csv 文件。这真的很容易。
  • 我确实采用了这种方法,感谢您的思考!

标签: python c opencv yolo darknet


【解决方案1】:

已经有关于如何使用命令行的说明,尤其是保存为.txt格式的结果,链接:

https://github.com/AlexeyAB/darknet#how-to-use-on-the-command-line

为了节省时间,我将提供可能有帮助的观点:

  • 要处理图像列表 data/train.txt 并将检测结果保存到 result.txt,请使用:
  • darknet.exe 检测器测试 cfg/coco.data cfg/yolov4.cfg yolov4.weights -dont_show -ext_output result.txt

可能会迟到,但可能对其他人有帮助。

【讨论】:

    【解决方案2】:

    我按照 Rafael 的建议编写了一些代码来从 JSON 迁移到 cvs。我会把它放在这里以防有人想使用它。这是针对分析视频的情况,因此每个“图像”都是视频中的一帧。

    import json
    import csv
    
    # with and height of the video
    WIDTH = 1920
    HEIGHT = 1080
    
    
    with open('~/detection_results.json', encoding='latin-1') as json_file:
        data = json.load(json_file)
        
    # open csv file
    csv_file_to_make = open('~/detection_results.csv', 'w', newline='\n')
    
    csv_file = csv.writer(csv_file_to_make)
    
    # write the header 
    # NB x and y values are relative
    csv_file.writerow(['Frame ID',
                       'class',
                       'x_center',
                       'y_center',
                       'bb_width',
                       'bb_heigth',
                       'confidence'])
    
    
    for frame in data:
        frame_id = frame['frame_id']
        instrument = ""
        center_x = ""
        center_y = ""
        bb_width = ""
        bb_height = ""
        confidence = ""
    
        if frame['objects'] == []:
            csv_file.writerow([frame_id,
                                  class,
                                  center_x,
                                  center_y,
                                  bb_width,
                                  bb_height,
                                  confidence
                                   ])
        else:
            for single_detection in frame['objects']:
                instrument = single_detection['name']
                center_x = WIDTH*single_detection['relative_coordinates']['center_x']
                center_y = HEIGHT*single_detection['relative_coordinates']['center_y']
                bb_width = WIDTH*single_detection['relative_coordinates']['width']
                bb_height = HEIGHT*single_detection['relative_coordinates']['height']
                confidence = single_detection['confidence']
            
                csv_file.writerow([frame_id,
                                  class,
                                  center_x,
                                  center_y,
                                  bb_width,
                                  bb_height,
                                  confidence
                                   ])
        
    csv_file_to_make.close()
    

    希望这会有所帮助!如果您看到优化此代码的解决方案,当然也欢迎 :)

    【讨论】:

    • 我也在尝试做类似的事情。我应该在哪个文件中进行这些更改?请问能详细描述一下吗?
    • 这是一个您应该自己创建的新脚本
    • 这也适用于实时流式传输吗?
    • 我不确定它在现实生活中的流式传输是如何工作的,但边界框的坐标应该在某个地方定义,因此也可以将它们保存到 JSON 文件,然后将它们重写为 . txt 或 .csv 文件。
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 1970-01-01
    • 2022-10-18
    • 2020-11-20
    • 2021-06-26
    • 2014-11-16
    • 2023-02-01
    • 1970-01-01
    相关资源
    最近更新 更多