Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

Use this:

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

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading