import cv2
image = cv2.imread("Слайд9.PNG")
black = ((0, 0, 0), (180, 255, 100))
def find_contours(img, color):
img_hsv = cv2.cvtColor(img, cv2.COLOR_BGR2HSV)
img_mask = cv2.inRange(img_hsv, color[0], color[1])
contours, _ = cv2.findContours(img_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
return contours
def detect_arrow_direction(contours):
cnt = max(contours, key=cv2.contourArea)
M = cv2.moments(cnt)
if M["m00"] == 0:
return None
cx = int(M["m10"] / M["m00"])
cy = int(M["m01"] / M["m00"])
x, y, w, h = cv2.boundingRect(cnt)
box_center_x = x + w / 2
box_center_y = y + h / 2
if w > h:
if cx > box_center_x:
return "left"
else:
return "right"
else:
if cy > box_center_y:
return "up"
else:
return "down"
contours = find_contours(image, black)
print(detect_arrow_direction(contours))