supervision icon indicating copy to clipboard operation
supervision copied to clipboard

Detect objects color

Open sceddd opened this issue 1 year ago • 3 comments

Search before asking

  • [X] I have searched the Supervision issues and found no similar feature requests.

Description

I’m building an intelligent traffic lighting system and see that color detection of an object may be necessary in many cases.

Use case

Ex: in the analysis of a store’s user group or in the classification of ripe fruits,

Additional

In case you're interested, I have a basic issue for detect traffic light color. Hope it helps:

def detect_color(frame:np.ndarray,xyxy:np.ndarray) -> str:
  """
  Detect traffic light's color

  Args:
  frame(np.ndarray): The image contain the detections to check it color
  xyxy (np.ndarray): An array of shape `(n, 4)` containing the bounding boxes coordinates in format `[x1, y1, x2, y2]`
  """
  cropped = crop_image(frame,xyxy)
  hsv_frame = cv2.cvtColor(cropped, cv2.COLOR_BGR2HSV)
 
  red_lower = np.array([0, 100, 100])
  red_upper = np.array([30, 255, 255])
  green_lower = np.array([40, 100, 100])
  green_upper = np.array([100, 255, 255])

  red_mask = cv2.inRange(hsv_frame, red_lower, red_upper)
  green_mask = cv2.inRange(hsv_frame, green_lower, green_upper)

  red_pixels = np.count_nonzero(red_mask)
  green_pixels = np.count_nonzero(green_mask)

  color_counts = {
      "Red": red_pixels,
      "Green": green_pixels,
  }

  color_counts_filtered = {color: count for color, count in color_counts.items() if count > 0}  #count pixels color 
  if len(color_counts_filtered)>0:
    return max(color_counts_filtered, key=color_counts_filtered.get)
  else: return None
def process_trafficLight(frame:np.ndarray,TF_detections:sv.Detections):
  """
    Detect traffic light color
    
    Args:
    TF_detections(Detections): Traffic light detections to check colors
  """
  colors = []
  for detect in TF_detections:
    if isinstance(detect, tuple):
      bbox = detect[0] 
    else:
      bbox = detect.bbox  
    colors.append(detect_color(frame, bbox))
  return max(colors,key=colors.count)

sceddd avatar Jun 09 '23 06:06 sceddd

Hi @sceddd 👋🏻 ! Looks quite specific. Do you have any idea how to make it more generic?

SkalskiP avatar Jun 09 '23 08:06 SkalskiP

Hi @SkalskiP, In my solution, I detect objects colors based on the number of pixels in ranges of upper and lower color dimension; maybe we can use the KNN algorithm or any classification for caculating Euclide distance between these pixels in the cropped detection image to detect which color fit with the detections.

sceddd avatar Jun 09 '23 10:06 sceddd

@sceddd Instead using a reinforcement technique would be more generic and accurate.

mahimairaja avatar Jul 03 '23 18:07 mahimairaja

Hi everyone! After deep consideration, I have decided we will not undertake this task. The role of supervision is to add easy-to-use blocks that allow for building a computer vision app. This task seems too specific, and for that reason, I am closing it.

SkalskiP avatar Jan 26 '24 09:01 SkalskiP