CV2 imshow function not responding

Question:

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 cv2.
If there is another way to detect color in realtime on video I will be glad to hear

Asked By: Aviel Ovadiya

||

Answers:

Use this:

...
cv.imshow('original', img)
cv.imshow('mask', mask)
cv.imshow('res', res)
cv2.waitkey(0)
...
Answered By: Salahuddin
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.