File size: 4,536 Bytes
c44cdf2 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
import cv2
import argparse
import time
def scan_available_cameras(max_ports=10):
"""扫描系统中可用的摄像头端口"""
available_ports = []
for i in range(max_ports):
cap = cv2.VideoCapture(i)
if not cap.isOpened():
cap.release()
continue
# 尝试读取一帧以确认摄像头是否真的可用
ret, frame = cap.read()
if ret:
available_ports.append(i)
cap.release()
return available_ports
def get_camera_info(camera_index):
"""获取摄像头的详细信息,包括分辨率和设备名称"""
cap = cv2.VideoCapture(camera_index)
cap.set(cv2.CAP_PROP_FRAME_WIDTH,1920)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT,1080)
if not cap.isOpened():
return {"status": "error", "message": f"无法打开摄像头 {camera_index}"}
try:
# 获取基本信息
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
fps = cap.get(cv2.CAP_PROP_FPS)
# 尝试获取设备名称(依赖于操作系统和驱动)
device_name = f"摄像头 {camera_index}"
try:
# Linux系统可能通过V4L2获取设备名称
import subprocess
result = subprocess.run(
f"v4l2-ctl -d /dev/video{camera_index} --all | grep 'Name'",
shell=True,
capture_output=True,
text=True
)
if result.stdout:
device_name = result.stdout.strip().split(':')[1].strip()
except:
pass
info = {
"status": "success",
"index": camera_index,
"device_name": device_name,
"resolution": f"{width}x{height}",
"fps": fps
}
except Exception as e:
info = {"status": "error", "message": str(e)}
while True:
# 读取一帧图像
ret, frame = cap.read()
# 检查是否成功读取图像
if not ret:
print("无法获取帧")
break
# 显示图像
cv2.imshow("", frame)
#cv2.imwrite("debug.png", frame)
# 等待按键事件,超时时间为1毫秒
key = cv2.waitKey(30)
# 按ESC键退出循环(ESC键的ASCII码为27)
if key == 27:
print("用户请求退出...")
break
cap.release()
return info
def main():
parser = argparse.ArgumentParser(description='扫描并检测摄像头分辨率')
parser.add_argument('--scan', action='store_true', help='扫描所有可用摄像头')
parser.add_argument('--index', type=int, help='指定要检测的摄像头索引')
args = parser.parse_args()
if args.scan:
print("正在扫描系统中可用的摄像头端口...")
available_cameras = scan_available_cameras()
if not available_cameras:
print("未检测到可用的摄像头")
return
print("\n可用摄像头列表:")
for i, cam_idx in enumerate(available_cameras):
info = get_camera_info(cam_idx)
if info["status"] == "success":
print(f"{i+1}. 端口: {cam_idx}, 设备: {info['device_name']}, 分辨率: {info['resolution']}")
else:
print(f"{i+1}. 端口: {cam_idx} (无法获取详细信息)")
print("\n使用方法:")
print(f"python {__file__} --index <端口号> # 检测指定摄像头的详细信息")
elif args.index is not None:
print(f"正在检测摄像头 {args.index} 的详细信息...")
info = get_camera_info(args.index)
if info["status"] == "success":
print("\n摄像头信息:")
print(f" 端口号: {info['index']}")
print(f" 设备名称: {info['device_name']}")
print(f" 分辨率: {info['resolution']}")
print(f" 帧率: {info['fps']:.2f} FPS")
else:
print(f"错误: {info['message']}")
else:
parser.print_help()
if __name__ == "__main__":
main() |