import cv2
import numpy as np

# 用于存储点击的坐标
click_coordinates = []

# 鼠标点击事件回调函数
def click_event(event, x, y, flags, param):
    if event == cv2.EVENT_LBUTTONDOWN:
        # 将点击的坐标加入到列表
        click_coordinates.append((x, y))
        print(f"({x}, {y})")

cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)

if not cap.isOpened():
    print("Error: Could not open video stream.")
    exit()

cv2.namedWindow("frame")
cv2.setMouseCallback("frame", click_event)

while(True):
    ret, frame = cap.read()

    if not ret:
        print("Error: Failed to capture frame.")
        break
    
    # 遍历所有保存的坐标，绘制坐标
    for coord in click_coordinates:
        x, y = coord
        font = cv2.FONT_HERSHEY_SIMPLEX
        text = f"({x}, {y})"
        # 在点击位置绘制坐标
        cv2.putText(frame, text, (x + 10, y + 10), font, 0.5, (255, 0, 0), 2)
        # 在点击位置画一个小圆圈作为标记
        cv2.circle(frame, (x, y), 5, (0, 0, 255), -1)

    # 显示视频帧
    cv2.imshow('frame', frame)
    
    # 按 'q' 键退出
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
