camera4kivy: clear_screen and reduced requirments

84 views
Skip to first unread message

Peter O

unread,
Jan 28, 2023, 11:43:08 PM1/28/23
to Kivy users support
Hello
Thanks for the great work.
It's nice how you can turn the camera on and off with camera4kivy.
So i only switch on when barcode-reading is needed.
Tested with windows and android.

I am using it this way, maybe old-fashioned:

        self.ids.camera.connect_camera()
        self.clockrun1 = Clock.schedule_once(self.wait_for_camera, 3)

def wait_for_camera(self,*args):
       self.clockrun2 = Clock.schedule_interval(self.read_barcode, 0.05)

def read_barcode(self,*args):
        cam_image = self.ids.camera.export_as_image()        #  done in my kv-file
        texture = cam_image.texture                                            #  Preview:                      
        size = texture.size                                                               #      id: camera
        pixels = texture.pixels
        pil_image = Image.frombytes(mode="RGBA", size=size, data=pixels)
        barcode = pyzbar.decode(pil_image)
        if len(barcode) > 0:
            self.captext = str(barcode[0].data.decode("utf-8"))                  
            self.stop_scanning() 
 
    def stop_scanning(self):
        self.clockrun2.cancel()
        self.ids.camera.disconnect_camera()

When i start barcode-reading for second time and later, at first there is the old image visible until the camera realy works. Same happens with "analyze_pixels_callback", wich i could not handle on second run ( i am a beginner ).

My two questions are:

1) Is it possible to clear the camera screen or cache on every start, so i don't have to use clock.schedule_once for 3 seconds (to be sure for every device)?
Otherwise the previous barcode is shown.

2) In this case i don't need no gestures4kivy (buildozer is asking) and microphone-permissions.  Is it possible to delete this functions?

Again, thank you very much.

Robert

unread,
Jan 29, 2023, 12:21:53 AM1/29/23
to Kivy users support
> 1) Is it possible to clear the camera screen or cache on every start, so i don't have to use clock.schedule_once for 3 seconds (to be sure for every device)?
  Otherwise the previous barcode is shown.

This is a known behavior, some users have worked around but I don't know how. My previous attempts to address this failed. I will have another try sometime sooner or later (so not a schedule you can rely on).

2) In this case i don't need no gestures4kivy (buildozer is asking) and
microphone-permissions.  Is it possible to delete this functions?

- It is OK not to ask for RECORD_AUDIO, in fact the qr example does exactly that.
- gestures4kivy is a dependency.

Peter O

unread,
Jan 29, 2023, 12:36:29 AM1/29/23
to Kivy users support
Thanks Robert
"It is OK not to ask for RECORD_AUDIO, in fact the qr example does exactly that."

Right. I think i can delete that part in camerax_provider/gradle_options.py.

Maybe someone else has got a workaround for a clear screen after camera restart?

Peter O

unread,
Jan 30, 2023, 3:57:50 PM1/30/23
to Kivy users support
Here is a small update to my question number 1

A simple way to get a "clear screen" is setting opacity to zero:        self.ids.camera.opacity = "0"
That looks good.

A simple solution for the "reading old barcode problem" is: first wait until len(barcode) == 0 , after that look out for a new one.
Thats as fast as possible and we don't have to schedule_once for 3 seconds (in my case) .

But for now, i can't combine this two solutions. Because opacity takes effect for the analyzing-part, too.
It would all be very easy if you knew exactly when the camera was actually running, but how to do this?
self.ids.camera_connected (True or False) is not working here, because it becomes True directly after self.ids.camera.connect_camera()

I hope my English is reasonably understandable. So, maybe someone has any idea.

Robert

unread,
Jan 30, 2023, 8:04:31 PM1/30/23
to Kivy users support
As I remember the issue was CameraX api was providing a 'stopped' signal that was 10-15 frames late. Any further details were deleted from my head to make space for other things. At some time I will look at it again with fresh eyes, but not now. 

You can see the original conversation with CameraX support here https://groups.google.com/a/android.com/g/camerax-developers/c/CChnrBp98r0/m/JEbz72ZyBQAJ  though the original question was different it is basically the same issue.

Peter O

unread,
Feb 4, 2023, 7:42:33 AM2/4/23
to Kivy users support
Again, thank you Robert.

It's a little bit to hard for me, to get the "OnFrameAvailable()".
I wonder if this is realy a bug from Andeoid camerax, because Opencv on windows is doing the same (showing old image for a short moment, when camera starts again).

Now I just accept it and start the barcode search as soon as the image has disappeared. There is also a small learning curve in seeing the successful old shot again before searching a new one. It's a feature, not a bug :)

Robert

unread,
Feb 4, 2023, 12:10:39 PM2/4/23
to Kivy users support
I started working on this and other minor enhancements, found it can occur 3 ways all threading issues (both generally and in camerax), and there is another case the examples.
No schedule for a release at the moment.

Robert

unread,
Feb 9, 2023, 9:12:17 PM2/9/23
to Kivy users support
Fix is released,  also get the new camerax_provider version.

Peter O

unread,
Feb 10, 2023, 1:31:28 PM2/10/23
to Kivy users support
First of all, I can confirm that it now works exactly as I imagined. And as desirable for repeated barcode reading I think. I am very surprised and delighted by the fact that you were able to make this possible. And it's an honor for me to communicate with Robert F, something I've been unclear on until now. Thank you very much. I really didn't expect that.

Robert

unread,
Feb 10, 2023, 3:05:45 PM2/10/23
to Kivy users support
😎
Reply all
Reply to author
Forward
0 new messages