我正在运行一个对象检测模型(Darknet Yolov4),并将其输出通过管道传输到 results.json
文件。但是,每次我在 "filename"
字段中运行它时,都会使用一个反冲,python 将其视为转义字符。有人知道我如何解决这个问题吗?
我正在运行的命令行代码:
cmd = f"darknet.exe detector test cfg/obj.data cfg/yolov4_test.cfg custom-yolov4-detector_best.weights -dont_show -ext_output -out result.json {path}"
Json 文件:
[
{
"frame_id":1,
"filename":"C:\\Yolo_v4\\darknet\\build\darknet\\x64\\f047.png",
"objects": [
{"class_id":32, "name":"right", "relative_coordinates":{"center_x":0.831927, "center_y":0.202225, "width":0.418463, "height":0.034752}, "confidence":0.976091},
{"class_id":19, "name":"h", "relative_coordinates":{"center_x":0.014761, "center_y":0.873551, "width":0.041723, "height":0.070544}, "confidence":0.484339},
{"class_id":24, "name":"left", "relative_coordinates":{"center_x":0.285694, "center_y":0.200752, "width":0.619584, "height":0.032149}, "confidence":0.646595},
{"class_id":11, "name":"a", "relative_coordinates":{"center_x":0.094595, "center_y":0.488480, "width":0.089853, "height":0.046685}, "confidence":0.978590},
{"class_id":12, "name":"b", "relative_coordinates":{"center_x":0.075972, "center_y":0.534347, "width":0.096545, "height":0.043278}, "confidence":0.930176},
]
}
]
Python 代码(用于识别
def identify(path):
cmd = f"darknet.exe detector test cfg/obj.data cfg/yolov4_test.cfg custom-yolov4-detector_best.weights -dont_show -ext_output -out result.json {path}"
# cmd = f"darknet.exe detector test cfg/obj.data cfg/yolov4-tiny-custom.cfg custom-yolov4-tiny-detector_best.weights -ext_output -out result.json {path}"
os.chdir(r'C:\Yolo_v4\darknet\build\darknet\x64')
p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
out = p.stdout.read()
print(out)
identify(r'C:\\Yolo_v4\\darknet\\build\darknet\\x64\\f047.png')
答案 0 :(得分:0)
反斜杠useEffect
本身在 JSON 中,它被视为转义字符,与其他字符一样。
因此,这些必须以特定方式编写在字符串中使用:
在您的情况下,反斜杠必须替换为 (\)
在您的代码中,在以下行:
\\
在 "C:\\Yolo_v4\\darknet\\build\darknet\\x64\\f047.png"
处,您只使用了一个反斜杠,因此 JSON 将其视为字符转义。
正确的代码应该是:
build\darknet
另请参阅 how other special characthers are escaped in strings(它也是用于进行 JSON 转义测试的在线工具)。