我正在使用暗网来检测自定义数据集上的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)文件,其中包含预测的每个帧号。
我确实找到了这个答案,但是这给出了json文件中的输出,我需要一个.txt或.csv文件。我对C不太熟悉,所以我发现很难将这个答案修改成我需要的格式。
发布于 2021-07-14 12:39:54
我按照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()
希望这能有所帮助!如果您看到了优化此代码的解决方案,当然这也是受欢迎的:)
发布于 2021-03-23 19:34:46
已经有关于如何使用命令行的说明,特别是以.txt格式保存结果的链接:
https://github.com/AlexeyAB/darknet#how-to-use-on-the-command-line
为了节省时间,我将提供一个可能有用的要点:
可能会迟到但可能对其他人有帮助。
https://stackoverflow.com/questions/64011222
复制相似问题