Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Suggestions for performance improvement of Image control #4665

Open
1 task done
lasifea opened this issue Jan 8, 2025 · 0 comments
Open
1 task done

Suggestions for performance improvement of Image control #4665

lasifea opened this issue Jan 8, 2025 · 0 comments

Comments

@lasifea
Copy link

lasifea commented Jan 8, 2025

Duplicate Check

Describe the requested feature

Happy new year! I hope the Image control can provide a way to directly convert image byte data, thereby avoiding unnecessary base64 encoding and decoding in some scenarios. Here is a common real-time video display example for deep learning, which requires the installation of opencv-python module. If the base64 encoding and decoding are omitted, the frame rate is expected to increase by 5%.

import flet as ft
import numpy as np
import cv2
import base64
import time


def main(page: ft.Page):
    def update_img():
        last_time = 0
        while True:
            current_time = time.time()
            fps = round(1 / (current_time - last_time), 1)
            last_time = current_time

            img = np.random.randint(0, 256, size=(640, 640, 3), dtype=np.uint8)
            cv2.putText(img, f'FPS: {fps}', (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, 16)
            img_data = cv2.imencode('.jpg', img)[1].tobytes()
            img_widget.src_base64 = base64.b64encode(img_data).decode('utf-8')
            img_widget.update()

    page.vertical_alignment = ft.MainAxisAlignment.CENTER
    page.horizontal_alignment = ft.CrossAxisAlignment.CENTER
    page.window.width = 480
    page.window.height = 480

    img_widget = ft.Image(src_base64='', width=320, height=320, filter_quality=ft.FilterQuality.MEDIUM)
    # container = ft.Container(image=ft.DecorationImage(src_base64=''),
    #                          border=ft.border.all(1), border_radius=4, width=640, height=640)
    page.add(img_widget)
    page.run_thread(update_img)


if __name__ == '__main__':
    ft.app(main)

Suggest a solution

My idea is to add bytes format to the src attribute, so that the src: Union[None, str, bytes].

Screenshots

image

Additional details

No response

@lasifea lasifea changed the title Suggestions for performance improvement of **Image** control Suggestions for performance improvement of Image control Jan 8, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant