How to access android's camera using opencv and kivy? - python

I have been trying to make an android apk in kivy to access mobile camera through opencv, but my application crashes in my phone. It is probably not able to access my phone's camera!
import imp
from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDRaisedButton
from kivy.uix.image import Image
from kivy.clock import Clock
import cv2
from kivy.graphics.texture import Texture
class MainApp(MDApp):
def build(self):
layout = MDBoxLayout(orientation='vertical')
self.image = Image()
layout.add_widget(self.image)
layout.add_widget(MDRaisedButton(
text='Click Here',
pos_hint={'center_x':.5,'center_y':.5},
size_hint=(None,None))
)
self.capture=cv2.VideoCapture(0)
Clock.schedule_interval(self.load_video, 1.0/30.0)
return layout
def load_video(self, *args):
ret, frame= self.capture.read()
self.frame=frame
buffer=cv2.flip(frame, 0).tostring()
texture=Texture.create(size=(frame.shape[1],frame.shape[0]),colorfmt='bgr')
texture.blit_buffer(buffer, colorfmt='bgr',bufferfmt='ubyte')
self.image.texture=texture
if __name__ == '__main__':
MainApp().run()
Also the changes I have made in my buildozer.spec folder are as follows:

Related

How can I play a video with kivy v2.0.0?

I want to play a video with Kivy 2.0.0 and Python 3.6, but running this code will give me a load error because Kivy's video player won't recognize the file. How can I get Kivy's video player to recognize the file?
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.videoplayer import VideoPlayer
from kivy.properties import ObjectProperty
from kivy.core.window import Window
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.uix.label import Label
from kivy.uix.video import Video
from kivy.lang.builder import *
from kivy.app import App
import kivy
import os
kivy.require('2.0.0')
# Main page code
class MainPage(Screen):
""" This is the code for the Main Screen """
# Initialization
def __init__(self, **var_args):
# Constructor for the Main Page class
super(MainPage, self).__init__(**var_args)
# Layout
self.layout = FloatLayout()
# Video player
self.filename = '/'
self.player = VideoPlayer(source=self.filename,state='play',options={'allow_stretch': True})
self.layout.add_widget(self.player)
# Add the layout to the screen
self.add_widget(self.layout)
# Main app class
class Video_Viewer(App):
""" Main build """
# Main build
def build(self, **kwargs):
# Create the screen manager
self.manager = ScreenManager()
# Add screens to screen manager
self.manager.add_widget(MainPage(name='Main'))
# Set the screen to the main sceren
self.manager.current = 'Main'
# Return the current screen of the screen manager
return self.manager
# Run the file
if __name__ == '__main__':
Video_Viewer().run()
This is the error that happens as a result of the code:
[ERROR ] [Image ] Error loading <C:/Users/My Laptop/Downloads/video.mp4>
Try to install ffpyplayer
python -m pip install ffpyplayer

Image inputed from user is displayed black in kivy after using Intent

I want the user of an android app input an image using on_activity_result funtion which is bind to the activity and then display it in a kivy image widget. I used Intent.createChooser for user's input:
from kivymd.app import MDApp
from kivy.lang import Builder
from kivy.uix.label import Label
from kivy.app import App
from kivy.uix.image import Image, CoreImage
class MainApp(App):
def build(self):
return Builder.load_file('main.kv')
def set_texture(self, root):
i = root.children[0].children[0] # getting the image widget
##set the image as bytes
with open("./small image.png", "rb") as image:
f = image.read()
bytesArray = bytearray(f)
import io
img = CoreImage(io.BytesIO(bytesArray), ext="png").texture
i.texture = img
def set_texture_after_intent(self):
from jnius import autoclass, cast
from android import activity
activity.bind(on_activity_result=on_activity_result)
PythonActivity = autoclass("org.kivy.android.PythonActivity")
context = PythonActivity.mActivity
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
AndroidString = autoclass('java.lang.String')
Intent = autoclass('android.content.Intent')
intent = Intent()
intent.setType("image/*")
intent.setAction(Intent.ACTION_GET_CONTENT)
currentActivity.startActivityForResult(
Intent.createChooser(intent, cast('java.lang.CharSequence', AndroidString("Select Picture"))), 1)
def on_activity_result(request, response, data):
#get the image widget
root = App.get_running_app().root
#call same display function
App.get_running_app().set_texture(root)
MainApp().run()
kv file:
Screen:
id:myscreen
BoxLayout:
Button:
text:'change image normally'
on_release: app.set_texture(root)
Button:
text:'change image after intent'
on_release: app.set_texture_after_intent()
Image:
id: image_id
source:'./big image.png'
if I change the texture of an image without getting back from an intent, it is displayed normally on android devices. I think this could be helpful in understanding the cause. After returning from an intent, the same image read in the same way is displayed black.
It might be a thread/gl context issue, can you try adding the mainthread decorator to your on_activity_result function?
from kivy.clock import mainthread
...
#mainthread
def on_activity_result(request, response, data):
...

Find an object in a library (here, it is the NavigationLayout of the Kivy library)

I am learning Kivy and I'm not used to .kv, so I am trying to learn the "hard way" first.
Problem: there is not a lot of documentation for interaction with Kivy except .kv.
I'm stuck with the import of the NavigationLayout, I tried to find it on the official Github, but I failed !
from kivy.uix.boxlayout import BoxLayout
from kivymd.uix.toolbar import MDToolbar
from kivy.properties import ObjectProperty
from kivy.uix.screenmanager import ScreenManager, Screen
from kivymd.app import MDApp
#from kivy import road.to.navigationlayout import NavigationLayout
class App(MDApp):
def build(self):
screen_manager = ScreenManager()
main_screen = Screen()
main_box = BoxLayout()
main_toolbar = MDToolbar()
main_toolbar.title = "Hello there !"
#navigation_layout = NavigationLayout()
screen_manager.add_widget(main_screen)
main_screen.add_widget(main_box)
main_box.add_widget(main_toolbar)
return screen_manager
App().run()
PS: If you have tips to find out where objects are situated in new libraries, I would be happy to heard them!
It was in the MDKivy library. (I found it with this other stack overflow post)
from kivymd.uix.navigationdrawer import NavigationLayout

FactoryException when trying to create a "IconButton" in Kivy

I've tried to create an Icon that can be clicked, which means an Image with a ButtonBehavior.I followed the documentation (http://kivy.org/docs/api-kivy.uix.behaviors.html), and I've got a FactoryException with the following code:
# coding: utf-8
from kivy.uix.behaviors import ButtonBehavior
from kivy.core.image import Image
from kivy.app import App
from kivy.uix.widget import Widget
from kivy.lang import Builder
kv_string = """
BoxLayout:
IconButton
"""
class IconButton(ButtonBehavior, Image):
def on_press(self):
print("on_press")
class DashboardApp(App):
pass
Builder.load_string(kv_string)
if __name__ == "__main__":
DashboardApp().run()
When I change the parent class of IconButton from (ButtonBehavior, Image) to (ButtonBehavior, Widget), the problem disappears.
You want kivy.uix.image, not kivy.core.image.

Trouble loading image into GUI app, show white screen ? Kivy Python

I'm having trouble loading my image into the GUI for Python using Kivy.All it shows is a white screen. Any help would be appreciated.
from kivy.app import App
from kivy.uix.image import Image
class TheApp(App):
def build(self):
image = Image(source= "psychTREE.jpg")
return image
if __name__ == "__main__":
TheApp().run()

Categories

Resources