add async to speed up 2x

This commit is contained in:
bluepulsar
2025-12-27 15:05:07 +08:00
parent 3f4f42ee4a
commit a8b02105fc
3 changed files with 230 additions and 67 deletions
+49
View File
@@ -0,0 +1,49 @@
"""
Example 5: Async/Pipelined Processing
Overlaps image capture with deformation computation for higher throughput.
Pipeline: [Capture Thread] -> [Queue] -> [Compute Thread] -> [Queue] -> [Main]
"""
import os
os.environ["OPENCV_VIDEOIO_MSMF_ENABLE_HW_TRANSFORMS"] = "0"
import cv2
import orisys
def main():
sensor = orisys.Sensor("./s1.mp4", isstitch=True, cuda=True, verbose=True)
sensor.enable_async(queue_size=2)
print("\nPress 'q' to quit, 'r' to reset")
try:
while True:
data = sensor.get_async(timeout=0.05)
if data is None:
continue
print(f"FPS: {data['fps']:5.1f}, Fn: {data['fnormal']:8.2f}, "
f"Fx: {data['fshearx']:8.2f}, Fy: {data['fsheary']:8.2f}")
if data['img'] is not None and data['flow'] is not None:
arrows = orisys.util.draw_arrows(data['img'].copy(), data['flow'], threshold=5)
cv2.imshow("Deformation", arrows)
if data['depth'] is not None:
depth_img = cv2.applyColorMap((data['depth'] * 255).astype('uint8'), cv2.COLORMAP_HOT)
cv2.imshow("Depth", depth_img)
key = cv2.waitKey(1) & 0xFF
if key == ord('q'):
break
elif key == ord('r'):
sensor.reset()
finally:
sensor.disconnect()
cv2.destroyAllWindows()
if __name__ == '__main__':
main()