#python #python-3.x #kivy #kivy-language #kivymd
Вопрос:
У меня проблема с камерой и плавающей кнопкой действия MD в киви . Я работаю над raspberry pi 4 . и у меня есть камера на малине. в моем проекте есть камера и опции. Я создаю плавающую кнопку действия MD, но камера ничего не показывает .
Я хочу показать камеру , и когда я нажму, например, «воспроизвести», начать запись видео или когда нажму «захватить» , сделайте снимок. а также может увеличивать масштаб .
вот мой код :
код .py :
from kivy.lang import Builder
from kivymd.app import MDApp
from kivy.app import App
from kivy.uix.image import Image,AsyncImage
from kivy.clock import Clock
from kivy.graphics.texture import Texture
import cv2
from kivy.core.window import Window
from kivy.uix.screenmanager import ScreenManager,Screen
from kivy.lang.builder import Builder
from kivy.uix.boxlayout import BoxLayout
import time
from kivy.uix.widget import Widget
from kivy.lang import Builder
from kivy.properties import ObjectProperty
from kivy.uix.image import Image
from kivy.base import runTouchApp
from kivymd.app import MDApp
from kivy.uix.camera import Camera
from kivy.uix.relativelayout import RelativeLayout
from kivy.properties import NumericProperty
from kivy.clock import Clock
from kivy.event import EventDispatcher
selettore = 1
class CamLayout(BoxLayout):
pass
class Manager(ScreenManager):
pass
class Micro(Screen):
def callback(self,instance):
if instance.icon == "play":
self.ids["camera"].play = True
else:
camera = self.ids["camera"]
timestr = time.strftime("%Y%m%d_%H%M%S")
camera.export_to_png("IMG_{}.png".format(timestr))
class KivyCamera(Image):
def update(self,dt):
global selettore
if selettore % 2 == 0:
ret, frame = self.capture.read()
if ret:
buf1 = cv2.flip(frame, 0)
height, width, channels = buf1.shape
# prepare the crop
X, Y = height, width
scaleX, scaleY = int(self.scale * height / 15), int(self.scale * width / 15)
minX,maxX = X - scaleX, scaleX
minY, maxY = Y - scaleY, scaleY
cropped= frame[minX:maxX, minY:maxY]
buf = cv2.resize(cropped, (width, height))
buf = buf.tobytes()
image_texture = Texture.create(
size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
image_texture.flip_vertical()
image_texture.blit_buffer(buf, colorfmt='bgr', bufferfmt='ubyte')
self.texture = image_texture
self.out.write(frame)
else:
self.out.release()
def salva(self):
frame_width = int(self.capture.get(3))
frame_height = int(self.capture.get(4))
self.size = (frame_width, frame_height)
timestr = time.strftime("%Y%m%d_%H%M%S")
self.out = cv2.VideoWriter(f'{timestr}.avi',
cv2.VideoWriter_fourcc(*'MJPG'),
10, self.size)
def on_touch_down(self, touch):
print(touch)
if touch.button == 'scrolldown':
print("down")
self.scale -= 0.1
if self.scale <= 1:
self.scale = 1
else:
print("up")
self.scale = 0.1
if self.scale >= 15:
self.scale = 15
class CamApp(MDApp):
data = {
"image": "capture",
"play": "play"
}
def open(self):
self.root.ids.my_label.text = f'open!!'
def close(self):
self.root.ids.my_label.text = f'close!!'
def build(self):
self.theme_cls.theme_style = "Dark"
self.theme_cls.primary_platte = "BlueGray"
# return Builder.load_file('cv.kv')
return Builder.load_file('cv.kv')
if __name__ == '__main__':
CamApp().run()
and here is .kv file :
name of file => cv.kv
<Manager>:
Micro:
id:micro
name:"micro"
Micro:
orientation: 'vertical'
MDScreen:
orientation:'vertical'
md_bg_color:(1,1,1,1)
Camera:
id: camera
resolution:(440,380)
play:True
pos_hint: {'center_x':.5 , 'center_y':.63 }
CamLayout:
id:box
MDFloatingActionButtonSpeedDial:
data: app.data
root_button_anim:True
bg_color_root_button: 1,1,1,1
color_icon_stack_button: 1,1,1,1
bg_hint_color: 1,1,1,1
callback: root.callback