実現したいこと
OpenCVで読み込んだWebカメラの映像を私の作ったGUIの一部に埋め込みたいと考えております。
しかし、Kivyをはじめて3日の私はどうすればよいか、わかりませんでした。
以下のコードを参考にして、OpenCVのVideoCapture()で読み込んだデータをtextureに変換すればKivyでtextureとして表示できることはわかりました。それの私のコードに実装する方法を教えてください。宜しくお願いします。
埋め込みたい位置は実行して左側のImageです。(Kivy: 18行目)
参考にしたコード
# coding:utf-8 from kivy.app import App from kivy.uix.image import Image from kivy.clock import Clock from kivy.graphics.texture import Texture import cv2 class KivyCamera(Image): def __init__(self, capture, fps, **kwargs): super(KivyCamera, self).__init__(**kwargs) self.capture = capture Clock.schedule_interval(self.update, 1.0 / fps) def update(self, dt): ret, frame = self.capture.read() if ret: # convert it to texture buf1 = cv2.flip(frame, 0) buf = buf1.tostring() image_texture = Texture.create( size=(frame.shape[1], frame.shape[0]), colorfmt='bgr') image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte') # display image from the texture self.texture = image_texture class CamApp(App): def build(self): self.capture = cv2.VideoCapture(1) self.my_camera = KivyCamera(capture=self.capture, fps=30) return self.my_camera def on_stop(self): #without this, app will not exit even if the window is closed self.capture.release() if __name__ == '__main__': CamApp().run()
自分のPythonコード
Python
from kivy.app import App from kivy.lang import Builder from kivy.uix.widget import Widget from kivy.uix.gridlayout import GridLayout from kivy.uix.label import Label from kivy.uix.image import Image from kivy.graphics.texture import Texture from kivy.properties import ObjectProperty from kivy.core.window import Window from kivy.uix.screenmanager import Screen from kivy.graphics import Color, Rectangle from kivy.clock import Clock import cv2 Window.size = (800, 460) Builder.load_file('sample.kv') class Screen(Screen): count = 0 img_src='' posX=100 posY=200 R=300 result='RESULT' accuracy=1 def __init__(self, **kwargs): super(Screen,self).__init__(**kwargs) def add(self): self.count += 1 layout1 = GridLayout(cols=4, size_hint=(1, None), height=60, padding=5) layout1.add_widget(Label(text='[' + str(self.count) + ']')) layout1.add_widget(Image(source=self.img_src)) layout2 = GridLayout(rows=2) layout2.add_widget(Label(text=self.result)) layout2.add_widget(Label(text=str(self.accuracy))) layout3 = GridLayout(rows=3) layout3.add_widget(Label(text=str(self.posX))) layout3.add_widget(Label(text=str(self.posY))) layout3.add_widget(Label(text=str(self.R))) layout1.add_widget(layout2) layout1.add_widget(layout3) self.ids.widget_list.add_widget(layout1) def remove(self): self.ids.widget_list.clear_widgets() class SampleApp(App): def build(self): self.capture = cv2.VideoCapture(0) self.screen = Screen() self.title = 'Sample_Code' return self.screen if __name__ == '__main__': SampleApp().run()
自分のKivyコード
Kivy
<Screen>: BoxLayout: size: root.size padding: 10 orientation: 'vertical' BoxLayout: size_hint_y: 0.1 Label: text: 'Total number : -----------' Label: text: 'Total fee : -----------' BoxLayout: size_hint_y: 0.8 padding: 10 Image: size_hint_x: 0.6 texture: ScrollView: size_hint_x: 0.4 GridLayout: size_hint_y: None height: self.minimum_height id: widget_list cols: 1 rows: 20 orientation: 'vertical' canvas.before: Color: rgba: 1, 1, 1, 0.2 Rectangle: pos: self.pos size: self.size AnchorLayout: anchor_x: 'right' size_hint_y: 0.1 BoxLayout: size_hint: (None, None) size: (300, 40) BoxLayout: padding: 5 Button: text: 'Add' on_press: root.add() BoxLayout: padding: 5 Button: text: 'Remove' on_press: root.remove()
補足情報
Python 3.6.4
Kivy 1.10.1
まだ回答がついていません
会員登録して回答してみよう