import cv2
import tensorflow as tf
# Initialize webcam and create window to display output
cap = cv2.VideoCapture(0)
cv2.namedWindow("Output", cv2.WINDOW_NORMAL)
while True:
# Capture frame-by-frame
ret, frame = cap.read()
# Convert frame to HSV color space
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
# Define range of red color in HSV
lower_red = tf.constant([0, 100, 100], dtype=tf.float32)
upper_red = tf.constant([10, 255, 255], dtype=tf.float32)
# Create a mask for the red color
mask = tf.logical_and(hsv >= lower_red, hsv <= upper_red)
# Find contours in the mask
contours, _ = cv2.findContours(mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Draw circles around contours
for contour in contours:
(x, y), radius = cv2.minEnclosingCircle(contour)
center = (int(x), int(y))
radius = int(radius)
cv2.circle(frame, center, radius, (0, 0, 255), 2)
# Display output
cv2.imshow("Output", frame)
# Exit if 'q' is pressed
if cv2.waitKey(1) & 0xFF == ord('q'):
break
# Release webcam and close window
cap.release()
cv2.destroyAllWindows()