简体   繁体   English

Javascript,Tesseract,AWS IoT,Dynamodb和缓冲区

[英]Javascript, Tesseract, AWS IoT, Dynamodb & Buffers

Im working and testing on a small Tesseract project and with full credit to http://aalvarez.me/blog/posts/building-an-ocr-service-with-tesseractjs-in-aws-lambda.html I've got a Tesseract project running in Lambda that takes s3 events and processes the image that was uploaded to s3. 我正在一个小型的Tesseract项目上进行工作和测试,并完全归功于http://aalvarez.me/blog/posts/building-an-ocr-service-with-tesseractjs-in-aws-lambda.html。在Lambda中运行的Tesseract项目接收s3事件并处理上载到s3的图像。

The next objective is to look at processing an image from AWS IoT. 下一个目标是研究处理来自AWS IoT的图像。 The main hurdle is that you cant send an image via MQTT. 主要障碍是您无法通过MQTT发送图像。

My approach would be the send the buffer/binary stream of the image on a MQTT topic and then an IoT Rule triggers the same lambda for processing. 我的方法是在MQTT主题上发送图像的缓冲区/二进制流,然后IoT规则触发相同的lambda进行处理。

But 1st I needed to have a valid binary stream (no real hardware and cameras yet FYI and I'm the backend guy) to send via IoT so I modified my project to not only store the results of the OCR performed on the image but also the binary stream/buffer of the image. 但是第一,我需要有一个有效的二进制流(没有真正的硬件和摄像头,但FYI,我是后端人员)才能通过IoT发送,所以我修改了项目,不仅要存储在图像上执行的OCR的结果,而且还要存储图像的二进制流/缓冲区。

I needed to do this because AWS Cloudwatch truncates the logging so when I logged out the Data payload of the s3 object that the Lambda function loads, all you get is a severely truncated buffer object. 我需要这样做是因为AWS Cloudwatch会截断日志记录,因此当我注销Lambda函数加载的s3对象的Data有效负载时,您得到的只是严重截断的缓冲区对象。

dataBody: <Buffer 89 50 4e 47 0d 0a 1a 0a 00 00 00 0d 49 48 44 52 00 00 01 90 00 00 01 90 08 06 00 00 00 80 bf 36 cc 00 00 20 00 49 44 41 54 78 5e ed 9d 09 f8 76 df 58 ... >

So I added a bit of code that uses DynamoDB updateItem with SET as per below to set an attribute on a db item with the original buffer used to perform the OCR with as per below 因此,我添加了一些代码,如下所示使用DynamoDB updateItem和SET来设置db项的属性,并使用原始缓冲区按以下方式使用OCR来执行OCR

            "UpdateExpression": "SET img = :attrValue",
        "ExpressionAttributeValues": {
            ":attrValue": { "B": dataBody }

That worked and the attribute was created in the item with a large string of characters which I took to be the original but in some different format perhaps 可行,属性在项目中创建,带有大字符串,我将其视为原始字符串,但格式可能不同

Now if I take that large string and send it as MQTT payload and trigger the rule then the Lambda fires but when I convert the payload and attempt to process it then Tesseract complains about 现在,如果我采用该大字符串并将其作为MQTT有效负载发送并触发规则,则Lambda将触发,但是当我转换有效负载并尝试对其进行处理时,Tesseract会抱怨

TypeError: Cannot read property 'mime' of null TypeError:无法读取null的属性“ mime”

The error is probably the result of converting the IoT payload to a Buffer for processing as its now completely different from the original Buffer when reading in the original s3 object and therefore mime property is not present. 该错误可能是由于将IoT负载转换为缓冲区进行处理的结果,因为当读取原始s3对象时,它现在与原始缓冲区完全不同,因此不存在mime属性。 The data is now munted I'm picking 现在我正在挑选数据

let ocrBuffer = Buffer.from(iotEvent)

ocrBuffer: <Buffer 69 56 42 4f 52 77 30 4b 47 67 6f 41 41 41 41 4e 53 55 68 45 55 67 41 41 41 5a 41 41 41 41 47 51 43 41 59 41 41 41 43 41 76 7a 62 4d 41 41 41 67 41 45 ... >

Bit of a newbie to buffers and working with images etc but I'm suspecting that the process of storing it to DynamoDB has somehow altered the original buffer or wrapped it in something so I need to unalter/unwrap it correctly so it becomes the same as the original Buffer that starts with 89 instead of 69. 缓冲区和处理图像等方面的新手,但是我怀疑将其存储到DynamoDB的过程以某种方式更改了原始缓冲区或将其包装在某些东西中,因此我需要正确地更改/解开它,使其与以89而不是69开头的原始缓冲区。

Does anyone have any ideas on where its all gone wrong and where should start looking. 是否有人对哪里出了问题以及应该从哪里开始寻找任何想法。

Right now I can upload images to s3 and I want to somehow save the payload so I can use it as a test payload for use via IoT Service 现在我可以将图像上传到s3,我想以某种方式保存有效负载,以便可以将其用作测试有效负载以通过IoT Service使用

Thanks Mark 谢谢马克

The crux of your problem seems to be this message from Tesseract: 您的问题的症结似乎是来自Tesseract的以下消息:

TypeError: Cannot read property 'mime' of null TypeError:无法读取null的属性“ mime”

This at least has a clear cause. 这至少有明确的原因。 When you are sending the data to Tesseract it sounds like you are not setting the Content-type http header. 将数据发送到Tesseract时,听起来好像没有设置Content-type http标头。

In the examples from Tesseract it looks like the Content-type header is required, which would make sense as it needs to know what kind of file you are posting to it. 在Tesseract的示例中,看起来好像需要Content-type标头,这很有意义,因为它需要知道要向其发布哪种文件。

So I think the question here how is your Lambda function posting data to Tesseract, and specifically how is it setting the Content-type and Content-Disposition headers? 因此,我认为这里的问题是您的Lambda函数如何将数据发布到Tesseract,特别是如何设置Content-type和Content-Disposition标头?

Although there could be issues with your binary data I probably wouldn't focus on that right now, as it doesn't appear to be the thing Tesseract is complaining about. 尽管您的二进制数据可能存在问题,但我现在可能暂时不关注它,因为这似乎不是Tesseract所抱怨的事情。

One thing you could try is posting the (local) file to Tesseract very simply using curl. 您可以尝试做的一件事就是非常简单地使用curl将(本地)文件发布到Tesseract。 Assuming this works, compare the HTTP request with the one your Lambda function is producing and work backwards from there. 假设此方法有效,请将HTTP请求与您的Lambda函数产生的请求进行比较,然后从那里反向进行工作。 I suspect you don't have a content issue, or at least that probably not the current problem. 我怀疑您没有内容问题,或者至少不是当前问题。

Resolved 解决

All I needed to do was specify 'base64' when converting to buffer using 我需要做的就是在使用转换为缓冲区时指定'base64'

Buffer.from(string, 'base64')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM