繁体   English   中英

使用 Kivy 和 Screenmanager 的 OCR 问题在屏幕之间传递变量

[英]OCR using Kivy and Screenmanager issue passing variable between screens

我正在为视障用户构建 OCR 应用程序。 我希望应用程序立即打开到相机屏幕上,当用户按下按钮拍照时,我希望发生 ocr 过程并在文本屏幕上以 lbl 或 txtbox 显示输出,并让 TTS 读出什么文字说。 我的问题是我无法获取 ocr 的输出并显示它,我不熟悉 screenmanager 或 python。 理想情况下,opencv 和 tesseract 过程将在与捕获相同的功能中发生,但是我无法在以下屏幕上识别输出。 继承人一些代码,任何建议和帮助表示赞赏!

# Importing the libraries
import cv2
import pytesseract

from kivy.app import App
from kivy.lang import Builder
from kivy.uix.screenmanager import Screen

pytesseract.pytesseract.tesseract_cmd = r'D:/pytesseract/tesseract.exe'


# voice_text = ""
# for i in ocrtext.split():
# voice_text += i + ' '

# voice_text = voice_text[:-1]
# voice_text
# engine = pyttsx3.init()
# engine.setProperty("rate", 145)
# voices = engine.getProperty('voices')
# engine.setProperty('voice', voices[0].id)
# engine.say(voice_text)
# engine.runAndWait()


class CameraScreen(Screen):
    def capture(self):
        camera = self.ids['camera']
        camera.export_to_png("./picforocr.png")
        image = cv2.imread("./picforocr.png")
        ocrtext = pytesseract.image_to_string(image)


class TextScreen(Screen):
    pass


GUI = Builder.load_string("""

GridLayout:
    cols: 1
    ScreenManager:
        id: screen_manager
        CameraScreen:
            name: "camera_screen"
            id: camera_screen
        TextScreen:
            name: "text_screen"
            id: text_screen



<CameraScreen>:
    orientation: 'vertical'
    GridLayout:
        cols: 1
        Camera:
            id: camera
            resolution: (800, 800)
        Button:
            text: 'OCR!'
            size_hint_y: None
            height: '48dp'
            on_press:
                root.capture()
                # root refers to <CameraScreen>
                # app refers to TestCamera, app.root refers to the GridLayout: at the top
                app.root.ids['screen_manager'].transition.direction = 'left'
                app.root.ids['screen_manager'].current = 'text_screen'

<TextScreen>:
    Label:
        id: ocr_output
        text:
            Camerascreen.ocrtext
        font_size: 20
    Button:
        text: "Do OCR Again"
        size_hint_y: None
        height: '48dp'
        font_size: 50
        on_press:
            app.root.ids['screen_manager'].transition.direction = 'right'
            app.root.ids['screen_manager'].current = 'camera_screen'
            
""")


class MyOCRApp(App):

    def build(self):
        return GUI

if __name__ == "__main__":
    MyOCRApp().run()

这是将文本从一个类传递到另一个类以及TextInput数据的基本代码。 这是python代码:

from kivy.app import App
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.button import Button
from kivy.uix.textinput import TextInput
from kivy.uix.label import Label
from kivy.uix.screenmanager import ScreenManager, Screen, SlideTransition

class FirstPage(FloatLayout):

    def switch_screen(self):
        secondpage = self.login_name = self.ids.login_name.text
        myapp.secondpage.update_name1(secondpage)
        secondpage = self.password_name = self.ids.password_name.text
        myapp.secondpage.update_name2(secondpage)
        myapp.screen_manager.transition = SlideTransition(direction='left', duration=.25)
        myapp.screen_manager.current = 'SecondPage'

class SecondPage(FloatLayout):

    def switch_back(self):
        myapp.screen_manager.transition = SlideTransition(direction='right', duration=.25)
        myapp.screen_manager.current = 'FirstPage'

    def update_name1(self, name_login):
        self.ids.name_login.text = (f'{name_login}:')

    def update_name2(self, name_password):
        self.ids.name_password.text = (f'{name_password}:')

class MyApp(App):

    def build(self):
        self.screen_manager = ScreenManager()

        self.firstpage = FirstPage()
        screen = Screen(name='FirstPage')
        screen.add_widget(self.firstpage)
        self.screen_manager.add_widget(screen)

        self.secondpage = SecondPage()
        screen = Screen(name='SecondPage')
        screen.add_widget(self.secondpage)
        self.screen_manager.add_widget(screen)

        return self.screen_manager

myapp = MyApp()
myapp.run()

这是kivy代码:

FirstPage:

<FirstPage>:

    TextInput:
        id: login_name
        size_hint: .65, .08
        multiline: False
        font_size: 20
        pos_hint: {'x': .2, 'y': .57}

    TextInput:
        id: password_name
        size_hint: .65, .08
        multiline: False
        font_size: 20
        pos_hint: {'x': .2, 'y': .47}

    Button:
        text: "First"
        size_hint: .1, .1
        on_release: root.switch_screen()

<SecondPage>:

    Button:
        text: "Second"
        size_hint: .1, .1
        on_release: root.switch_back()

    Label:
        id: name_login
        text: "Login"
        size_hint: .2, .2
        pos_hint: {'x': .2, 'y': .57}

    Label:
        id: name_password
        text: "Password"
        size_hint: .2, .2
        pos_hint: {'x': .2, 'y': .47}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM