目标:--我正在尝试使用请求库将本地图像文件附加到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) Diagnostics:虽然Forge将“无错误”报告为每个附件调用返回的"Msg“字段,但Forge也报告文件大小= 0字节。这是错误的,因为每个文件的实际大小都在1-2MB之间。
我猜想它实际上并没有读取files=参数中定义的文件内容,这就是为什么它可能报告零长度的原因。
我对如何在传递给requests.post().的data=、files=和headers=参数中指定本地文件感到困惑。有人能在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%正如您可能预期的,在收到此错误消息后,尝试使用以下方法检索模型(OBJ文件):
f'https://developer.api.autodesk.com/photo-to-3d/v1/photoscene/{photoscene_id}?format=obj'同样失败,因为如果没有生成模型,则无法检索模型。
发布于 2022-04-07 01:25:36
额外的阅读和实验证实,data=和files=参数的不正确编码是错误的根源。
问题的根源在于文件本身需要识别为mime类型的“图像/jpg”,但其他data= Form信息(如PhotosceneID )是text。
解决这一冲突需要在中创建多部分编码的有效负载,并消除file=参数的使用。
下面是使用MultipartEncoder来解决问题的修改代码,这段代码现在似乎适合我了:
为我工作的代码:
url = 'https://developer.api.autodesk.com/photo-to-3d/v1/file'
payload = MultipartEncoder(
fields={'photosceneid': photoscene_id,
'type': 'image',
'file[0]': (file_name, open(file_name, 'rb'), 'image/jpg')
}
)
headers = {'Content-Type': payload.content_type,
'Authorization': 'Bearer ' + access_token}
r = requests.post(url, headers=headers, data=payload)
content = eval(r.content)注:,我认为还需要一个更好的答案。“:https://stackoverflow.com/a/52396494/2708519”中的讨论似乎表明,这种用于多部分编码的方法不再必要,使用适当的files=参数可以避免这种需要。
然而,我还没有让files=方法为我工作。因此,尽管我认为这可能是更好的未来解决方案,我分享的解决方案,正在为我工作。我希望其他人能分享一个更最新的版本。
https://stackoverflow.com/questions/71774774
复制相似问题