OiO.lk English python Kivy camera freezes/disappears and keeps returning AttributeError upon application start
python

Kivy camera freezes/disappears and keeps returning AttributeError upon application start


I am currently making a project using Kivy that allows the user to take a picture that will then be converted into a string and translated. I am developing said project on Windows 10, using a python 3.11 anaconda-made environment and VSCode. However, when I tried to implement the OCR function using pytesseract, whenever I run the .py file at first the camera freezes but now it won’t even open the camera anymore. Also, the terminal returns these 2 errors continuously:

[ WARN:0@3.981] global cap_msmf.cpp:1769 CvCapture_MSMF::grabFrame videoio(MSMF): can't grab frame. Error: -1072873821
[ERROR  ] [OpenCV      ] Couldn't get image from Camera
Traceback (most recent call last):
  File "C:\Users\-\anaconda3\envs\kti\Lib\site-packages\kivy\core\camera\camera_opencv.py", line 144, in _update
    self._buffer = frame.imageData
                   ^^^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'imageData'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "C:\Users\-\anaconda3\envs\kti\Lib\site-packages\kivy\core\camera\camera_opencv.py", line 148, in _update
    self._buffer = frame.reshape(-1)
                   ^^^^^^^^^^^^^
AttributeError: 'NoneType' object has no attribute 'reshape'

My code uses the Kivy camera example as a base and it worked fine before I added pytesseract. Here is the relevant .py code:

pytesseract.pytesseract.tesseract_cmd = 'C:\\Program Files\\Tesseract-OCR\\tesseract.exe'
Builder.load_file('kti.kv')


class CameraScreen(Screen):
    def capture(self):
        camera = self.ids['camera']
        if os.path.exists("input.png"):
            os.remove("input.png")
        else:
            pass
        camera.export_to_png("input.png")
        print("Captured")
    def ocr(self):
        filename="input.png"
        img1 = np.array(Image.open(filename))
        global ocrOutput
        ocrOutput = pytesseract.image_to_string(img1, lang='jpn+eng')
        return ocrOutput

And also the relevant .kv code:

<CameraScreen>:
    orientation: 'vertical'
    name: "camera_screen"
    Camera:
        id: camera
        resolution: (640, 480)
        play: True
    Button:
        text: 'Capture'
        size_hint_y: None
        height: '48dp'
        on_press: 
            root.capture()
            root.manager.current="translate_screen"

I have a suspicion the problem lies in the cap_msmf error but as I am not using OpenCV directly, I have no idea how to change it through the Kivy camera function/class. Is there a possible way to change MSMF or perhaps another fix to this problem? Thanks in advance.



You need to sign in to view this answers

Exit mobile version