pythonopencvreal-timepython-mss

OpenCV imshow function not responding


I try to detect color on the screen in real time and when I'm using imshow function it is not responding and I can't see my screen live Someone can help?

import numpy as np
from PIL import Image
from mss import mss
import cv2 as cv
import pyautogui
from pynput.keyboard import Key, Controller
import time
import sys
import keyboard as detectClick

topPixels = 880
leftPixels = 200

keyboard = Controller()
mon = {'top': topPixels, 'left': leftPixels, 'width': 1000, 'height': 2}
sct = mss()

while 1:
    # Take each
    sct.get_pixels(mon)
    img = Image.frombytes('RGB', (sct.width, sct.height), sct.image)
    img = np.array(img)

    # Convert RGB to HSV
    hsv = cv.cvtColor(img, cv.COLOR_RGB2HSV)
    # define range of gray color in HSV

    lower_gray = np.array([16, 100, 20])
    upper_gray = np.array([25, 255, 255])

    # Threshold the HSV image to get only gray colors
    mask = cv.inRange(hsv, lower_gray, upper_gray)

    # Bitwise-AND mask and original image
    res = cv.bitwise_and(img, img, mask=mask)

    coord = cv.findNonZero(mask)
    if coord is None:
        print("No coords")
    else:
        x = leftPixels + coord[0, 0].item(0)
        y = topPixels + coord[0, 0].item(1)
        pyautogui.moveTo(x, y)
        pyautogui.dragTo(x, y - 140, button='left')
        time.sleep(1.5)
        
    cv.imshow('original', img)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
cv.destroyAllWindows()

Note: I'm using mss version 2.0.22 because I have to use it for the OpenCV. If there is another way to detect color in real-time on video I will be glad to hear


Solution

  • Use this:

    ...
    cv.imshow('original', img)
    cv.imshow('mask', mask)
    cv.imshow('res', res)
    cv2.waitkey(0)
    ...