【问题标题】:KivyMD App Error : build self.image = Image() TypeError: 'module' object is not callableKivyMD App Error : build self.image = Image() TypeError: 'module' object is not callable
【发布时间】:2021-10-17 15:24:40
【问题描述】:

我正在尝试使用 pytesseract、opencv 和 KivyMD 作为本教程构建文本扫描仪应用程序:https://www.youtube.com/watch?v=xIYbgCvFfdQ&list=PL0lYY7rL__yLMEI7k9hA8L1EOVlH79FyY&index=2

但我的结果是空窗口和错误。请帮忙

这是我的代码 main.py

from kivymd.app import MDApp
from kivymd.uix.boxlayout import MDBoxLayout
from kivymd.uix.button import MDRaisedButton
from kivy.uix.image import Image
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import cv2
import pytesseract
from kivymd.uix.label import MDLabel
from PIL import Image

class MainApp(MDApp):

    def build(self):
        layout = MDBoxLayout(orientation='vertical')
        self.image = Image()
        self.label = MDLabel()
        layout.add_widget(self.image)
        layout.add_widget(self.label)
        self.save_img_button = MDRaisedButton(
            text="CLICK HERE",
            pos_hint={'center_x': .5, 'center_y': .5},
            size_hint=(None, None))
        self.save_img_button.bind(on_press=self.take_picture)
        layout.add_widget(self.save_img_button)
        self.capture = cv2.VideoCapture(1)
        Clock.schedule_interval(self.load_video, 1.0/30.0)
        return layout

    def load_video(self, *args):
        ret, frame = self.capture.read()
        # Frame initialize
        self.image_frame = frame
        buffer = cv2.flip(frame, 0).tostring()
        texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt='bgr')
        texture.blit_buffer(buffer, colorfmt='bgr', bufferfmt='ubyte')
        self.image.texture = texture

    def take_picture(self, *args):
        image_name = "picture_at_2_02.png"
        img = cv2.cvtColor(self.image_frame, cv2.COLOR_BGR2GRAY)
        img = cv2.GaussianBlur(img, (3, 3), 0)
        img = cv2.threshold(img, 0, 255, cv2.THRESH_BINARY_INV+cv2.THRESH_OTSU)[1]
        text_data = pytesseract.image_to_string(img, lang='eng', config="--oem 3 --psm 6")
        print(text_data)
        self.label.text = text_data
        cv2.imshow("cv2 final image", img)
        cv2.imwrite(image_name, self.image_frame)



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

以下是错误:

 Traceback (most recent call last):
   File "c:/Users/aroon/prac python/MyApp/main.py", line 53, in <module>
     MainApp().run()
   File "C:\Users\aroon\Anaconda3\lib\site-packages\kivy\app.py", line 949, in run
     self._run_prepare()
   File "C:\Users\aroon\Anaconda3\lib\site-packages\kivy\app.py", line 919, in _run_prepare
     root = self.build()
   File "c:/Users/aroon/prac python/MyApp/main.py", line 16, in build
     self.image = Image()
 TypeError: 'module' object is not callable

【问题讨论】:

    标签: python mobile kivy kivymd


    【解决方案1】:

    这是因为PILkivy库中的图像类被称为相同的事实-Image,您只需将类名更改为from PIL import Image as PILImage

    【讨论】:

      猜你喜欢
      • 2020-03-27
      • 2018-03-31
      • 2019-04-07
      • 1970-01-01
      • 1970-01-01
      • 2020-12-31
      • 1970-01-01
      • 1970-01-01
      • 2021-07-29
      相关资源
      最近更新 更多