KivyMD App Error : build self.image = Image() TypeError: 'module' object is not callable

Viewed 136

I'm trying to build a text scanner app using pytesseract, opencv, and KivyMD as this tutorial: https://www.youtube.com/watch?v=xIYbgCvFfdQ&list=PL0lYY7rL__yLMEI7k9hA8L1EOVlH79FyY&index=2

But my result is empty window and errors. Please help

This is my code 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()

Here are errors:

 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

1 Answers

This is due to the fact that the image classes in the PIL and kivy libraries are called the same - Image, you just need to change the class name this way from PIL import Image as PILImage

Related