Ниже код решение для моей проблемы:
cap = cv2.VideoCapture('path')
while(True):
_, frame = cap.read()
hsv = cv2.cvtColor(frame, cv2.COLOR_BGR2HSV)
black1 = np.array([0, 0, 0], np.uint8)
black2 = np.array([45,35,35], np.uint8)
red1 = np.array([170, 70, 50], np.uint8)
red2 = np.array([180, 255, 255], np.uint8)
red_mask = cv2.inRange(hsv, red1, red2)
black_mask = cv2.inRange(hsv, black1, black2)
kernel = np.ones((7, 7), "uint8")
black_mask = cv2.dilate(black_mask, kernel)
red_mask = cv2.dilate(red_mask, kernel)
res_red = cv2.bitwise_and(frame, frame, mask = red_mask)
res_black = cv2.bitwise_and(frame, frame, mask = black_mask)
cnts, _ = cv2.findContours(black_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
contours, _ = cv2.findContours(red_mask, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
for cont in contours:
x1,y1,w1,h1 = cv2.boundingRect(cont)
area_red = cv2.contourArea(cont)
if (1000 > area_red >200):
cv2.rectangle(frame, (x1,y1), (x1+w1, y1+h1), (255,0,0, 2))
for c in cnts:
x,y,w,h = cv2.boundingRect(c)
approx = cv2.approxPolyDP(c, 0.04 * cv2.arcLength(c, True), True)
area = cv2.contourArea(c)
if (1000 > area > 250):
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
cv2.imshow("Video from camera", frame)
if cv2.waitKey(10) & 0xFF == ord('q'):
break