How to send an image or matrix (gray scale) in a request Python-C#?

I have a Python API interfacing with a C# application. The C# component transmits an image to the API, which undergoes byte conversion followed by base64 encoding to facilitate transmission within a JSON request.

Subsequently, the API (python) decodes the base64 data to reconstruct the image for processing within a Convolutional Neural Network (CNN). The CNN outputs a binary response, indicating the presence or absence of a detected object.

However, the current end-to-end processing time is suboptimal, averaging approximately 1.5 seconds. I seek to achieve a significantly faster response time, ideally targeting a latency of around 200 milliseconds.

Are there alternative approaches to include the image in the request without prior conversion, such as transmitting a grayscale matrix or the object image directly?

Concerning the CNN model, I possess the H5 file. Are there alternative model formats conducive to faster inference? Attempts to mitigate latency by image resizing and reducing CNN hidden layers have proven inadequate.

My codes:

Python

@app.post('/overlap/')
async def overlap(request: Request):
    global cnn
    body = await request.json()
    image_np = np.frombuffer(base64.b64decode(body["image"]), dtype=np.uint8)
    image = cv2.imdecode(image_np, cv2.IMREAD_GRAYSCALE)
    image_resized = cv2.resize(image, (360, 300))
    gamma_corrected = tf.pow(image_resized / 255.0, 0.6) * 255.0
    image_batch = np.expand_dims(gamma_corrected, axis=0)
    result = cnn.predict(image_batch)
    if result[0][0] > result[0][1]:
        overlap_result = False
    else:
        overlap_result = True
    return [overlap_result, str(result[0][0]), str(result[0][1])]

C#

using System;
using System.Diagnostics;
using System.IO;
using System.Net;
using System.Text;
using System.Threading;

namespace comunica_api
{
    class Program
    {
        static void Main(string[] args)
        {
            string imagePath = @"C:my_photo.jpg";

            try
            {
                Stopwatch stopwatch1 = new Stopwatch();
                stopwatch1.Start();

                byte[] imageBytes = File.ReadAllBytes(imagePath);
                string base64String = Convert.ToBase64String(imageBytes);

                Console.WriteLine("Image read");

                string url = "http://localhost:8000/overlap/";
                string json = "{"image": "" + base64String + ""}";

                var request = (HttpWebRequest)WebRequest.Create(url);
                request.Method = "POST";
                request.ContentType = "application/json";
                request.ContentLength = json.Length;

                byte[] jsonBytes = Encoding.UTF8.GetBytes(json);

                using (Stream requestStream = request.GetRequestStream())
                using (StreamWriter writer = new StreamWriter(requestStream))
                {
                    writer.Write(json);
                }

                stopwatch1.Stop();

                Stopwatch stopwatch2 = new Stopwatch();
                stopwatch2.Start();

                using (var response = (HttpWebResponse)request.GetResponse())
                {
                    using (var streamReader = new StreamReader(response.GetResponseStream()))
                    {
                        string responseText = streamReader.ReadToEnd();
                        stopwatch2.Stop();
                        Console.WriteLine("API Response:");
                        Console.WriteLine(responseText);
                        long elapsedTimeMs1 = stopwatch1.ElapsedMilliseconds;
                        long elapsedTimeMs2 = stopwatch2.ElapsedMilliseconds;

                        Console.WriteLine($"Image conversion time: {elapsedTimeMs1} ms");
                        Console.WriteLine($"API response time: {elapsedTimeMs2} ms");
                        Thread.Sleep(3000);
                    }
                }
            }
            catch (FileNotFoundException)
            {
                Console.WriteLine("The specified image could not be found.");
                Thread.Sleep(3000);
            }
            catch (WebException ex)
            {
                Console.WriteLine("API communication error:");
                Console.WriteLine(ex.Message);
                Thread.Sleep(3000);
            }

            Console.ReadLine();
        }
    }
}

Any idea for this problem?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật