ktkd commited on
Commit
c44cdf2
·
verified ·
1 Parent(s): 9761ff2

添加重要文件

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