requests.post将文件添加到https://developer.autodesk.com/photo-to-3d/v1/file api创建零长度文件,没有模型
目标:我正在尝试使用 python3.8 将本地图像文件 使用请求库。
问题:连接文件并开始处理后,我发现Forge报告了错误,并且没有产生模型文件。
代码:我相信问题代码在我要附加的每个图像文件中执行的摘要中:
data = {'filename': file_name,
'photosceneid': photoscene_id,
'type': 'image'}
files = {'files': open(file_name,'rb')}
r = requests.post(url, data=data, headers=headers, files=files)
content = eval(r.content)
诊断:,尽管Forge正在报告“无错误” 作为返回的“ msg”字段对于每个附件调用,Forge还报告了文件大小= 0 字节。这是错误的,因为每个文件的实际大小都在附加的1-2 MB之间。
我猜想它实际上并未读取文件=参数中定义的文件内容,这就是为什么它可能报告零长度的原因。
我对 data =,files = and headers = grain = graborments 之间的何处以及如何指定本地文件感到困惑。 示例
有人可以在Python中向我展示正确的代码
list_of_JPGs = {
'LRC21738.jpg', 'LRC21728.jpg', 'LRC21729.jpg',
'LRC21730.jpg', 'LRC21725.jpg', 'LRC21731.jpg',
'LRC21727.jpg', 'LRC21733.jpg', 'LRC21732.jpg',
'LRC21726.jpg', 'LRC21736.jpg', 'LRC21737.jpg',
'LRC21735.jpg', 'LRC21734.jpg'}
access_token = get_access_token() # attach JPGs to Autodesk Forge
photoscene_id = get_new_photoscene()
url = 'https://developer.api.autodesk.com/photo-to-3d/v1/file'
headers = {'Authorization': 'Bearer ' + access_token}
i = 0
print('FILE #\tFILE NAME\tSIZE\tMSG')
for file_name in list_of_JPGS:
data = {'filename': file_name,
'photosceneid': photoscene_id,
'type': 'image'
}
files = {'files': open(file_name,'rb')}
r = requests.post(url, data=data, headers=headers, files=files)
content = eval(r.content)
try:
photoscene_id = content['photosceneid']
files_items = content['Files']
file_tupple = files_items['file']
filename = file_tupple['filename']
filesize = file_tupple['filesize']
msg = file_tupple['msg']
print (f'file[{i}]: {filename}\t{filesize}\t{msg}')
except:
print(f'WARNING: attaching {file_name} to photoscene {photoscene_id} failed.')
try:
request_error = content['Error']
errormsg = request_error['msg']
print(errormsg)
errorcode = request_error['code']
print(errorcode)
except:
print(f"ERROR 8: Request to attach {file_name} to photoscene {photoscene_id} failed without a message.")
sys.exit(8)
i = i + 1
print(f"{i} images were attached to {photoscene_id}.\n Ready to Begin Modeling")
?
FILE # FILE NAME SIZE MSG
file[0]: LRC21738.jpg 0 No error
file[1]: LRC21728.jpg 0 No error
file[2]: LRC21729.jpg 0 No error
file[3]: LRC21730.jpg 0 No error
file[4]: LRC21725.jpg 0 No error
file[5]: LRC21731.jpg 0 No error
file[6]: LRC21727.jpg 0 No error
file[7]: LRC21733.jpg 0 No error
file[8]: LRC21732.jpg 0 No error
file[9]: LRC21726.jpg 0 No error
file[10]: LRC21736.jpg 0 No error
file[11]: LRC21737.jpg 0 No error
file[12]: LRC21735.jpg 0 No error
file[13]: LRC21734.jpg 0 No error
14 images were attached to O9ukPcj2u47iV43WqOzrvwCLQ5NN8E75NrA64DPU2SA.
Ready to Begin Modeling
附件失败的证据
所有图像文件后,
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}'
附件附加了我用作我的URL开始建模的
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}/progress'
,然后我用来进行调查以进行进度。
140秒后,Forge开始处理,但随后以错误作为其进度消息值而迅速退出:
TIME: PROGRESSMSG PROGRESS
147s: Processing 10%
158s: ERROR 100%
方
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}?format=obj'
您可能期望收到此错误消息后,尝试使用以下 如果没有生产,则检索模型。
GOAL: I am trying to attach local image files to a photoscene in python3.8 using the requests library.
PROBLEM: After attaching the files, and starting processing, I discover that Forge reports an error and that no model file is produced.
CODE: I believe the problem code is within this snippet which I perform for each image file I want to attach:
data = {'filename': file_name,
'photosceneid': photoscene_id,
'type': 'image'}
files = {'files': open(file_name,'rb')}
r = requests.post(url, data=data, headers=headers, files=files)
content = eval(r.content)
Diagnostics: Although Forge is reporting "No Error" as the returned "Msg" field for each of these attachment calls, Forge also reports the FILE SIZE = 0 bytes. This is erroneous as the actual size of each file to attached is between 1-2 MB each.
I am guessing that it is not actually reading the file contents defined in the files= argument, and that is why it is probably reporting zero length.
I am confused about where and how to specify the local files among the data=, files= and headers= arguments passed to requests.post(). Can someone show me a correct code example in python?
Larger code snippet (for context if needed):
list_of_JPGs = {
'LRC21738.jpg', 'LRC21728.jpg', 'LRC21729.jpg',
'LRC21730.jpg', 'LRC21725.jpg', 'LRC21731.jpg',
'LRC21727.jpg', 'LRC21733.jpg', 'LRC21732.jpg',
'LRC21726.jpg', 'LRC21736.jpg', 'LRC21737.jpg',
'LRC21735.jpg', 'LRC21734.jpg'}
access_token = get_access_token() # attach JPGs to Autodesk Forge
photoscene_id = get_new_photoscene()
url = 'https://developer.api.autodesk.com/photo-to-3d/v1/file'
headers = {'Authorization': 'Bearer ' + access_token}
i = 0
print('FILE #\tFILE NAME\tSIZE\tMSG')
for file_name in list_of_JPGS:
data = {'filename': file_name,
'photosceneid': photoscene_id,
'type': 'image'
}
files = {'files': open(file_name,'rb')}
r = requests.post(url, data=data, headers=headers, files=files)
content = eval(r.content)
try:
photoscene_id = content['photosceneid']
files_items = content['Files']
file_tupple = files_items['file']
filename = file_tupple['filename']
filesize = file_tupple['filesize']
msg = file_tupple['msg']
print (f'file[{i}]: {filename}\t{filesize}\t{msg}')
except:
print(f'WARNING: attaching {file_name} to photoscene {photoscene_id} failed.')
try:
request_error = content['Error']
errormsg = request_error['msg']
print(errormsg)
errorcode = request_error['code']
print(errorcode)
except:
print(f"ERROR 8: Request to attach {file_name} to photoscene {photoscene_id} failed without a message.")
sys.exit(8)
i = i + 1
print(f"{i} images were attached to {photoscene_id}.\n Ready to Begin Modeling")
Output from code above:
FILE # FILE NAME SIZE MSG
file[0]: LRC21738.jpg 0 No error
file[1]: LRC21728.jpg 0 No error
file[2]: LRC21729.jpg 0 No error
file[3]: LRC21730.jpg 0 No error
file[4]: LRC21725.jpg 0 No error
file[5]: LRC21731.jpg 0 No error
file[6]: LRC21727.jpg 0 No error
file[7]: LRC21733.jpg 0 No error
file[8]: LRC21732.jpg 0 No error
file[9]: LRC21726.jpg 0 No error
file[10]: LRC21736.jpg 0 No error
file[11]: LRC21737.jpg 0 No error
file[12]: LRC21735.jpg 0 No error
file[13]: LRC21734.jpg 0 No error
14 images were attached to O9ukPcj2u47iV43WqOzrvwCLQ5NN8E75NrA64DPU2SA.
Ready to Begin Modeling
Evidence that attachments failed
After attaching all the image files I used
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}'
as my url to commence modeling, and then I used
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}/progress'
to poll for progress.
After 140 seconds, Forge began processing, but then quickly exited with an ERROR as its progress message value:
TIME: PROGRESSMSG PROGRESS
147s: Processing 10%
158s: ERROR 100%
As you might expect after receiving this error message, attempts to retrieve a model (OBJ file) using:
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}?format=obj'
also fail, as you can't retrieve a model if none was produced.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
额外的阅读和实验证实 data= 和 files= 参数的错误编码是错误的根源。
问题的根源在于文件本身需要被识别为mime type "images/jpg",但其他数据=Form信息,例如 >PhotosceneID,是文本。
解决该冲突需要在data=参数中创建多部分编码的有效负载,并消除使用file= 参数。
以下是我使用 MultipartEncoder 更改的代码来解决该问题,该代码现在似乎对我有用:
对我有用的代码:
注意: 我认为更好的方法仍然需要答案。讨论位于:https://stackoverflow.com/a/52396494/2708519
似乎表明不再需要这种多部分编码方法,并且使用正确的 files= 参数可以避免这种需要。
但是,我还没有找到适合我的 files= 方法。因此,尽管我认为这可能是未来更好的解决方案,但我正在分享对我有用的解决方案。我希望其他人能够分享更新的版本。
Additional reading and experimentation confirmed that incorrect coding of the data= and files= arguments were the source of the error.
The source of the problem is that the files themselves need to be identified as mime type "images/jpg", but the other data= Form information, such as the PhotosceneID, are text.
Resolving that conflict required creating a Multipart-encoded payload in the data= argument and eliminatinguse of the file= argument.
The following is my changed code using MultipartEncoder to work around the problem and this code seems to be working for me now:
Code that worked for me:
Note: I think a better answer is still needed. The discussion at: https://stackoverflow.com/a/52396494/2708519
seems to suggest that this method for multipart encoding is no longer necessary, and that use of the proper files= arguments avoids the need for that.
However,I have not got the files= approach to work for me yet. So even though I think it is probably the better solution for the future, I am sharing the workaround solution that is working for me. I hope others will share a more up to date version.