Screen option not working

On Ubuntu 24.10: (PyCharm 2024.3 Community Edition)
Code snippet:

model_name = "yolo11n"      # pretrained Ultralytics model for YOLO11, nano, COCO dataset
model = YOLO(f"{model_name}.pt")    # the nano model by Ultralytics
image_name = "screen"       # current screen content is the input source for inference
results = model(f"{image_name}", stream=True)   # using a parameter driven value for input source


for result in results:

Error message:

Traceback (most recent call last):
  File "/home/reza/PycharmProjects/yolo11/predict/screen_predict.py", line 60, in <module>
    for result in results:
                  ^^^^^^^
  File "/home/reza/PycharmProjects/yolo11/.venv/lib/python3.12/site-packages/torch/utils/_contextlib.py", line 36, in generator_context
    response = gen.send(None)
               ^^^^^^^^^^^^^^
  File "/home/reza/PycharmProjects/yolo11/.venv/lib/python3.12/site-packages/ultralytics/engine/predictor.py", line 245, in stream_inference
    for self.batch in self.dataset:
                      ^^^^^^^^^^^^
  File "/home/reza/PycharmProjects/yolo11/.venv/lib/python3.12/site-packages/ultralytics/data/loaders.py", line 277, in __next__
    im0 = np.asarray(self.sct.grab(self.monitor))[:, :, :3]  # BGRA to BGR
                     ^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/reza/PycharmProjects/yolo11/.venv/lib/python3.12/site-packages/mss/base.py", line 101, in grab
    screenshot = self._grab_impl(monitor)
                 ^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/reza/PycharmProjects/yolo11/.venv/lib/python3.12/site-packages/mss/linux.py", line 428, in _grab_impl
    ximage = self.xlib.XGetImage(
             ^^^^^^^^^^^^^^^^^^^^
  File "/home/reza/PycharmProjects/yolo11/.venv/lib/python3.12/site-packages/mss/linux.py", line 230, in _validate
    raise ScreenShotError(msg, details=details)
mss.exception.ScreenShotError: XGetImage() failed

Process finished with exit code 1

Would appreciate some guidance, please. Thanks.

Regards.

Hello!

It seems like you’re encountering a ScreenShotError which indicates an issue with capturing the screen content. This error is commonly related to the mss library’s interaction with the X server. Here are a few steps you can take to resolve this issue:

  1. Check X Server Configuration: Ensure that the X server is properly configured and running. The mss library relies on the X server to capture screen content. You might want to verify your X server settings or restart it.

  2. Run PyCharm with Proper Permissions: Sometimes, running PyCharm with elevated permissions can resolve permission-related issues with screen capturing. Try running PyCharm as a superuser and see if that resolves the issue.

  3. Ensure mss is Up-to-Date: Make sure you have the latest version of the mss library, as updates may include bug fixes or improvements for compatibility with newer systems.

  4. Test with a Simple Script: Create a simple script to test screen capturing using mss directly, outside of the YOLO11 setup, to isolate the issue. Here’s a basic example:

    import mss
    import numpy as np
    
    with mss.mss() as sct:
        monitor = sct.monitors[1]  # Capture the first monitor
        img = np.asarray(sct.grab(monitor))
        print("Screen captured successfully!")
    

    If this script also fails, the issue is likely with the screen capture setup.

  5. Resources and Support: For further assistance, you can check the mss GitHub repository for similar issues or raise a discussion there. Additionally, engaging with the Ultralytics community on GitHub or Discord can provide more insights.

Hopefully, these steps help you resolve the issue. If you have any other questions or need further assistance, feel free to reach out! :blush:

Best of luck!

You can try switching to X11