BoboTiG / python-mss

An ultra fast cross-platform multiple screenshots module in pure Python using ctypes.
https://pypi.org/project/mss/
MIT License
1.04k stars 94 forks source link

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

Open Hecto-r opened 9 months ago

Hecto-r commented 9 months ago

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())

Upvote & Fund

Fund with Polar

Hecto-r commented 9 months ago

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 commented 9 months ago

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 commented 9 months ago

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)