double_async.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. __author__ = "Christian Heider Nielsen"
  4. __doc__ = r"""
  5. Created on 31-10-2020
  6. """
  7. from itertools import cycle
  8. import cv2
  9. from draugr.opencv_utilities import frame_generator
  10. from draugr.opencv_utilities.windows.image import show_image
  11. if __name__ == "__main__":
  12. def variant0():
  13. """
  14. :return:
  15. :rtype:
  16. """
  17. cameras = []
  18. print("open cam1")
  19. cameras += ((frame_generator(cv2.VideoCapture(0)), cycle("0")),)
  20. print("open cam2")
  21. cameras += ((frame_generator(cv2.VideoCapture(1)), cycle("1")),)
  22. print("opened")
  23. a = iter(cycle(cameras))
  24. class AsyncIterator:
  25. def __aiter__(self):
  26. return self
  27. async def __anext__(self):
  28. # await asyncio.sleep(0.1)
  29. g, i = next(a)
  30. return next(g), next(i)
  31. async def run():
  32. """description"""
  33. async for image, idd in AsyncIterator():
  34. # await asyncio.sleep(0.1)
  35. if show_image(image, idd, wait=1):
  36. break
  37. import asyncio
  38. loop = asyncio.get_event_loop()
  39. try:
  40. loop.run_until_complete(run())
  41. finally:
  42. loop.close()
  43. def variant1():
  44. """description"""
  45. cameras = []
  46. print("open cam1")
  47. # cameras += frame_generator(cv2.VideoCapture(0)),
  48. print("open cam2")
  49. cameras += (frame_generator(cv2.VideoCapture(1)),)
  50. print("opened")
  51. def show(mat, n=0):
  52. """
  53. :param mat:
  54. :type mat:
  55. :param n:
  56. :type n:
  57. """
  58. cv2.imshow(f"{n}", mat)
  59. while 1:
  60. list(map(show, map(next, cameras)))
  61. cv2.waitKey(1)
  62. def variant1_2():
  63. """description"""
  64. cameras = []
  65. print("open cam1")
  66. # cameras += frame_generator(cv2.VideoCapture(0)),
  67. print("open cam2")
  68. cameras += (frame_generator(cv2.VideoCapture(1)),)
  69. print("opened")
  70. def show(mat, n=0):
  71. """
  72. :param mat:
  73. :type mat:
  74. :param n:
  75. :type n:
  76. """
  77. cv2.imshow(f"{n}", mat)
  78. while 1:
  79. list(
  80. map(show, *zip(map(next, cameras), range(2)))
  81. ) # BROKEN, maybe need to run show on main loop
  82. cv2.waitKey(1)
  83. def variant2():
  84. """description"""
  85. cameras = []
  86. print("open cam1")
  87. cameras += (frame_generator(cv2.VideoCapture(0)),)
  88. print("open cam2")
  89. cameras += (frame_generator(cv2.VideoCapture(1)),)
  90. print("opened")
  91. def show(mat, n):
  92. """
  93. :param mat:
  94. :type mat:
  95. :param n:
  96. :type n:
  97. """
  98. cv2.imshow(f"{n}", mat)
  99. while 1:
  100. for im, i in zip(map(next, cameras), range(len(cameras))):
  101. show(im, i)
  102. cv2.waitKey(1)
  103. def variant3():
  104. """description"""
  105. cameras = []
  106. print("open cam1")
  107. cameras += (frame_generator(cv2.VideoCapture(0)),)
  108. print("open cam2")
  109. cameras += (frame_generator(cv2.VideoCapture(1)),)
  110. print("opened")
  111. from itertools import count
  112. def show(mat, n):
  113. """
  114. :param mat:
  115. :type mat:
  116. :param n:
  117. :type n:
  118. """
  119. cv2.imshow(f"{n}", mat)
  120. while 1:
  121. for i, im in count(map(next, cameras)):
  122. show(im, i)
  123. cv2.waitKey(1)
  124. variant3()