vlm_agent/camera_check.py

49 lines
1.2 KiB
Python
Raw Normal View History

2024-12-02 11:43:41 +08:00
import cv2
import numpy as np
2025-05-18 20:56:32 +08:00
# 用于存储点击的坐标
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)
2024-12-02 11:43:41 +08:00
while(True):
ret, frame = cap.read()
2025-05-18 20:56:32 +08:00
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)
2024-12-02 11:43:41 +08:00
2025-05-18 20:56:32 +08:00
# 显示视频帧
2024-12-02 11:43:41 +08:00
cv2.imshow('frame', frame)
2025-05-18 20:56:32 +08:00
# 按 'q' 键退出
2024-12-02 11:43:41 +08:00
if cv2.waitKey(1) & 0xFF == ord('q'):
break
cap.release()
2025-05-18 20:56:32 +08:00
cv2.destroyAllWindows()