使用屏幕管理器时不显示相机图像
Posted
技术标签:
【中文标题】使用屏幕管理器时不显示相机图像【英文标题】:Camera images are not displaying when using a screen manager 【发布时间】:2021-10-22 20:04:49 【问题描述】:我需要在单独的屏幕中实现相机预览。应使用按钮启动和停止相机。我已经实现了下面的代码。当我按下开始相机按钮时,相机正在闪烁并且正在工作。但是来自相机的图像没有显示在我的屏幕上(在 UploadScreen 中。)请帮助。
main.py
from kivymd.app import MDApp
from kivy.lang.builder import Builder
from kivy.uix.screenmanager import Screen
from screen_nav import screen_helper
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import cv2
from kivy.properties import BooleanProperty
class MenuScreen(Screen):
pass
class ProfileScreen(Screen):
pass
class UploadScreen(Screen):
cameraActive = BooleanProperty(False)
capture = cv2.VideoCapture()
def start_camera(self):
if not self.cameraActive:
self.ids.camera_button.text = 'Stop Camera'
self.image = Image()
self.capture = cv2.VideoCapture(0)
if self.capture.isOpened():
self.cameraActive = True
Clock.schedule_interval(self.update, 1.0 / 10.0)
else:
print('Cannot Open the Camera at index 0')
else:
self.cameraActive = False
self.ids.camera_button.text = 'Start Camera'
if self.capture.isOpened():
self.capture.release()
return self.capture
def on_upload_back(self):
self.cameraActive = False
self.ids.camera_button.text = 'Start Camera'
if self.capture.isOpened():
self.capture.release()
self.manager.current = 'menu'
def update(self, dt):
ret, frame = self.capture.read()
if ret:
# convert it to texture
buf1 = cv2.flip(frame, 0)
buf = buf1.tobytes()
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.image.texture = image_texture
self.ids.my_image = self.image
class DemoApp(MDApp):
def build(self):
screen = Builder.load_string(screen_helper)
return screen
DemoApp().run()
screen_nav.py
screen_helper = """
ScreenManager:
MenuScreen:
ProfileScreen:
UploadScreen:
<MenuScreen>:
name: 'menu'
MDRectangleFlatButton:
text: 'Profile'
pos_hint: 'center_x':0.5,'center_y':0.5
on_press: root.manager.current = 'profile'
MDRectangleFlatButton:
text: 'Upload'
pos_hint: 'center_x':0.5,'center_y':0.4
on_press: root.manager.current = 'upload'
<ProfileScreen>:
name: 'profile'
MDLabel:
text: 'Profile'
halign: 'center'
MDRectangleFlatButton:
text: 'Back'
pos_hint: 'center_x':0.5,'center_y':0.1
on_press: root.manager.current = 'menu'
<UploadScreen>:
name: 'upload'
Image:
id: my_image
MDRectangleFlatButton:
text: 'Back'
pos_hint: 'center_x':0.5,'center_y':0.1
# on_press: root.manager.current = 'menu'
on_press: root.on_upload_back()
MDRectangleFlatButton:
id: camera_button
text: 'Start Camera'
pos_hint: 'center_x':0.7,'center_y':0.1
on_press: root.start_camera()
# on_press: camera.play = not camera.play
"""
【问题讨论】:
【参考方案1】:一个小问题,一行:
self.image = Image()
正在创建一个新的Image
对象,该对象将使用来自相机的图像进行更新。但是那个Image
实例不是您在kv
中定义的实例,也不在您的GUI 中。只需将该行替换为:
self.image = self.ids.my_image
访问 GUI 中的 Image
。
【讨论】:
非常感谢@John Anderson,它确实有效。太感谢了。您还可以建议以下方法有什么问题。 ***.com/questions/68875754/… 我已经发布了这个问题的答案。 非常感谢约翰。你真是天才。我是 python 编程新手,非常感谢您的帮助。 嗨John Anderson你能帮忙解决这个***.com/questions/68976164/…以上是关于使用屏幕管理器时不显示相机图像的主要内容,如果未能解决你的问题,请参考以下文章
UnsatisfiedLinkError - 在 Android 中旋转相机图像数据(在 OpenCV 中使用,而不仅仅是在屏幕上显示)