Can't seem to properly run python MSS library

927 Views Asked by At

I am trying to learn the MSS python library to stream/reflect what's in the monitor. My intention is to use it with OpenCV to process the image. I got this source code from a youtube tutorial but I am getting the this error: (I can't solve this to run the code). Any tips and guidance are very much appreciated.

Traceback (most recent call last):
  File "C:\Users\Ryan Webb\Documents\OpenCV-objectDetection\mss.py", line 4, in <module>
    from mss.windows import MSS as mss
  File "C:\Users\Ryan Webb\Documents\OpenCV-objectDetection\mss.py", line 4, in <module>
    from mss.windows import MSS as mss
ModuleNotFoundError: No module named 'mss.windows'; 'mss' is not a package

Here is the source code I copy-pasted from the tutorial:

from mss import mss
import cv2
from PIL import Image
import numpy as np
from time import time

mon = {'top': 100, 'left':200, 'width':1600, 'height':1024}

sct = mss()

while 1:
    begin_time = time()
    sct_img = sct.grab(mon)
    img = Image.frombytes('RGB', (sct_img.size.width, sct_img.size.height), sct_img.rgb)
    img_bgr = cv2.cvtColor(np.array(img), cv2.COLOR_RGB2BGR)
    cv2.imshow('test', np.array(img_bgr))
    print('This frame takes {} seconds.'.format(time()-begin_time))
    if cv2.waitKey(25) & 0xFF == ord('q'):
        cv2.destroyAllWindows()
        break

I think I properly installed the MSS library here is my pip list results:

C:\Users\Ryan Webb>pip list
Package           Version
----------------- --------
astroid           2.6.5
colorama          0.4.4
isort             5.9.3
lazy-object-proxy 1.6.0
mccabe            0.6.1
MouseInfo         0.1.3
mss               6.1.0
numpy             1.21.1
opencv-python     4.5.3.56
Pillow            8.3.1
pip               21.1.3
PyAutoGUI         0.9.53
PyGetWindow       0.0.9
pylint            2.9.6
PyMsgBox          1.0.9
pyperclip         1.8.2
PyRect            0.1.4
PyScreeze         0.1.27
PyTweening        1.0.3
pywin32           301
setuptools        56.0.0
toml              0.10.2
wrapt             1.12.1
WARNING: You are using pip version 21.1.3; however, version 21.2.3 is available.
You should consider upgrading via the 'c:\program files\python39\python.exe -m pip install --upgrade pip' command.
1

There are 1 best solutions below

0
On

My mistake is naming my script mss.py causing issues with the runtime as pointed out by friend Ghoti in the comments. I don't know how to mark my question as answered so I want to say it here.