Python:
import cv2
import numpy as np
class DiHuskyLensReco(Skill):
def __init__(self):
super().__init__()
self._skill_lobe = 5
self.cap = cv2.VideoCapture(0)
self._object_registry = {} # object_hash -> unique_id
self._next_id = 1
self.detector = cv2.SimpleBlobDetector_create()
def input(self, ear: str, skin: str, eye: str):
ret, frame = self.cap.read()
if not ret:
return
# Convert to HSV for better color segmentation
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Detect colored objects (adjust ranges for your environment)
lower_red = np.array([0, 120, 70])
upper_red = np.array([10, 255, 255])
mask = cv2.inRange(hsv, lower_red, upper_red)
# Find contours of detected objects
contours, _ = cv2.findContours(mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
detected_ids = []
for contour in contours:
if cv2.contourArea(contour) > 300: # Filter small noise
x, y, w, h = cv2.boundingRect(contour)
roi = frame[y:y+h, x:x+w]
# Create unique signature for this object
obj_hash = self._generate_object_hash(roi)
# Assign or retrieve unique ID
if obj_hash not in self._object_registry:
self._object_registry[obj_hash] = self._next_id
self._next_id += 1
detected_ids.append(self._object_registry[obj_hash])
if detected_ids:
unique_objects = sorted(set(detected_ids))
self.setVerbatimAlg(3, f"Objects detected: {', '.join(map(str, unique_objects))}")
def _generate_object_hash(self, roi):
# Create a unique hash based on color histogram and shape features
gray = cv2.cvtColor(roi, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray], [0], None, [8], [0, 256])
hist_hash = hash(tuple(hist.flatten()))
return hist_hash
def __del__(self):
if hasattr(self, 'cap'):
self.cap.release()
def skillNotes(self, param: str) -> str:
if param == "notes":
return "Husky Lens-like object recognition with persistent unique ID assignment"
elif param == "triggers":
return "Continuous camera monitoring, assigns unique IDs to colored objects"
return "note unavailable"
Last edited: