Android Amazon Rekognition 文本检测不适用于字节,但适用于 S3 对象
Android Amazon Rekognition Text Detection doesn't work with bytes but does with S3 Objects
我正在尝试检测用相机拍摄的照片中的文字,但没有成功。
我使用的代码是:
AWSCredentials credentials = new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return "some access key id";
}
@Override
public String getAWSSecretKey() {
return "some secret key";
}
};
File file = new File(photoFilePath);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
Timber.e(e);
} catch (IOException e) {
Timber.e(e);
}
AmazonRekognition rekognitionClient = new AmazonRekognitionClient(credentials);
byte [] base64 = android.util.Base64.encode(bytes, Base64.DEFAULT);
Image image = new Image().withBytes(ByteBuffer.wrap(base64));
DetectTextRequest detectTextRequest = new DetectTextRequest().withImage(image);
Observable.create((Observable.OnSubscribe<String>) observer -> {
try {
DetectTextResult result = rekognitionClient.detectText(detectTextRequest);
List<TextDetection> labels = result.getTextDetections();
String alllabels = "";
for (TextDetection detection : labels) {
alllabels += detection.getDetectedText();
}
observer.onNext(alllabels);
observer.onCompleted();
} catch (AmazonServiceException e) {
Timber.e(e);
observer.onError(e);
} catch (AmazonClientException e) {
Timber.e(e);
observer.onError(e);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override
public void onNext(String item) {
System.out.println("Next: " + item);
}
@Override
public void onError(Throwable error) {
System.err.println("Error: " + error.getMessage());
}
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
});
}
这会产生异常消息
Failed to upload image; the format is not supported
当不以 base64 编码字节时 - 它会产生奇怪的输出,其中检测到的每个文本都用逗号分隔,例如
S, !!:, 8, anons SAr, !!:, S, 8, anons, SAr,
或
8B, 8B
我的示例可能有什么问题?
即使对同一张照片使用对 S3 对象的引用 - 一切正常。
由于不支持的格式,服务器似乎拒绝了您的请求。请注意,AWS Rekognition 仅支持 PNG 和 JPEG 图像格式,并且对像素分辨率等有特定要求[有关图像要求的更多信息,请参阅 https://docs.aws.amazon.com/rekognition/latest/dg/limits.html]
我过去使用以下代码在 jpg 图像上取得了成功
InputStream fin1 = getResources().openRawResource(getResources().getIdentifier("face1", "raw", getPackageName()));
ByteBuffer byteBuffer1 = ByteBuffer.wrap(IOUtils.toByteArray(fin1));
Image image1 = new Image();
image1.withBytes(byteBuffer1);
请尝试一下,如果能解决您的问题,请告诉我。
图片旋转了,这就是它返回奇怪数据的原因...
我正在尝试检测用相机拍摄的照片中的文字,但没有成功。
我使用的代码是:
AWSCredentials credentials = new AWSCredentials() {
@Override
public String getAWSAccessKeyId() {
return "some access key id";
}
@Override
public String getAWSSecretKey() {
return "some secret key";
}
};
File file = new File(photoFilePath);
int size = (int) file.length();
byte[] bytes = new byte[size];
try {
BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
buf.read(bytes, 0, bytes.length);
buf.close();
} catch (FileNotFoundException e) {
Timber.e(e);
} catch (IOException e) {
Timber.e(e);
}
AmazonRekognition rekognitionClient = new AmazonRekognitionClient(credentials);
byte [] base64 = android.util.Base64.encode(bytes, Base64.DEFAULT);
Image image = new Image().withBytes(ByteBuffer.wrap(base64));
DetectTextRequest detectTextRequest = new DetectTextRequest().withImage(image);
Observable.create((Observable.OnSubscribe<String>) observer -> {
try {
DetectTextResult result = rekognitionClient.detectText(detectTextRequest);
List<TextDetection> labels = result.getTextDetections();
String alllabels = "";
for (TextDetection detection : labels) {
alllabels += detection.getDetectedText();
}
observer.onNext(alllabels);
observer.onCompleted();
} catch (AmazonServiceException e) {
Timber.e(e);
observer.onError(e);
} catch (AmazonClientException e) {
Timber.e(e);
observer.onError(e);
}
})
.observeOn(AndroidSchedulers.mainThread())
.subscribeOn(Schedulers.io())
.subscribe(new Subscriber<String>() {
@Override
public void onNext(String item) {
System.out.println("Next: " + item);
}
@Override
public void onError(Throwable error) {
System.err.println("Error: " + error.getMessage());
}
@Override
public void onCompleted() {
System.out.println("Sequence complete.");
}
});
}
这会产生异常消息
Failed to upload image; the format is not supported
当不以 base64 编码字节时 - 它会产生奇怪的输出,其中检测到的每个文本都用逗号分隔,例如
S, !!:, 8, anons SAr, !!:, S, 8, anons, SAr,
或
8B, 8B
我的示例可能有什么问题?
即使对同一张照片使用对 S3 对象的引用 - 一切正常。
由于不支持的格式,服务器似乎拒绝了您的请求。请注意,AWS Rekognition 仅支持 PNG 和 JPEG 图像格式,并且对像素分辨率等有特定要求[有关图像要求的更多信息,请参阅 https://docs.aws.amazon.com/rekognition/latest/dg/limits.html]
我过去使用以下代码在 jpg 图像上取得了成功
InputStream fin1 = getResources().openRawResource(getResources().getIdentifier("face1", "raw", getPackageName()));
ByteBuffer byteBuffer1 = ByteBuffer.wrap(IOUtils.toByteArray(fin1));
Image image1 = new Image();
image1.withBytes(byteBuffer1);
请尝试一下,如果能解决您的问题,请告诉我。
图片旋转了,这就是它返回奇怪数据的原因...