Small OpenCV notebook for detecting a target color from a webcam stream.
- Python 3.9+
numpy,opencv-python,Pillow
Install:
pip install numpy opencv-python Pillow- Open
ColorDetection.ipynb. - Run the first cell to define
get_limits(). - Run the webcam cell.
- Press
qor close the window to exit. - Change the color if wanted, the format is BGR
- On macOS,
cv2.CAP_AVFOUNDATIONis used for stability. - On Windows or Linux, you can switch to
cv2.VideoCapture(0)or a platform-specific backend (e.g.,CAP_DSHOWorCAP_V4L2).
get_limits()now handles hue wrap-around (needed for red) and supports tuninghue_tol,sat_min, andval_min.- The webcam loop combines multiple HSV ranges (for red) and applies a light morphology + contour area filter to reduce false positives.