numpy array to image

270 views
Skip to first unread message

Gaurav Bhoi

unread,
Jun 17, 2021, 6:24:08 PM6/17/21
to Kivy users support
Hey  guys I have a numpy array which is a single frame i m taking from opencv and I want to set it to image but I m not understanding what I m doing wrong. I have two buttons when I press "grab frame button" it grabs a single frame and then I press "show image" its suppose to show image but image looks distorted .please help thank you.

python code :

from kivy.app import App
from kivy.uix.gridlayout import GridLayout
import cv2
from kivy.graphics.texture import Texture
from kivy.uix.image import Image

vid = cv2.VideoCapture(0)
class Grid(GridLayout):

    def grab_image(self):
        while True:
            _, frame = vid.read()
            global f
            f = frame
            break
        vid.release()

    def show_image(self):
        img = cv2.cvtColor(f, cv2.COLOR_BGR2RGB)
        img = cv2.flip(img, 0)
        w, h, _ = f.shape
        texture = Texture.create(size=(w, h),  colorfmt='luminance')
        texture.blit_buffer(img.flatten(), colorfmt='luminance', bufferfmt='ubyte')

        i = Image(size=(w, h), texture=texture)
        self.add_widget(i)
        # print(f, f.dtype)



class MainApp(App):
    def build(self):
        return Grid()

if __name__ == '__main__':
    MainApp().run()


kv code: 

<Grid>:
    cols: 2

    Button:
        id: btn
        text: 'grab image'
        on_release:
            app.root.grab_image()

    Button:
        id: btn2
        text: 'show image'
        on_release: app.root.show_image()


Gaurav Bhoi

unread,
Jun 17, 2021, 6:42:39 PM6/17/21
to kivy-...@googlegroups.com
Hey guys, I just wanna tell you i got my answer thank you

--
You received this message because you are subscribed to a topic in the Google Groups "Kivy users support" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/kivy-users/Gmh2XnaILNM/unsubscribe.
To unsubscribe from this group and all its topics, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/a610a7b9-e36f-4cf0-b081-42b2c7a6c553n%40googlegroups.com.

Gaurav Bhoi

unread,
Jun 17, 2021, 8:29:06 PM6/17/21
to kivy-...@googlegroups.com
Hey guys i got another problem. please take a look at my code and tell me whats wrong. also when i use tostring() i get a DeprecationWarning. please help e thank you.

python code:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

import cv2
from kivy.graphics.texture import Texture
from kivy.clock import Clock

video_capture = cv2.VideoCapture(0)

class Float(FloatLayout):
    def start_calling(self, dt):
        _, frame = video_capture.read()
        self.img_frame = frame
        buffer = cv2.flip(frame, 0).tobytes()
        # print(buffer)
        texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
        texture.blit_buffer(buffer, colorfmt='bgr', bufferfmt='ubyte')
        self.ids.img.texture = texture
        print(self.ids.img.texture)

    def stop_streaming(self):
        video_capture.release()

class StreamingApp(App):
    def build(self):
        return Float()

    def on_start(self):
        Clock.schedule_interval(Float().start_calling, 1.0 / 30.0)

if __name__ == '__main__':
    StreamingApp().run()


kv code: 

<Float>:
    Image:
        id: img

    Button:
        text: 'stop'
        size_hint: .1, .1
        on_release: app.root.stop_streaming()



Elliot Garbus

unread,
Jun 17, 2021, 9:09:21 PM6/17/21
to kivy-...@googlegroups.com

I can see one issue:

 

Change:

    def on_start(self):
        Clock.schedule_interval(Float().start_calling, 1.0 / 30.0)

to:

    def on_start(self):
        Clock.schedule_interval(self.start_calling, 1.0 / 30.0)

--
You received this message because you are subscribed to the Google Groups "Kivy users support" group.
To unsubscribe from this group and stop receiving emails from it, send an email to kivy-users+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/kivy-users/CAH_LR1VGm9AezYJmXeYmTjjsYeE33TaOzOJCxVrJE9bP76jE9Q%40mail.gmail.com.

 

Gaurav Bhoi

unread,
Jun 17, 2021, 9:27:02 PM6/17/21
to kivy-...@googlegroups.com
But start_calling method is in Float class its throwing me the error as expected  "'StreamingApp' object has no attribute 'start_calling'"

Elliot Garbus

unread,
Jun 17, 2021, 10:31:48 PM6/17/21
to kivy-...@googlegroups.com
Ahh, then use self.root.start_calling
You want to use the existing instance of the class. 

Sent from my iPhone

On Jun 17, 2021, at 6:27 PM, Gaurav Bhoi <bhoiga...@gmail.com> wrote:



Elliot Garbus

unread,
Jun 17, 2021, 11:10:14 PM6/17/21
to kivy-...@googlegroups.com

The highlighted lines are the changes.  The schedule interval needs to be canceled when the streaming is stopped.

 

 

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout

import cv2
from kivy.graphics.texture import Texture
from kivy.clock import Clock

video_capture = cv2.VideoCapture(
0)

class Float(FloatLayout):
   
def start_calling(self, dt):
        _, frame = video_capture.read()
       
self.img_frame = frame
        buffer = cv2.flip(frame,
0
).tobytes()
        texture = Texture.create(
size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
        texture.blit_buffer(buffer,
colorfmt='bgr', bufferfmt='ubyte')
       
self.ids.img.texture = texture
       
# print(self.ids.img.texture)

   
def stop_streaming(self):
        app = App.get_running_app()
        app.schedule.cancel()

        video_capture.release()

class StreamingApp(App):
   
def __init__(self, **kwargs):
       
super().__init__(**kwargs)
       
self.schedule = None


    def
build(self):
       
return Float()

   
def on_start(self
):
       
self.schedule = Clock.schedule_interval(self.root.start_calling, 1.0 / 30.0)

Gaurav Bhoi

unread,
Jun 18, 2021, 6:53:15 PM6/18/21
to kivy-...@googlegroups.com
ok and how can i send this buffer into the firebase database. Is it possible ?

Elliot Garbus

unread,
Jun 18, 2021, 6:57:00 PM6/18/21
to kivy-...@googlegroups.com

I don’t know anything about firebase.  I assume you could convert the buffer to JSON and store it.

Why do you want to store the buffer in firebase?

Gaurav Bhoi

unread,
Jun 19, 2021, 3:58:59 AM6/19/21
to kivy-...@googlegroups.com
ok i tried to convert it to json but i keep geting error that it is not json serializable
if you know how to do it please let me know thank you for your help

Elliot Garbus

unread,
Jun 19, 2021, 11:48:08 AM6/19/21
to kivy-...@googlegroups.com

The data type of buffer is bytes, so you need to convert the data into a text representation.  I used .hex()

In the code below I set it up some it only saves one buffer for my test.

 

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import cv2
import json


video_capture = cv2.VideoCapture(
0)

class Float(FloatLayout):
    once =
True
    def
start_calling(self, dt):
        _, frame = video_capture.read()
       
self.img_frame = frame
        buffer = cv2.flip(frame,
0).tobytes()
       
if self.once:
           
with open('buffer_file.txt', 'w') as f:
                json.dump(buffer.hex(), f)
               
self.once = False
Reply all
Reply to author
Forward
0 new messages