利用AWS内的S3与Lambda来辨识文字(也可以辨识车牌),辨识到的文字会框起来

注意,只能后端完成!!!!!

文字辨识与车牌辨识架构图

http://img2.58codes.com/2024/20166876D7mXKQxMG6.png

功能说明

上传图片至S3之后经由Lambda辨识图片内的文字,并框出所辨识到的文字(使用opencv)再回传至S3

操作与流程说明

步骤1: 在搜寻框里搜寻S3,之后打开S3,如下图所示
http://img2.58codes.com/2024/20166876w8DRA38FEM.png

步骤2: 进入S3,点选建立伫体,如下图所示
http://img2.58codes.com/2024/20166876zWlmWGFEYP.png

步骤3: 先命名S3储存桶的名称,如下图所示
http://img2.58codes.com/2024/20166876Hwf6gEXI2g.png

步骤4: 将点选"ACL已启用",如下图所示
http://img2.58codes.com/2024/20166876v6AKZTYuCP.png

步骤5: 将"封锁公开存取权"改为不勾选的状态,记得要勾选"我确认目前的设定可能导致此储存贮体和其内的物件变成公开状态。",如下图所示
http://img2.58codes.com/2024/20166876rDpTlfjMVR.png

步骤6: 第3~5的步骤做完后,即可建立储存桶,如下图所示
http://img2.58codes.com/2024/20166876mRhHqh6jFt.png

步骤7: 回到刚进S3时候的页面,点选建立好的储存桶,如下图所示
http://img2.58codes.com/2024/20166876vfJ8LcetXl.png

步骤8: 点选"上传" ,如下图所示
http://img2.58codes.com/2024/20166876Nszfe4jRTC.png

步骤9: 点选"新增档案",之后就可以选取您想要辨识的图片,如下图所示
http://img2.58codes.com/2024/20166876qTtOBXbVqY.png

步骤10: 按"上传",如下图所示
http://img2.58codes.com/2024/20166876UJwLenFR3u.png

步骤11: 回到刚进S3时候的页面,确认是否上传成功,如下图所示
http://img2.58codes.com/2024/20166876Mz111tDZFw.png

步骤12: 需要上传opencv至S3上,后面会利用到"物件URL"的连结(上传方式和第8~10的步骤相同,只是上传的是opencv档案,并不是图档而已),如下图所示
http://img2.58codes.com/2024/20166876kVMVOi2Xbm.png

步骤13: 点选您上传的opencv,框起来的地方就是"物件URL"的连结,複製起来,等等会用到,如下图所示
http://img2.58codes.com/2024/20166876RhzdlUxtxR.png

步骤14: 在搜寻框上搜寻Lambda,之后打开Lambda,如下图所示
http://img2.58codes.com/2024/20166876i7PuWPamFZ.png

步骤15: 点选"层",如下图所示
http://img2.58codes.com/2024/20166876W2IwGJnsrq.png

-步骤16: 点选"建立Layer",如下图所示
http://img2.58codes.com/2024/20166876lj0RgAHfp1.png

-步骤17: 特别要注意的是执行时间需与opencv所下载的python版本一致,其他都照着做即可,如下图所示
http://img2.58codes.com/2024/20166876MelhKgGcyW.png

步骤18: 回到刚进lambda时候的页面,点选"建立函式",如下图所示
http://img2.58codes.com/2024/20166876C4QCGPBRsZ.png

步骤19: 照着下图做,特别需要注意的是要点选现有角色,现有角色为"LabRole",如下图所示
http://img2.58codes.com/2024/20166876sj7Xg8KQHP.png

步骤20: 建立完后,将滑鼠滑到至最下面的页面,就可找到"新增层",如下图所示
http://img2.58codes.com/2024/20166876CnoaSG5iar.png

步骤21: 照着下图做,要注意的点是,我在步骤17是命名为opencv,所以要选您命名的,除非跟我命名的是一模一样的,如下图所示
http://img2.58codes.com/2024/20166876n3PgmyDSoG.png

步骤22: 回到刚进lambda时候的页面,複製程式码

import jsonimport base64import boto3from datetime import datetimeimport osimport cv2import numpy as npdef lambda_handler(event, context):    s3 = boto3.client('s3')    # Get the bucket name and the uploaded file name    bucket_name = event['Records'][0]['s3']['bucket']['name']    file_name = event['Records'][0]['s3']['object']['key']    print('Bucket name: {}'.format(bucket_name))    print('Upload file name: {}'.format(file_name))    # Get current unix time    created = int(datetime.now().timestamp())        # OCR    client = boto3.client('rekognition')    response = client.detect_text(Image={'S3Object':{'Bucket':bucket_name,'Name':file_name}})    print(json.dumps(response))        # Text detection    img_path = '/tmp/input_img.jpg'  # Path to temporarily store the input image    output_img_path = '/tmp/output_img.jpg'  # Path to store the output image    s3.download_file(bucket_name, file_name, img_path)  # Download image from S3    img = cv2.imread(img_path)    detectedText = response['TextDetections']        # Iterate through detectedText to get the required name/value pairs    for text in detectedText:        # Get bounding box coordinates        box = text['Geometry']['BoundingBox']        h, w, _ = img.shape        x1, y1, x2, y2 = int(box['Left'] * w), int(box['Top'] * h), int((box['Left'] + box['Width']) * w), int((box['Top'] + box['Height']) * h)        # Draw rectangle around detected text        cv2.rectangle(img, (x1, y1), (x2, y2), (0, 255, 0), 2)        # Put text label        cv2.putText(img, text['DetectedText'], (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)        # Save annotated image    cv2.imwrite(output_img_path, img)        # Upload annotated image to S3    s3.upload_file(output_img_path, bucket_name, 'annotated_' + file_name)    return {        'statusCode': 200,        'body': json.dumps('Hello from Lambda!')    }    

把複製的程式码贴到框框内,之后按"Deploy",如下图所示
http://img2.58codes.com/2024/20166876yHcrHpS4j3.png

步骤23: 按Test旁边的箭号,之后点框起来的地方,如下图所示
http://img2.58codes.com/2024/201668764g7fcZRwmd.png

步骤24: 下面的程式码为测试事件所用的程式码,程式码为JSON所用的,需要注意的点是程式码内的"0429te"是我步骤1~6所建立的储存桶名称,所以必须要换成您所用命名的名称,除非步骤都和我的一模一样,还有"test2.png"的档案,需要换成您上传至S3的图档,档名与副档名都要相同才行!!!

{  "Records": [    {      "eventVersion": "2.1",      "eventSource": "aws:s3",      "awsRegion": "us-west-2",      "eventTime": "2024-04-29T00:00:00.000Z",      "eventName": "ObjectCreated:Put",      "userIdentity": {        "principalId": "EXAMPLE"      },      "requestParameters": {        "sourceIPAddress": "127.0.0.1"      },      "responseElements": {        "x-amz-request-id": "EXAMPLE123456789",        "x-amz-id-2": "EXAMPLE123/5678abcdefghijklambdaisawesome/mnopqrstuvwxyzABCDEFGH"      },      "s3": {        "s3SchemaVersion": "1.0",        "configurationId": "testConfigRule",        "bucket": {          "name": "0429te",          "ownerIdentity": {            "principalId": "EXAMPLE"          },          "arn": "arn:aws:s3:::0429te"        },        "object": {          "key": "test2.png",          "size": 1024,          "eTag": "0123456789abcdef0123456789abcdef",          "sequencer": "0A1B2C3D4E5F678901"        }      }    }  ]}

如下图所示
http://img2.58codes.com/2024/20166876kaGjT9QpdE.png

步骤25: 点击"组态",如下图所示
http://img2.58codes.com/2024/20166876Gmrn6yySBz.png

步骤26: 点选左侧的一般组态,再点击"编辑",如下图所示
http://img2.58codes.com/2024/20166876yPpY16Pomi.png

步骤27: 需要把时间设定成1分钟以上,如下图所示
http://img2.58codes.com/2024/20166876qyfmMxtrmp.png

步骤28: 点击"Test",辨识后的文字就会回传至S3了,如下图所示
http://img2.58codes.com/2024/20166876iq8LTuETT5.png

步骤29: 回到刚进S3时候的页面,点击回传后的新图档,如下图所示
http://img2.58codes.com/2024/20166876Gm8lsaCrrw.png

步骤30: 点击"许可",如下图所示
http://img2.58codes.com/2024/20166876FtANShWpeR.png

步骤31: 点击"编辑",如下图所示
http://img2.58codes.com/2024/20166876qttbk5i7wZ.png

步骤32: 存取控制清单(ACL)里有框起来的範围都要勾,还有"我了解这些变更对此物件的影响。"也要勾,如下图所示
http://img2.58codes.com/2024/20166876EveqDWmkhN.png

步骤33: 回到"属性"介面,点击框起来的连结即可下载辨识完的图档
http://img2.58codes.com/2024/20166876eSXjgY6jiE.png

参考资料

框出文字内容(opencv)

测试程式码由chatgpt帮我写的


关于作者: 网站小编

码农网专注IT技术教程资源分享平台,学习资源下载网站,58码农网包含计算机技术、网站程序源码下载、编程技术论坛、互联网资源下载等产品服务,提供原创、优质、完整内容的专业码农交流分享平台。

热门文章