Last active
August 12, 2019 15:45
-
-
Save linnil1/ad285486f8aa742d99653b69c3f71527 to your computer and use it in GitHub Desktop.
Image processing by Kivy on Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from kivy.logger import Logger | |
import logging | |
Logger.setLevel(logging.TRACE) | |
from kivy.app import App | |
import kivy | |
from kivy.lang import Builder | |
from kivy.uix.widget import Widget | |
from kivy.uix.boxlayout import BoxLayout | |
from kivy.uix.camera import Camera | |
from kivy.graphics.texture import Texture | |
import cv2 | |
import time | |
class MyCamera(Camera): | |
def __init__(self, **kwargs): | |
super(MyCamera, self).__init__(**kwargs) | |
def _camera_loaded(self, *largs): | |
if kivy.platform == 'android': | |
self.texture = Texture.create(size=self.resolution, colorfmt='rgb') | |
self.texture_size = list(self.texture.size) | |
else: | |
self.texture = self._camera.texture | |
self.texture_size = list(self.texture.size) | |
def on_tex(self, *l): | |
if kivy.platform == 'android': | |
buf = self._camera.grab_frame() | |
if buf is None: | |
return | |
frame = self._camera.decode_frame(buf) | |
else: | |
ret, frame = self._camera._device.read() | |
if frame is None: | |
print("No") | |
buf = self.process_frame(frame) | |
self.texture.blit_buffer(buf, colorfmt='rgb', bufferfmt='ubyte') | |
super(MyCamera, self).on_tex(*l) | |
def process_frame(self,frame): | |
# Process frame with opencv | |
print(123) | |
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV) | |
return frame.tostring() | |
class CameraClick(BoxLayout): | |
def capture(self): | |
''' | |
Function to capture the images and give them the names | |
according to their captured time and date. | |
''' | |
camera = self.ids['camera'] | |
# camera.export_to_png("IMG_{}.png".format(timestr)) | |
print("Captured") | |
Builder.load_string(''' | |
<CameraClick>: | |
size: root.size | |
orientation: 'vertical' | |
MyCamera: | |
id: camera | |
index: 0 | |
resolution: (640, 480) | |
size_hint: 1, .7 | |
play: True | |
Button: | |
text: 'Capture' | |
size_hint: 1, .3 | |
on_press: root.capture() | |
''') | |
class TestCamera(App): | |
def build(self): | |
return CameraClick() | |
TestCamera().run() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Can you compile this code to IOS OR Android while you are using Opencv Library??? Or other python library can work on android after build??