简体   繁体   中英

How to capture and save images from two USB cameras connected to a Raspberry Pi 3 simultaneously using OpenCV?

I'm looking to use a pair of top-bottom pair of stereo images taken by two USB cameras connected to a Raspberry Pi 3 for use on a drone. How do I save the images taken simultaneously in a folder on the pi? Here's what I did to get both the cameras working on two separate windows:

import cv2

frame0 = cv2.VideoCapture(1)
frame1 = cv2.VideoCapture(3)
while 1:

   ret0, img0 = frame0.read()
   ret1, img00 = frame1.read()
   img1 = cv2.resize(img0,(1280,720))                          #360,240
   img2 = cv2.resize(img00,(1280,720))
   if (frame0):
       cv2.imshow('img1',img1)
   if (frame1):
       cv2.imshow('img2',img2)

   k = cv2.waitKey(30) & 0xff
   if k == 27:
      break

frame0.release()
frame1.release()
cv2.destroyAllWindows()

Set of two images are saved every 7 seconds in the same directory as the .py file.

import cv2
import time

frames = 100
interval = 7

frame0 = cv2.VideoCapture(1)
frame0.release()
frame0 = cv2.VideoCapture(1)
frame1 = cv2.VideoCapture(3)
frame1.release()
frame1 = cv2.VideoCapture(3)
for i in range(frames):

   ret0, img0 = frame0.read()
   ret1, img00 = frame1.read()
   img1 = cv2.resize(img0,(1280,720))                          #360,240
   img2 = cv2.resize(img00,(1280,720))
   if (frame0):
       cv2.imwrite('./img_'+str(i).zfill(4)+'.jpg',img1)
   if (frame1):
       cv2.imwrite('./img2_'+str(i).zfill(4)+'.jpg',img2)

   time.sleep(interval)
   #k = cv2.waitKey(30) & 0xff
   #if k == 27:
    #  break

frame0.release()
frame1.release()
cv2.destroyAllWindows()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM