piUek
piUek

Reputation: 53

How to refresh Image.texture from CoreImage.texture?


I'm looking for a way to refresh camera miniature images from snapshots. I have this piece of code, but after first refresh (not the one in the reloadMiniatures thread) I get nothing (black screen).
I have tried other solutions but showing 6x mjpeg streams was to heavy (and I don't really need high framerate). Had some success with AsyncImage and saving images to file, but it wasn't very efficient and I had this loading_image to get rid of.

from kivy.app import App
from kivy.uix.image import Image
import time
import threading
import urllib
from kivy.core.image import Image as CoreImage
from io import BytesIO

class TestApp(App):
    def reloadMiniatures(self):
        while True:
            data = BytesIO(urllib.urlopen("http://10.0.13.206:9000/?action=snapshot").read())
            time.sleep(3)
            self.image.texture = CoreImage(data, ext='jpg').texture

    def build(self):
        data = BytesIO(urllib.urlopen("http://10.0.13.206:9000/?action=snapshot").read())
        self.image = Image()
        self.image.texture = CoreImage(data, ext='jpg').texture

        miniatures = threading.Thread(target=self.reloadMiniatures)
        miniatures.daemon = True
        miniatures.start()

        return self.image

TestApp().run()

Upvotes: 1

Views: 426

Answers (1)

kitti
kitti

Reputation: 14854

You could try using Loader instead:

def load_miniatures(self, *args):
    proxy = Loader.image('http://10.0.13.206:9000/?action=snapshot')
    proxy.bind(on_load=self.receive_miniatures)

def receive_miniatures(self, proxy):
    if proxy.image.texture:
        self.image.texture = proxy.image.texture
    Clock.schedule_once(self.load_miniatures, 0.1)

def build(self):
    self.image = Image()
    self.load_miniatures()
    return self.image

Upvotes: 2

Related Questions