You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@teaclave.apache.org by ms...@apache.org on 2020/08/07 20:23:05 UTC

[incubator-teaclave] branch master updated: Use byte array instead of base64 for image transfer making parsing faster (#402)

This is an automated email from the ASF dual-hosted git repository.

mssun pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/incubator-teaclave.git


The following commit(s) were added to refs/heads/master by this push:
     new d60ce97  Use byte array instead of base64 for image transfer making parsing faster (#402)
d60ce97 is described below

commit d60ce97c78510398a9206a7d5f53d598c12f6b15
Author: Mingshen Sun <bo...@mssun.me>
AuthorDate: Fri Aug 7 13:22:55 2020 -0700

    Use byte array instead of base64 for image transfer making parsing faster (#402)
---
 examples/python/builtin_face_detection.py | 11 ++++-------
 function/src/face_detection.rs            | 12 +++++-------
 2 files changed, 9 insertions(+), 14 deletions(-)

diff --git a/examples/python/builtin_face_detection.py b/examples/python/builtin_face_detection.py
index c7c56ec..ebf3267 100644
--- a/examples/python/builtin_face_detection.py
+++ b/examples/python/builtin_face_detection.py
@@ -2,7 +2,6 @@
 
 import os
 import sys
-import base64
 import json
 
 from PIL import Image, ImageDraw
@@ -21,7 +20,7 @@ class BuiltinFaceDetectionExample:
         self.user_id = user_id
         self.user_password = user_password
 
-    def detect_face(self, image_base64):
+    def detect_face(self, image):
         client = AuthenticationService(
             AUTHENTICATION_SERVICE_ADDRESS, AS_ROOT_CA_CERT_PATH,
             ENCLAVE_INFO_PATH).connect().get_client()
@@ -45,7 +44,7 @@ class BuiltinFaceDetectionExample:
             executor_type="builtin",
             inputs=[],
             arguments=[
-                "image_base64", "min_face_size", "score_thresh",
+                "image", "min_face_size", "score_thresh",
                 "pyramid_scale_factor", "slide_window_step_x",
                 "slide_window_step_y"
             ])
@@ -53,7 +52,7 @@ class BuiltinFaceDetectionExample:
         print("[+] creating task")
         task_id = client.create_task(function_id=function_id,
                                      function_arguments={
-                                         "image_base64": image_base64,
+                                         "image": image,
                                          "min_face_size": 20,
                                          "score_thresh": 2.0,
                                          "pyramid_scale_factor": 0.8,
@@ -89,11 +88,9 @@ def main():
 
     with open(img_file_name, 'rb') as fin:
         image_data = fin.read()
-        base64_encoded_data = base64.b64encode(image_data).decode('utf-8')
-
         example = BuiltinFaceDetectionExample(USER_ID, USER_PASSWORD)
 
-        rt = example.detect_face(base64_encoded_data)
+        rt = example.detect_face(list(image_data))
 
         print("[+] function return:", rt)
 
diff --git a/function/src/face_detection.rs b/function/src/face_detection.rs
index 5acf5e1..944a69f 100644
--- a/function/src/face_detection.rs
+++ b/function/src/face_detection.rs
@@ -30,7 +30,7 @@ pub struct FaceDetection;
 
 #[derive(serde::Deserialize)]
 struct FaceDetectionArguments {
-    image_base64: String,
+    image: Vec<u8>,
     /// Set the size of the sliding window.
     ///
     /// The minimum size is constrained as no smaller than 20.
@@ -108,10 +108,8 @@ impl FaceDetection {
         _runtime: FunctionRuntime,
     ) -> anyhow::Result<String> {
         let arguments = FaceDetectionArguments::try_from(arguments)?;
-        let image_base64 = arguments.image_base64;
-        let vec = base64::decode(&image_base64).unwrap();
-        let bytes: &[u8] = &vec;
-        let img = image::load_from_memory(&bytes)?;
+        let image = arguments.image;
+        let img = image::load_from_memory(&image)?;
 
         let mut detector = rustface::create_default_detector()?;
         if let Some(window_size) = arguments.window_size {
@@ -157,9 +155,9 @@ pub mod tests {
 
     fn test_face_detection() {
         let input = "fixtures/functions/face_detection/input.jpg";
-        let image_base64 = base64::encode(&fs::read(input).unwrap());
+        let image = fs::read(input).unwrap();
         let arguments = FunctionArguments::from_json(json!({
-            "image_base64": image_base64,
+            "image": &image,
             "min_face_size": 20,
             "score_thresh": 2.0,
             "pyramid_scale_factor": 0.8,


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@teaclave.apache.org
For additional commands, e-mail: commits-help@teaclave.apache.org