|
| 1 | +"use strict"; |
| 2 | +import axios from "axios"; |
| 3 | +import { APIGatewayProxyEvent } from "aws-lambda"; |
| 4 | +import * as aws from "aws-sdk"; |
| 5 | +import "source-map-support/register"; |
| 6 | + |
| 7 | +class Handler { |
| 8 | + private rekoSvc: aws.Rekognition; |
| 9 | + private translatorSvc: aws.Translate; |
| 10 | + constructor({ rekoSvc, translatorSvc }) { |
| 11 | + this.rekoSvc = rekoSvc; |
| 12 | + this.translatorSvc = translatorSvc; |
| 13 | + } |
| 14 | + |
| 15 | + async detectImageLabels(buffer: Buffer) { |
| 16 | + const result = await this.rekoSvc |
| 17 | + .detectLabels({ |
| 18 | + Image: { |
| 19 | + Bytes: buffer, |
| 20 | + }, |
| 21 | + }) |
| 22 | + .promise(); |
| 23 | + |
| 24 | + const workingItems = result.Labels.filter( |
| 25 | + ({ Confidence }) => Confidence > 80 |
| 26 | + ); |
| 27 | + |
| 28 | + const names = workingItems.map(({ Name }) => Name).join(" | "); |
| 29 | + |
| 30 | + return { names, workingItems }; |
| 31 | + } |
| 32 | + |
| 33 | + async translateText(text: string) { |
| 34 | + const params = { |
| 35 | + SourceLanguageCode: "en", |
| 36 | + TargetLanguageCode: "pt", |
| 37 | + Text: text, |
| 38 | + }; |
| 39 | + const { TranslatedText } = await this.translatorSvc |
| 40 | + .translateText(params) |
| 41 | + .promise(); |
| 42 | + return TranslatedText.split(" | "); |
| 43 | + } |
| 44 | + |
| 45 | + formatFinalText({ translatedNames, workingItems }) { |
| 46 | + const finalText = []; |
| 47 | + for (const index in workingItems) { |
| 48 | + const nameInPortuguese = translatedNames[index]; |
| 49 | + const currentItemConfidence = workingItems[index].Confidence; |
| 50 | + finalText.push( |
| 51 | + `${currentItemConfidence.toFixed( |
| 52 | + 2 |
| 53 | + )}% de chance de conter ${nameInPortuguese}` |
| 54 | + ); |
| 55 | + } |
| 56 | + return finalText; |
| 57 | + } |
| 58 | + |
| 59 | + async getImageBuffer(imageUrl: string) { |
| 60 | + const response = await axios.get(imageUrl, { |
| 61 | + responseType: "arraybuffer", |
| 62 | + }); |
| 63 | + const buffer = Buffer.from(response.data, "base64"); |
| 64 | + return buffer; |
| 65 | + } |
| 66 | + |
| 67 | + async main(event: APIGatewayProxyEvent) { |
| 68 | + try { |
| 69 | + const { imageUrl } = event.queryStringParameters; |
| 70 | + console.log("Fetching image buffer..."); |
| 71 | + const imageBuffer = await this.getImageBuffer(imageUrl); |
| 72 | + console.log("Detecting labels..."); |
| 73 | + const { names, workingItems } = await this.detectImageLabels(imageBuffer); |
| 74 | + |
| 75 | + console.log("Translating to portuguese..."); |
| 76 | + const translatedNames = await this.translateText(names); |
| 77 | + |
| 78 | + console.log("Handling final text..."); |
| 79 | + const finalText = this.formatFinalText({ |
| 80 | + translatedNames, |
| 81 | + workingItems, |
| 82 | + }); |
| 83 | + return { |
| 84 | + statusCode: 200, |
| 85 | + body: JSON.stringify(finalText), |
| 86 | + }; |
| 87 | + } catch (e) { |
| 88 | + console.log("ERROR***", e); |
| 89 | + return { |
| 90 | + statusCode: 500, |
| 91 | + body: "Internal server error!", |
| 92 | + }; |
| 93 | + } |
| 94 | + } |
| 95 | +} |
| 96 | + |
| 97 | +const trantranslate = new aws.Translate(); |
| 98 | +const reko = new aws.Rekognition(); |
| 99 | + |
| 100 | +const handler = new Handler({ |
| 101 | + rekoSvc: reko, |
| 102 | + translatorSvc: trantranslate, |
| 103 | +}); |
| 104 | + |
| 105 | +export const main = handler.main.bind(handler); |
0 commit comments