問題描述
我想在 kivy 中插入一個 opencv 網絡攝像頭視頻.不幸的是,當我這樣做并最大化窗口時,圖像不會調整到屏幕尺寸.有沒有辦法做到這一點?
I want to insert an opencv webcam video in kivy. Unfortunately, when I do this and maximize the window the image does not adjust to the screen size. Is there any way to do this ?
from kivy.app import App
from kivy.uix.image import Image
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
from kivy.uix.boxlayout import BoxLayout
from kivy.uix.gridlayout import GridLayout
from kivy.uix.widget import Widget
from kivy.uix.button import Button
from kivy.lang import Builder
from kivy.core.window import Window
Window.size=(1000,1000)
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:
buf1 = cv2.flip(frame, 0)
buf = buf1.tostring()
image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='rgb')#(480,640)
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.camera= cv2.VideoCapture(2)
self.my_camera = KivyCamera(capture=self.camera, fps=10,resolution=(1280,960))
return self.my_camera
def on_stop(self):
self.camera.release()
if __name__ == '__main__':
CamApp().run()
我希望當窗口大小增加時框架的大小也會增加.當我嘗試更改 Texture 中的大小時,它給了我錯誤.喜歡要么顯示奇怪的圖像,要么不打開任何窗口.如果有人可以提供幫助,那就太好了.謝謝 !
I am hoping that the size of the frame increases when winow size increases. When I try to change the size in Texture it gives me error. Like either shows weird images or does not open any window. It would be great if somebody could help . Thanks !
推薦答案
你可以這樣做:將類類型更改為 BoxLayout.使用 size_hint=(1,1) 將圖像小部件添加到 Boxlayout.在 init 中使用大小綁定,如下所示:
you can do this: change the class type to BoxLayout. adding Image widget to Boxlayout with size_hint=(1,1). use size binding in init like this:
self.image = Image(size_hint=(1,1))
self.add_widget(self.image)
self.binding(size=self.Updatesize,
pos=self.Updatesize)
并像這樣在你的類中定義一個函數:
and define a function in your class like this:
def Updatesize(*args):
self.image.size=self.size
self.image.pos=self.pos
這篇關于Kivy 使用 opencv.調整圖像大小的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!