我不熟悉javascript和aws。我从主lambda函数(比如父lambda)的中间行调用了一个单独的lambda函数(比如子lambda),并在父lambda中使用子lambda返回的值。
我的子lambda在python 3.6中实现,父lambda在node.js 12.x中实现。
我将一个3d数组分配给父lambda中的变量“img”。如果条件满足,则通过将有效负载作为3d“img”数组传递来调用子lambda。在子lambda中,我使用数组值创建一个图像,对图像进行预处理(添加一些过滤器并裁剪图像),通过下面的代码将图像转换回3d数组。
imageList = croppedImage.tolist()
然后使用下面的代码交换其中的一些元素(请注意imagelist数组的大小是224x224x3)
for i in range(224):
for j in range(224):
a=imageList[i][j][0]
imageList[i][j][0]=imageList[i][j][2]
imageList[i][j][2]=a
然后使用下面的代码将其返回给父lambda。
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps({
'returnArray': imageList
})
}
最后用返回的数组替换'img'变量。
父lambda中的代码块如下所示。我使用第1行将其转换为json对象,第2行获取返回的数组并将其分配给“img”变量。
const AWS = require('aws-sdk');
AWS.config.region = 'ap-southeast-2';
var lambda = new AWS.Lambda();
exports.handler = async (event, ctx, callback) => {
//////////code lines for other operations////////////
let img = //Line A - 3d array with RGB values of an image;
let body1;
if(condition){
console.log("BEGIN");
var params = {
FunctionName: 'childFunction', // child lambda function written in Pyton 3.6
InvocationType: 'RequestResponse',
Payload: JSON.stringify({ "sendImg" : img})
};
await lambda.invoke(params, function(err, data) {
if (err) {
console.log(err);
} else {
flag = 1;
console.log("Returned : ",data.Payload); //Line B
if(typeof(data.Payload) == 'object') {
body1 = data.Payload;
} else {
body1 = JSON.parse(data.Payload); //Line 1
}
img = JSON.parse(body1.body).returnArray; //Line 2
}
}).promise();
}
////////Rest of the code///////////////////////
};
这是python lambda函数(子lambda)
import json
import cv2
import numpy as np
def lambda_handler(event, context):
array=event['sendImg']
model = event['model']
print("model : ",model)
for i in range(224):
for j in range(224):
a=array[i][j][0]
array[i][j][0]=array[i][j][2]
array[i][j][2]=a
new_image = np.array(array, np.uint8)
image = new_image
#crop
image2 = image.copy()
lab = cv2.cvtColor(image2, cv2.COLOR_BGR2LAB)
l,a,b = cv2.split(lab)
#perform thresholding
m,thresh = cv2.threshold(a,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
image6 = image.copy()
contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cv2.drawContours(image6, contours, -1, (0,255,0), 1)
i=0
maxArea=0
for c in contours:
i+=1
areaC = cv2.contourArea(c)
if maxArea<areaC:
maxArea=areaC
maxIndex=i
print("Outside")
i=0
image7=image.copy()
image8=image.copy()
for c in contours:
i+=1
if i==maxIndex:
print("Inside")
rect = cv2.minAreaRect(c)
(x, y), (width, height), angle = rect
x=round(x)
y=round(y)
width=round(width)
height=round(height)
box = cv2.boxPoints(rect)
box = np.int0(box)
cv2.drawContours(image7,[box],0,(0,255,0),2)
src_pts = box.astype("float32")
dst_pts = np.array([[0, height-1],
[0, 0],
[width-1, 0],
[width-1, height-1]], dtype="float32")
M = cv2.getPerspectiveTransform(src_pts, dst_pts)
warped = cv2.warpPerspective(image8, M, (width, height))
croppedImage = cv2.resize(warped, (224,224), interpolation = cv2.INTER_AREA)
imageList = croppedImage.tolist()
for i in range(224):
for j in range(224):
a=imageList[i][j][0]
imageList[i][j][0]=imageList[i][j][2]
imageList[i][j][2]=a
print("returnArray ",imageList) #Line 3
return {
'statusCode': 200,
'headers': {
'Content-Type': 'application/json'
},
'body': json.dumps({
'returnArray': imageList
})
}
问题:在不同时间使用相同的“img”数组(a行)运行程序时,第1行会给出不同的错误消息。有时它不会给出任何错误消息并成功完成程序。
下面是一些错误消息。
一次发送一条错误消息-
{
"errorType": "SyntaxError",
"errorMessage": "Unexpected token ] in JSON at position 1****",
"code": "SyntaxError",
"message": "Unexpected token ] in JSON at position 1",
"time": "2021-07-19T09:50:14.678Z",
"stack": [
"SyntaxError: Unexpected token ] in JSON at position 1",
" at JSON.parse (<anonymous>)",
" at Response.<anonymous> (/var/task/index.js:81:30)",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:369:18)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
" at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
" at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
" at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)"
]
其他时间的错误消息-
{
"errorType": "SyntaxError",
"errorMessage": "Unexpected token , in JSON at position 3",
"code": "SyntaxError",
"message": "Unexpected token , in JSON at position 3",
"time": "2021-07-19T09:10:41.553Z",
"stack": [
"SyntaxError: Unexpected token , in JSON at position 3",
" at JSON.parse (<anonymous>)",
" at Response.<anonymous> (/var/task/index.js:81:30)",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:369:18)",
" at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
" at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
" at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
" at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
" at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
" at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)"
]
}
您可以看到,在上述两个错误中,标记和位置是不同的。
有时它不会给出任何错误消息并成功完成程序。
我在子lambda中打印输出3d数组,并在父lambda(行b)中返回3d数组(data.payload)。
python lambda(子lambda)中的第3行打印正确的3d数组。
python lambda中第3行的输出-
返回数组[[[113,111,110],[66,60,55],[51,44,38],[53,44,38],[59,48,42],[63,53,47],[68,61,56],[73,62,56],[74,64,58],[75,65,59],[80,70,64],[84,75,67],[86,77,68],。。。
没错。
但是b行给出了不同的结果。
有时data.payload的输出在开始时丢失了阵列的某个部分。
下面是一些示例输出(第b行输出)。
返回[131,69,20],[130,70,28],[125,69,34],[118,59,26],[81,22,0],。。。
返回133]、[186146、110]、[172、125、86]、[123、72、43]、[88、37、14]、[150、100、54]、[180、126、74]、[179、121、64]、,。。。
但它应该是;
返回{“statuscode”:200,“headers”:{“content type”:“application/json”},“body”:“{“returnarray”:[[113,111,110],[66,60,55],[51,44,38],[53,44,38],[59,48,42],[63,53,47],[68,61,56],。。。
为什么子lambda返回的数组在父lambda中有时不同?
谢谢
暂无答案!
目前还没有任何答案,快来回答吧!