79529862

Date: 2025-03-23 23:32:32
Score: 1
Natty:
Report link

thank you for the help, I really appreciate it. I actually ended up fixing the bug, but I thank you all for your help, I will post my fixed version below. However I did have a follow up question, it only detects yellow, and blue/purple, nothing else. These are the two colors I'm using, [0,255,255] and [255,0,0], am I missing something to have it not detect more colors? Also do yall have any recommendations on having me input a color into the console, for example typing something like yellow, and then having the computer search for that color on screen. I'm sorry if this seems like a bulky question, I bit off more than I could chew a little with this one.

import numpy as np
import cv2

def get_limits(color):
    c = np.uint8([[color]])
    hsvC = cv2.cvtColor(c, cv2.COLOR_BGR2HSV)

    hue = hsvC[0][0][0]

    lowerb = np.array([hue - 10, 100, 100], dtype=np.uint8)
    upperb = np.array([hue + 10, 255, 255], dtype=np.uint8)

    return lowerb, upperb

# The detect_black function
def detect_black(frame):
    hsv_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lower_black = np.array([0, 0, 0])
    upper_black = np.array([180, 255, 50])

    black_mask = cv2.inRange(hsv_frame, lower_black, upper_black)
    return black_mask
import cv2
import numpy as np
from PIL import Image

from util import get_limits

#Yellow [0,255,255]
#Blue/Purple [255,0,0]

yellow = [255, 0, 0]  # Yellow in RGB colorspace
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()

    hsvImage = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)

    lowerb, upperb = get_limits(color=yellow)

    mask = cv2.inRange(hsvImage, lowerb, upperb)

    mask_ = Image.fromarray(mask)

    bbox = mask_.getbbox()

    if bbox is not None:
        x1, y1, x2, y2 = bbox

        frame = cv2.rectangle(frame,(x1,y1),(x2,y2), (0,255,0), 5)

    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break



cap.release()
cv2.destroyAllWindows()
Reasons:
  • Blacklisted phrase (0.5): thank you
  • Long answer (-1):
  • Has code block (-0.5):
  • Contains question mark (0.5):
  • Self-answer (0.5):
  • Low reputation (1):
Posted by: Daniel Giordano