iT邦幫忙

2

如何使用AWS AI Service Cards – Amazon Rekognition ?

  • 分享至 

  • xImage
  •  

什麼是 Amazon Rekognition?

Amazon Rekognition 提供預先訓練和可自訂的電腦視覺 (CV) 功能,可從影像和影片中擷取資料和分析。

為何要使用 AWS AI Service?

AI影像辨識需要大量運算資源跟訓練資料,我們可以用AWS的AI Service使其在AWS Server做運算,省下不少麻煩。

怎麼使用 Amazon Rekognition?

這次使用的AWS是教育方案的Learner Lab,差別在某些的功能限制以及IAM 角色只能使用"LabRole",接下來會展示Amazon Rekognition的文字辨識功能。

  • 下面是接下來的範例架構圖。
    https://ithelp.ithome.com.tw/upload/images/20240430/20166878vtH16rNGRa.png

  • 功能說明:使用lambda的測試事件將S3的指定圖片傳到Lambda去做Amazon Rekognition的文字辨識,並將結果用Python的 OpenCV 將結果框起來並標籤。

  • 輸入圖片
    https://ithelp.ithome.com.tw/upload/images/20240430/20166878IeluLtMIps.jpg

  • 輸出圖片
    https://ithelp.ithome.com.tw/upload/images/20240430/20166878gQ8ScpHi2e.jpg

操作圖

  1. 確認lambda的測試事件裡的圖片是否有存在於指定的S3貯存桶。
  • 點擊 "測試"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878GsOPNZLxqZ.png

https://ithelp.ithome.com.tw/upload/images/20240501/20166878jPCj5b78Ld.png

  • 查看S3貯存桶
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878l5ALBsebf5.png
  1. 執行lambda
  • 點擊 "程式碼"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878OzIUxzetME.png

  • 點擊 "Test"
    https://ithelp.ithome.com.tw/upload/images/20240501/201668784pRbeXs8aR.png
    3.查看結果

  • 回到S3查看是否回傳結果圖 "ress.jpg"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878DzJ6OuNSmQ.png

操作流程

  1. 建立lambda
  • 點擊 "建立函式"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878lWD7l9t7qI.png

https://ithelp.ithome.com.tw/upload/images/20240501/20166878su8nLrht5v.png
2. 新增層

  • 關於step2 可以參考OpenCV Layer建立非常清楚 。
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878KXr1OM6W5A.png

  • 建立完畢後,到lambda 程式碼最下面,點擊 "新增層"。
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878XXaRmuTst8.png
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878Oap77lzwVV.png

  1. 編輯程式碼
  • 回到上面lambda_function.py 將裡面的程式碼全部改掉,換成以下程式碼。
import json
import boto3
from datetime import datetime
import cv2

def 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))
    
    #建立 Amazon Rekognition OCR
    client = boto3.client('rekognition')
    response = client.detect_text(Image={'S3Object':{'Bucket':bucket_name,'Name':file_name}})
    print(json.dumps(response))
    
    # 將辨識結果用OpenCV的函式框起來並標記號。
    image_path = '/tmp/input_image.jpg'
    s3.download_file(bucket_name, file_name, image_path)
    output_image_path = '/tmp/output_image.jpg'
    draw_boxes_and_put_text_with_order(image_path, response['TextDetections'], output_image_path)
    
    # #將結果圖放回該原圖的S3位置,並命名為 "ress.jpg" (可以依照你輸入的圖片的副檔名去更改)。
    upload_image_to_s3(output_image_path, bucket_name, 'ress.jpg')

    return {
        'statusCode': 200,
        'body': json.dumps('Hello from Lambda!')
    }

def draw_boxes_and_put_text_with_order(image_path, text_detections, output_image_path):
    # Load the image
    image = cv2.imread(image_path)
    
    # Sort text detections by their top coordinate
    sorted_detections = sorted(text_detections, key=lambda x: x['Geometry']['BoundingBox']['Top'])

    # Draw bounding boxes and put text with order on the image
    for i, detection in enumerate(sorted_detections):
        box = detection['Geometry']['BoundingBox']
        width, height = image.shape[1], image.shape[0]
        left = int(width * box['Left'])
        top = int(height * box['Top'])
        right = int(left + (width * box['Width']))
        bottom = int(top + (height * box['Height']))

        # Draw the bounding box
        cv2.rectangle(image, (left, top), (right, bottom), (0, 255, 0), 2)

        # Put text with order on the image
        text = '{}: {}'.format(i+1, detection['DetectedText'])
        cv2.putText(image, str(i+1), (left, top-10), cv2.FONT_HERSHEY_SIMPLEX, 1.5, (0, 255, 0), 2)

    # Save the image with bounding boxes and text
    cv2.imwrite(output_image_path, image)

def upload_image_to_s3(image_path, bucket_name, key_name):
    s3 = boto3.client('s3')
    with open(image_path, 'rb') as file:
        s3.upload_fileobj(
            file,
            bucket_name,
            key_name,
            ExtraArgs={'ACL': 'public-read'}
        )
  • 編輯完成點擊 "Deploy"。
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878fuvNTxAeIu.png
  1. 上傳圖片到S3
    點擊 "上傳"。
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878hdlgkwSHiw.png
    點擊 "新增檔案"。
    https://ithelp.ithome.com.tw/upload/images/20240501/201668789eO3TRal74.png
  • 可以依你上傳的圖片的附檔名去更改 lambda_function.py 的輸出圖片的副檔名。
  1. 新增lambda 測試事件
  • 會到lambda 點擊 "測試"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878S1rM6fy3ng.png
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878DYrWiv3Zdt.png

  • step2 的事件JSON請用以下程式碼(需要替換成你的S3貯存桶名稱跟圖片名稱)。

{
  "Records": [
    {
      "eventVersion": "2.1",
      "eventSource": "aws:s3",
      "awsRegion": "us-east-1",
      "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": "你的S3貯存桶名稱", 
          "ownerIdentity": {
            "principalId": "EXAMPLE"
          },
          "arn": "arn:aws:s3:::你的S3貯存桶名稱"
        },
        "object": {
          "key": "你的圖片名稱(包含副檔名)",
          "size": 1024,
          "eTag": "0123456789abcdef0123456789abcdef",
          "versionId": "096fKKXTRTtl3on89fVO.nfljtsv6qko",
          "sequencer": "0A1B2C3D4E5F678901"
        }
      }
    }
  ]
}
  1. 更改lamda執行時間
  • 點擊 "組態"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878VUoEykoSyO.png
  • 點擊 "一般組態" 並 "編輯"。

https://ithelp.ithome.com.tw/upload/images/20240501/20166878GNtu6KufNH.png

  • 更改逾時時間

https://ithelp.ithome.com.tw/upload/images/20240501/20166878JvKttytOIQ.png
7. 執行程式

  • 回到程式碼並點擊 "Test"
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878gpDCUYfBjU.png

  • 查看step2的訊息是否跟下面的訊息一樣
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878mYAweuoDjJ.png

  • 回到 S3 查看是否有 "ress.jpg" 的資料,有就代表成功了。
    https://ithelp.ithome.com.tw/upload/images/20240501/20166878nZb8gQOH7C.png
    參考資料

  1. OpenCV Layer建立
  2. 什麼是Amazon Rekognition
  3. OpenCV 範例函式
  4. Lambda 事件JSON範例

圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

2 則留言

1
Jolia5314
iT邦新手 5 級 ‧ 2024-05-03 00:15:22

非常詳盡! /images/emoticon/emoticon51.gif

謝謝 這是我的最後一個文章了/images/emoticon/emoticon57.gif

1
kkeea1020
iT邦新手 5 級 ‧ 2024-05-03 00:19:57

簡明扼要,非常詳盡/images/emoticon/emoticon41.gif

謝謝,我會繼續進步,我會持續提升自身能力,發布更有用的文章,雖然這是我的最後一篇/images/emoticon/emoticon08.gif

我要留言

立即登入留言