从子python lambda函数返回的数组在父node.js lambda函数中有时不同?

ymzxtsji  于 2021-10-10  发布在  Java
关注(0)|答案(0)|浏览(286)

我不熟悉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数组。

  1. imageList = croppedImage.tolist()

然后使用下面的代码交换其中的一些元素(请注意imagelist数组的大小是224x224x3)

  1. for i in range(224):
  2. for j in range(224):
  3. a=imageList[i][j][0]
  4. imageList[i][j][0]=imageList[i][j][2]
  5. imageList[i][j][2]=a

然后使用下面的代码将其返回给父lambda。

  1. return {
  2. 'statusCode': 200,
  3. 'headers': {
  4. 'Content-Type': 'application/json'
  5. },
  6. 'body': json.dumps({
  7. 'returnArray': imageList
  8. })
  9. }

最后用返回的数组替换'img'变量。
父lambda中的代码块如下所示。我使用第1行将其转换为json对象,第2行获取返回的数组并将其分配给“img”变量。

  1. const AWS = require('aws-sdk');
  2. AWS.config.region = 'ap-southeast-2';
  3. var lambda = new AWS.Lambda();
  4. exports.handler = async (event, ctx, callback) => {
  5. //////////code lines for other operations////////////
  6. let img = //Line A - 3d array with RGB values of an image;
  7. let body1;
  8. if(condition){
  9. console.log("BEGIN");
  10. var params = {
  11. FunctionName: 'childFunction', // child lambda function written in Pyton 3.6
  12. InvocationType: 'RequestResponse',
  13. Payload: JSON.stringify({ "sendImg" : img})
  14. };
  15. await lambda.invoke(params, function(err, data) {
  16. if (err) {
  17. console.log(err);
  18. } else {
  19. flag = 1;
  20. console.log("Returned : ",data.Payload); //Line B
  21. if(typeof(data.Payload) == 'object') {
  22. body1 = data.Payload;
  23. } else {
  24. body1 = JSON.parse(data.Payload); //Line 1
  25. }
  26. img = JSON.parse(body1.body).returnArray; //Line 2
  27. }
  28. }).promise();
  29. }
  30. ////////Rest of the code///////////////////////
  31. };

这是python lambda函数(子lambda)

  1. import json
  2. import cv2
  3. import numpy as np
  4. def lambda_handler(event, context):
  5. array=event['sendImg']
  6. model = event['model']
  7. print("model : ",model)
  8. for i in range(224):
  9. for j in range(224):
  10. a=array[i][j][0]
  11. array[i][j][0]=array[i][j][2]
  12. array[i][j][2]=a
  13. new_image = np.array(array, np.uint8)
  14. image = new_image
  15. #crop
  16. image2 = image.copy()
  17. lab = cv2.cvtColor(image2, cv2.COLOR_BGR2LAB)
  18. l,a,b = cv2.split(lab)
  19. #perform thresholding
  20. m,thresh = cv2.threshold(a,0,255,cv2.THRESH_BINARY+cv2.THRESH_OTSU)
  21. image6 = image.copy()
  22. contours, hierarchy = cv2.findContours(thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  23. cv2.drawContours(image6, contours, -1, (0,255,0), 1)
  24. i=0
  25. maxArea=0
  26. for c in contours:
  27. i+=1
  28. areaC = cv2.contourArea(c)
  29. if maxArea<areaC:
  30. maxArea=areaC
  31. maxIndex=i
  32. print("Outside")
  33. i=0
  34. image7=image.copy()
  35. image8=image.copy()
  36. for c in contours:
  37. i+=1
  38. if i==maxIndex:
  39. print("Inside")
  40. rect = cv2.minAreaRect(c)
  41. (x, y), (width, height), angle = rect
  42. x=round(x)
  43. y=round(y)
  44. width=round(width)
  45. height=round(height)
  46. box = cv2.boxPoints(rect)
  47. box = np.int0(box)
  48. cv2.drawContours(image7,[box],0,(0,255,0),2)
  49. src_pts = box.astype("float32")
  50. dst_pts = np.array([[0, height-1],
  51. [0, 0],
  52. [width-1, 0],
  53. [width-1, height-1]], dtype="float32")
  54. M = cv2.getPerspectiveTransform(src_pts, dst_pts)
  55. warped = cv2.warpPerspective(image8, M, (width, height))
  56. croppedImage = cv2.resize(warped, (224,224), interpolation = cv2.INTER_AREA)
  57. imageList = croppedImage.tolist()
  58. for i in range(224):
  59. for j in range(224):
  60. a=imageList[i][j][0]
  61. imageList[i][j][0]=imageList[i][j][2]
  62. imageList[i][j][2]=a
  63. print("returnArray ",imageList) #Line 3
  64. return {
  65. 'statusCode': 200,
  66. 'headers': {
  67. 'Content-Type': 'application/json'
  68. },
  69. 'body': json.dumps({
  70. 'returnArray': imageList
  71. })
  72. }

问题:在不同时间使用相同的“img”数组(a行)运行程序时,第1行会给出不同的错误消息。有时它不会给出任何错误消息并成功完成程序。
下面是一些错误消息。
一次发送一条错误消息-

  1. {
  2. "errorType": "SyntaxError",
  3. "errorMessage": "Unexpected token ] in JSON at position 1****",
  4. "code": "SyntaxError",
  5. "message": "Unexpected token ] in JSON at position 1",
  6. "time": "2021-07-19T09:50:14.678Z",
  7. "stack": [
  8. "SyntaxError: Unexpected token ] in JSON at position 1",
  9. " at JSON.parse (<anonymous>)",
  10. " at Response.<anonymous> (/var/task/index.js:81:30)",
  11. " at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:369:18)",
  12. " at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
  13. " at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
  14. " at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
  15. " at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
  16. " at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
  17. " at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
  18. " at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)"
  19. ]

其他时间的错误消息-

  1. {
  2. "errorType": "SyntaxError",
  3. "errorMessage": "Unexpected token , in JSON at position 3",
  4. "code": "SyntaxError",
  5. "message": "Unexpected token , in JSON at position 3",
  6. "time": "2021-07-19T09:10:41.553Z",
  7. "stack": [
  8. "SyntaxError: Unexpected token , in JSON at position 3",
  9. " at JSON.parse (<anonymous>)",
  10. " at Response.<anonymous> (/var/task/index.js:81:30)",
  11. " at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:369:18)",
  12. " at Request.callListeners (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:106:20)",
  13. " at Request.emit (/var/runtime/node_modules/aws-sdk/lib/sequential_executor.js:78:10)",
  14. " at Request.emit (/var/runtime/node_modules/aws-sdk/lib/request.js:688:14)",
  15. " at Request.transition (/var/runtime/node_modules/aws-sdk/lib/request.js:22:10)",
  16. " at AcceptorStateMachine.runTo (/var/runtime/node_modules/aws-sdk/lib/state_machine.js:14:12)",
  17. " at /var/runtime/node_modules/aws-sdk/lib/state_machine.js:26:10",
  18. " at Request.<anonymous> (/var/runtime/node_modules/aws-sdk/lib/request.js:38:9)"
  19. ]
  20. }

您可以看到,在上述两个错误中,标记和位置是不同的。
有时它不会给出任何错误消息并成功完成程序。
我在子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中有时不同?
谢谢

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题