问题描述
$(document).ready(function(){
$.ajax({
url: 'test2.PHP',type: 'POST',dataType: 'JSON',success: function(data){
var result = JSON.stringify(data);
result = JSON.parse(result);
console.log("RESULT",result);
$("#time").html(result[0]); //POPULATION LINE
$("#operator").val(result[1]);
$("#destination").val(result[2]);
$("#platform").val(result[3]);
$("#estimated").val(result[4]);
}
});
});
这两个都很好:
file_path = '/dir/sub_dir/my_image.jpg'
我实例化了一个 pathlib.Path 对象
open(file_path,'r')
tensorflow.io.read_file(file_path)
这两个都可以正常工作:
p = pathlib.Path(file_path)
但这不起作用:
open(p,'r')
tensorflow.io.read_file(str(p))
它返回:
tensorflow.io.read_file(p)
为什么 ValueError: Attempt to convert a value (PosixPath('/dir/sub_dir/my_image.jpg')) with an unsupported type (<class 'pathlib.PosixPath'>) to a Tensor.
无法读取 tensorflow.io.read_file
对象
我明白为什么 pathlib.Path
工作正常。我不明白的是 tensorflow.io.read_file(str(p))
是如何与 path_like_object 一起工作的,但 open
不能
https://docs.python.org/3/glossary.html#term-path-like-object
https://docs.python.org/3/library/functions.html#open
解决方法
那是因为 tensorflow.io.read_file 需要一个 String 对象作为文件路径,而不是 PosixPath 对象,这是你从 pathlib 传递的。
当您使用 str(path) 时,您将 PosixPath 转换为字符串,这就是为什么在这种情况下它可以正常工作。
您可以找到有关文档的更多详细信息。
TensorFlow:https://www.tensorflow.org/api_docs/python/tf/io/read_file