Cv2.error: OpenCV(4.11.0) 👎 error: (-5:Bad argument) in function 'inRange' > Overload resolution failed: > - lowerb is not a numpy array, neither a scalar > - Expected Ptr<cv::UMat> for argument 'lowerb'

import cv2
import numpy as np

def detect_bucket(frame, lower_color, upper_color):
    # Convert frame to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    # Create mask for the given color range
    mask = cv2.inRange(hsv, lower_color, upper_color)
    
    # Find contours from the mask
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    if contours:
        # Get the largest contour (assumed to be the bucket)
        largest_contour = max(contours, key=cv2.contourArea)
        
        # Get bounding box for the largest contour
        x, y, w, h = cv2.boundingRect(largest_contour)
        return x, y, w, h, mask
    
    return None, None, None, None, mask

def get_movement_instructions(x, y, w, h, frame_width, frame_height):
    center_x, center_y = x + w // 2, y + h // 2
    frame_center_x, frame_center_y = frame_width // 2, frame_height // 2
    
    move_x, move_y = "", ""
    
    if center_x < frame_center_x - 20:
        move_x = "Move Left"
    elif center_x > frame_center_x + 20:
        move_x = "Move Right"
    
    if center_y < frame_center_y - 20:
        move_y = "Move Up"
    elif center_y > frame_center_y + 20:
        move_y = "Move Down"
    
    if not move_x and not move_y:
        return "Centered, Now Move Closer"
    
    return f"{move_x} {move_y}".strip()

def main():
    cap = cv2.VideoCapture(0)  # Open camera
    
    # Define color range for orange (adjust if needed)
    lower_orange = np.array([5, 100, 100])
    upper_orange = np.array([15, 255, 255])
    
    while cap.isOpened():
        ret, frame = cap.read()

        if frame is None:
            print("Error: Frame not captured. Check camera permissions or device index.")
            continue


        if not ret:
            break
        

        frame_height, frame_width, _ = frame.shape
        x, y, w, h, mask = detect_bucket(frame, lower_orange, upper_orange)
        
        if x is not None:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            instruction = get_movement_instructions(x, y, w, h, frame_width, frame_height)
            cv2.putText(frame, instruction, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        else:
            cv2.putText(frame, "Bucket Not Found", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        
        cv2.imshow("Frame", frame)
        cv2.imshow("Mask", mask)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

I am working on a centering algorithm using OpenCV where it detects anything that is orange and gives direction of where to move the camera to center it to the orange landing pad. This is done ultimately for a precise landing of the drone. I made a python OpenCV to see if the code is working or not and later to convert it into a ROS node for the drone. When I run this script below on Windows, it works properly. However, when I run the same code in Ubuntu, I get an error. The code is below:

import cv2
import numpy as np

def detect_bucket(frame, lower_color, upper_color):
    # Convert frame to HSV
    hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
    
    # Create mask for the given color range
    mask = cv2.inRange(hsv, lower_color, upper_color)
    
    # Find contours from the mask
    contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
    
    if contours:
        # Get the largest contour (assumed to be the bucket)
        largest_contour = max(contours, key=cv2.contourArea)
        
        # Get bounding box for the largest contour
        x, y, w, h = cv2.boundingRect(largest_contour)
        return x, y, w, h, mask
    
    return None, None, None, None, mask

def get_movement_instructions(x, y, w, h, frame_width, frame_height):
    center_x, center_y = x + w // 2, y + h // 2
    frame_center_x, frame_center_y = frame_width // 2, frame_height // 2
    
    move_x, move_y = "", ""
    
    if center_x < frame_center_x - 20:
        move_x = "Move Left"
    elif center_x > frame_center_x + 20:
        move_x = "Move Right"
    
    if center_y < frame_center_y - 20:
        move_y = "Move Up"
    elif center_y > frame_center_y + 20:
        move_y = "Move Down"
    
    if not move_x and not move_y:
        return "Centered, Now Move Closer"
    
    return f"{move_x} {move_y}".strip()

def main():
    cap = cv2.VideoCapture(0)  # Open camera
    
    # Define color range for orange (adjust if needed)
    lower_orange = np.array([5, 100, 100])
    upper_orange = np.array([15, 255, 255])
    
    while cap.isOpened():
        ret, frame = cap.read()

        if frame is None:
            print("Error: Frame not captured. Check camera permissions or device index.")
            continue


        if not ret:
            break
        

        frame_height, frame_width, _ = frame.shape
        x, y, w, h, mask = detect_bucket(frame, lower_orange, upper_orange)
        
        if x is not None:
            cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
            instruction = get_movement_instructions(x, y, w, h, frame_width, frame_height)
            cv2.putText(frame, instruction, (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
        else:
            cv2.putText(frame, "Bucket Not Found", (50, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2)
        
        cv2.imshow("Frame", frame)
        cv2.imshow("Mask", mask)
        
        if cv2.waitKey(1) & 0xFF == ord('q'):
            break
    
    cap.release()
    cv2.destroyAllWindows()

if __name__ == "__main__":
    main()

I get this error when I run it:

/bin/python3 /home/keshav/Desktop/detectOrangeBucket.py
Traceback (most recent call last):
  File "/home/keshav/Desktop/detectOrangeBucket.py", line 84, in <module>
    main()
  File "/home/keshav/Desktop/detectOrangeBucket.py", line 65, in main
    x, y, w, h, mask = detect_bucket(frame, lower_orange, upper_orange)
  File "/home/keshav/Desktop/detectOrangeBucket.py", line 9, in detect_bucket
    mask = cv2.inRange(hsv, lower_color, upper_color)
cv2.error: OpenCV(4.11.0) :-1: error: (-5:Bad argument) in function 'inRange'
> Overload resolution failed:
>  - lowerb is not a numpy array, neither a scalar
>  - Expected Ptr<cv::UMat> for argument 'lowerb'

I am not sure how to fix this. Can someone please help me debug this?