Why does my cv2 slider keep starting with "None"

35 Views Asked by At

My CV2 slider keeps starting with the value "None", but I need it to be an int instandly. The error code I'm getting is this: TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'

The solution I tried is as follows:

def on_change(value):
    pass

windowname = 'settings'  

cv2.imshow(windowname, 0)
fovx = cv2.createTrackbar('fov x', windowname, int(640), int(1920), on_change)
fovy = cv2.createTrackbar('fov y', windowname, int(640), int(1080), on_change)

while True:
    with mss() as sct:
                
        dimensions = sct.monitors[1] 
        WINDOW_SIZEX = fovx
        WINDOW_SIZEY = fovy
        
        
        monitor = {"top": int((dimensions['height'] / 2) - (int(WINDOW_SIZEY) / 2)), "left": int((dimensions['width'] / 2) - (int(WINDOW_SIZEX) / 2)), "width": int(WINDOW_SIZEX), "height": int(WINDOW_SIZEY)}
1

There are 1 best solutions below

0
On

Your issue seems to be that your CV2 slider is starting with the value "None," but you need it to start as an integer instantly. The error message you're receiving is as follows: "TypeError: int() argument must be a string, a bytes-like object or a real number, not 'NoneType'."

The solution you've attempted involves creating trackbars using OpenCV's cv2.createTrackbar function, but it seems that the default value is initialized as "None," leading to the error. To resolve this and set the default value to an integer, you can modify your code as follows:

import cv2

# Set default values
default_fov_x = 640
default_fov_y = 640

def on_change(value):
    pass
windowname = 'settings'
# Create trackbars with default values using cv2.createTrackbar
fovx = cv2.createTrackbar('fov x', windowname, default_fov_x, 1920, on_change)
fovy = cv2.createTrackbar('fov y', windowname, default_fov_y, 1080, on_change)

while True:
    # Get values from the trackbars
    fov_x = cv2.getTrackbarPos('fov x', windowname)
    fov_y = cv2.getTrackbarPos('fov y', windowname)
    with mss() as sct:
        dimensions = sct.monitors[1]
        WINDOW_SIZEX = fov_x
        WINDOW_SIZEY = fov_y
        monitor = {
            "top": int((dimensions['height'] / 2) - (int(WINDOW_SIZEY) / 2)),
            "left": int((dimensions['width'] / 2) - (int(WINDOW_SIZEX) / 2)),
            "width": int(WINDOW_SIZEX),
            "height": int(WINDOW_SIZEY)
        }

In this code, when creating trackbars with cv2.createTrackbar, we use default_fov_x and default_fov_y as the default values to initialize the trackbars with integer values instead of "None." Then, we retrieve the values from the trackbars using cv2.getTrackbarPos.