import cv2 #two video capture = cv2.VideoCapture(0) capture_usb = cv2.VideoCapture(2)
打开自带的摄像头
if capture.isOpened(): if capture_usb.isOpened(): # 以下设置显示屏的宽高 capture.set(cv2.CAP_PROP_FRAME_WIDTH, 640) capture.set(cv2.CAP_PROP_FRAME_HEIGHT, 480) capture_usb.set(cv2.CAP_PROP_FRAME_WIDTH, 640) capture_usb.set(cv2.CAP_PROP_FRAME_HEIGHT, 480)
持续读取摄像头数据
while True: read_code, frame = capture.read() read_code2, frame2 = capture_usb.read() if not read_code or not read_code2: break cv2.imshow(“screen_title”, frame) cv2.imshow(“screen_title_usb”, frame2) # 输入 q 键,保存当前画面为图片 if cv2.waitKey(1) == ord(‘q’): # 设置图片分辨率 frame = cv2.resize(frame, (1920, 1080)) cv2.imwrite(‘pic.jpg’, frame) capture_usb.release() break
释放资源
capture.release() #cv2.destroyWindow(“screen_title”) #注意在台式机后侧两个USB口是可以的,前面的usb口不可以,和硬件端口有关系。
|