Kivy: White screen on OpenCV3, but works on OpenCV2

Created on 25 Sep 2017  路  10Comments  路  Source: kivy/kivy

Versions

  • Python: v2.7.12
  • OS: Linux (Gentoo)
  • Kivy: 1.10.0
  • Kivy installation method: pip install kivy==1.10.0

Description

I want to use my camera in Kivy using the official example code from here:
https://github.com/kivy/kivy/blob/1.10.0/examples/camera/main.py
However I only get a white screen when using cv2.so from opencv-3.3.0|. If I compileopencv-2.4.13.3` it works.
It's interesting to note that in both cases the camera seems to get initialised (led turning on), but with opencv-2 the video shows, while the screen stays white with opencv-3.
I also tried with a more minimalist example below:

import kivy

from kivy.app import App
from kivy.uix.camera import Camera

class MainApp(App):
    def build(self):
        return Camera(play=True, resolution=(640, 480))

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

And I get the same result i.e. works with opencv-2.4.13.3 not with opencv-3.3.0.

I downloaded both version from https://github.com/opencv/opencv/releases.
And they were both compiled with default flags (simply cmake and then make -j4).
Also for both the pure OpenCV example below is working.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame
    ret, frame = cap.read()

    # Display the resulting frame
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()

So it really looks like a problem with its integration in Kivy.

Most helpful comment

Solved it. Look in the file .../lib/python3.6/site-packages/kivy/core/camera/camera_opencv.py
line 121:

        if self.fps <= 0:
            self.fps = 1 / 30.

When you get there in debug mode, you see that the fps value is actually 30 (and not 1/30).
This value is then used for the frame-update scheduling:

self._update_ev = Clock.schedule_interval(self._update, self.fps)

Changing this line to be:

self._update_ev = Clock.schedule_interval(self._update, 1/self.fps)

solves the problem. It's of course not a complete solution, and probably other places in the code should change as well.
Hope this works for you as well

All 10 comments

I can confirm the problem

I also have a white screen with opencv 3.
I debugged it and saw that incoming frames are not empty, but still they don't show well on screen

Solved it. Look in the file .../lib/python3.6/site-packages/kivy/core/camera/camera_opencv.py
line 121:

        if self.fps <= 0:
            self.fps = 1 / 30.

When you get there in debug mode, you see that the fps value is actually 30 (and not 1/30).
This value is then used for the frame-update scheduling:

self._update_ev = Clock.schedule_interval(self._update, self.fps)

Changing this line to be:

self._update_ev = Clock.schedule_interval(self._update, 1/self.fps)

solves the problem. It's of course not a complete solution, and probably other places in the code should change as well.
Hope this works for you as well

None of the above 'solutions' work on Ubuntu 18.04 (Beta release) with python 2.7.14 and opencv 3.
Webcam is working with desktop apps like cheese.
We, braintrainerplus.nl, really need the webcam working and are prepared to put a bounty on this issue if anyone is interested.

I wanted to preprocess the numpy array data returned by opencv, before displaying it. Unfortunately, kivy鈥檚 camera abstraction only offered texture binary data.

This gist was useful to access the camera directly:

https://gist.github.com/ExpandOcean/de261e66949009f44ad2

FYI: opencv 2.4.13 works with kivy 1.10 on Ubuntu 18.04.
(Not sure why my first attempt failed but after another compile it worked)

Can confirm that @yoelk's "fix" works on Arch Linux using Python 3.6.5 and OpenCV 3.3.0 inside a Virtual Env.

This looks like the same issue as https://github.com/kivy/kivy/issues/5146

@yoelk my line 121 is
if self.fps <= 0: self.fps = 1 / 30

And i'm getting the white screen. I'm not getting to download opencv2 either using pip or conda in my attempt to downgrade

I can confirm that the webcam, tested with build-in bison and usb logitech, are now working on stable kivy 1.10.1 with the fix from @yoelk .
System: Ubuntu 18.04/Mint 19, openvc 3.2.0 and kivy 1.10.1 from ppa:kivy-team/kivy.

I also have to comment out all the providers except the opencv in kivy/core/camera/__init__.py to force the use of opencv as the rest just fails and give all kinds of wierd errors.

@CurtlyCritchlow the fix is in line 152: self._update_ev = Clock.schedule_interval(self._update, 1/self.fps)

Was this page helpful?
0 / 5 - 0 ratings