Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AttributeError: '_thread._local' object has no attribute 'srcdc' #273

Open
Hecto-r opened this issue Feb 3, 2024 · 3 comments
Open

AttributeError: '_thread._local' object has no attribute 'srcdc' #273

Hecto-r opened this issue Feb 3, 2024 · 3 comments

Comments

@Hecto-r
Copy link

Hecto-r commented Feb 3, 2024

MSS Version: 9.0.1
Python Version: 3.10.11
Windows 10 22H2

Exception in thread Thread-1 (update):
Traceback (most recent call last):
File "C:threading.py", line 1016, in _bootstrap_inner
self.run()
File "C:threading.py", line 953, in run
self._target(*self._args, **self._kwargs)
File "d:PlayerHealth.py", line 29, in update
img = np.array(self.ms.grab(self.healthBox))
File "C:mss\base.py", line 90, in grab
screenshot = self._grab_impl(monitor)
File "C:mss\windows.py", line 235, in _grab_impl
srcdc, memdc = self._handles.srcdc, self._handles.memdc
AttributeError: '_thread._local' object has no attribute 'srcdc'

class PlayerHealth:

    def init(self):
        # Initialize variables
        self.ms = mss.mss()
        self.healthBox = {'top': 45, 'left': 100, 'width': 100, 'height': 20}
        self.healthPercentage = 100

    def start(self):
        # Start the thread that reads frames from the video stream
        Thread(target=self.update,args=()).start()
        return self

    def update(self):
        # Keep looping indefinitely until the thread is stopped
        while True:
            time.sleep(1)
            # Otherwise, grab the next frame from the stream
            img = Image.frombytes('RGB', (100, 20), self.ms.grab(self.healthBox).rgb)
            screen = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)

            # Save so we can see what we grabbed - Debug purpose only
            cv2.imwrite("health.png",screen)
class main:
    def testObject():
      aPlayerHealth = PlayerHealth().start()
      while True:
          time.sleep(5)
          print(aPlayerHealth.read())
@Hecto-r
Copy link
Author

Hecto-r commented Feb 3, 2024

well fixed the issue by changing the following if anyone else runs into this:

def update(self):
        # Keep looping indefinitely until the thread is stopped
        while True:
            time.sleep(1)
            with mss.mss() as sct:
                # Otherwise, grab the next frame from the stream
                img = Image.frombytes('RGB', (100, 20), sct.grab(self.healthBox).rgb)
                screen = cv2.cvtColor(np.array(img), cv2.COLOR_BGR2GRAY)

@lrq3000
Copy link

lrq3000 commented Feb 4, 2024

I have the same error happening now, it never happened before. I have no idea of the cause, but it's totally breaking my app.

Your solution is disadvised in the documentation, with mss.mss() as sct: should be out of the loop to avoid instanciating non stop the same object.

But it does fix the issue. So the issue is indeed that for some reason when instanciating mss.mss() inside a thread it now cannot be called outside.

@Hecto-r
Copy link
Author

Hecto-r commented Feb 4, 2024

Your solution is disadvised in the documentation, with mss.mss() as sct: should be out of the loop to avoid instanciating non stop the same object.

Yeah I’ll leave the issue open to try and get a real fix. The other solution I’ve seen is downgrading to a version that works (possibly v6. I didn’t give that a try though)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants