尝试将Google的CloudVision images.annotate与Akka-HTTP结合使用,图像以流的形式出现,http分块响应。而不是收集整个图像,然后使用Base64对其进行编码,然后仅发送JSON请求进行批注,我对图像块进行编码,然后发送在请求中串联的编码块。
找不到可用的现有Akka-Streams快速实现,用于使用Base64编码流。幸运的是,Base64设计为可以对原始序列的串联编码部分进行解码。但是CloudVision不接受:
base64 --version
#base64 (GNU coreutils) 8.30
ipfs get QmfUQT8FnKY4ZbymvfEMPptFughPgcVi9ahUbrPzhBeavb --output base64_encoded_chunks
ipfs get QmfQpPo5ag4dRtuB9uJLwyc8Z3W3vQr5qMRud4YB2KZFWp --output original_image
base64 -w 0 original_image > base64_encoded_whole
# now both base64 encoded files can be decoded into original file contents:
cat base64_encoded_chunks| base64 -d | shasum
# 7c446d90e78baf9511fbf6e9cfad80139f7b708e -
cat base64_encoded_whole | base64 -d | shasum
# 7c446d90e78baf9511fbf6e9cfad80139f7b708e -
shasum original_image
# 7c446d90e78baf9511fbf6e9cfad80139f7b708e original_image
echo '{"requests":[{"image":{"content": "' > ./request_prefix
echo '"},"features":[{"type":"LABEL_DETECTION"}]}]}' > ./request_suffix
ANNOTATE_URL=https://vision.googleapis.com/v1/images:annotate
API_KEY=XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
annotate(){ cat request_prefix $1 request_suffix | curl -H "Content-Type: application/json" "$ANNOTATE_URL?key=$API_KEY" --data-binary @- }
#using the sequence encoded 'in one go' with base64 utility
#it's accepted by Google Vision, getting a response with labels
annotate base64_encoded_whole
# "responses": [
# {
# "labelAnnotations": [
# {
# "mid": "/m/03q69",
# "description": "hair",
# "score": 0.9692406,
# "topicality": 0.9692406
# },
# ...
#using concatenated base64-encoded chunks of the original sequence
#despite the fact that it's decoded exactly to original sequence with base64 utility
#Google Vision can't decode it
annotate base64_encoded_chunks
#{
# "error": {
# "code": 400,
# "message": "Invalid value at 'requests[0].image.content' (TYPE_BYTES), Base64 decoding failed for \"\n/9j/4AAQSkZJRgABAQEAYABgAA...
我知道诸如“不要流式传输”或“使Base64实现适应Akka-Streams”之类的解决方法,但问题是:
[Q] 是CloudVision中Base64解码的一些限制/错误,还是我使用Base64的方式错误?