双目测距项目
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

45 lines
1.2KB

  1. import cv2
  2. import time
  3. # AUTO = True # 自动拍照,或手动按s键拍照
  4. AUTO = False
  5. INTERVAL = 2 # 自动拍照间隔
  6. camera = cv2.VideoCapture(1) # 设置分辨率左右摄像机同一频率,同一设备ID;左右摄像机总分辨率2560x720;分割为两个1280x720
  7. camera.set(cv2.CAP_PROP_FRAME_WIDTH, 2560)
  8. camera.set(cv2.CAP_PROP_FRAME_HEIGHT, 720)
  9. counter = 0
  10. utc = time.time()
  11. folder = "./takePhoto/" # 拍照文件目录
  12. def shot(pos, frame):
  13. global counter
  14. path = folder + pos + "_" + str(counter) + ".jpg"
  15. cv2.imwrite(path, frame)
  16. print("snapshot saved into: " + path)
  17. while camera.isOpened():
  18. ret, frame = camera.read()
  19. left_frame = frame[0:720, 0:1280]
  20. right_frame = frame[0:720, 1280:2560]
  21. cv2.imshow("left", left_frame)
  22. cv2.imshow("right", right_frame)
  23. now = time.time()
  24. if AUTO and now - utc >= INTERVAL:
  25. shot("left", left_frame)
  26. shot("right", right_frame)
  27. counter += 1
  28. utc = now
  29. key = cv2.waitKey(1)
  30. if key == ord("q"):
  31. break
  32. elif key == ord("s"):
  33. shot("left", left_frame)
  34. shot("right", right_frame)
  35. counter += 1
  36. camera.release()
  37. cv2.destroyWindow("left")
  38. cv2.destroyWindow("right")