Advertisement
Guest User

Untitled

a guest
Sep 19th, 2019
195
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.88 KB | None | 0 0
  1. import subprocess as sp
  2. import cv2
  3. import numpy as np
  4. from PIL import Image
  5. import tensorflow as tf
  6.  
  7. ffmpeg_cmd_1 = ["./ffmpeg", "-y",
  8. "-hwaccel", "nvdec",
  9. "-c:v", "h264_cuvid",
  10. "-vsync", "0",
  11. "-max_delay", "500000",
  12. "-reorder_queue_size", "10000",
  13. "-i", "rtsp://admin:startdt123@192.168.2.71/Streaming/Channels/1",
  14. "-f", "rawvideo",
  15. "-pix_fmt", "yuv420p",
  16. "-preset", "slow",
  17. "-an", "-sn",
  18. "-vf", "fps=15",
  19. "-"]
  20.  
  21. ffmpeg_cmd_2 = ["./ffmpeg", "-y",
  22. "-hwaccel", "nvdec",
  23. "-c:v", "h264_cuvid",
  24. "-vsync", "0",
  25. "-max_delay", "500000",
  26. "-reorder_queue_size", "10000",
  27. "-i", "rtsp://admin:startdt123@192.168.2.72/Streaming/Channels/1",
  28. "-f", "rawvideo",
  29. "-preset", "slow",
  30. "-an", "-sn",
  31. "-pix_fmt", "yuv420p",
  32. "-vf", "fps=15",
  33. "-"]
  34.  
  35.  
  36. ffmpeg1 = sp.Popen(ffmpeg_cmd_1, stdout=sp.PIPE, bufsize=10)
  37. ffmpeg2 = sp.Popen(ffmpeg_cmd_2, stdout=sp.PIPE, bufsize=10)
  38.  
  39. class YUV2RGB_GPU():
  40. def __init__(self, w=1920, h=1080):
  41. config = tf.ConfigProto(gpu_options=tf.GPUOptions(per_process_gpu_memory_fraction=0.03))
  42. self.y = tf.placeholder(shape=(1, h, w), dtype=tf.float32)
  43. self.u = tf.placeholder(shape=(1, h, w), dtype=tf.float32)
  44. self.v = tf.placeholder(shape=(1, h, w), dtype=tf.float32)
  45. r = self.y+1.371*(self.v-128)
  46. g = self.y+0.338* (self.u-128)-0.698*(self.v-128)
  47. b = self.y+1.732*(self.u-128)
  48. result = tf.stack([b, g, r], axis=-1)
  49. self.result = tf.clip_by_value(result, 0, 255)
  50. self.sess = tf.Session(config=config)
  51.  
  52. def convert(self, y, u, v):
  53. results = self.sess.run(self.result, feed_dict={self.y:y, self.u: u, self.v: v})
  54. return results.astype(np.uint8)
  55.  
  56. C = YUV2RGB_GPU()
  57.  
  58. ffmpegs = [ffmpeg1, ffmpeg2]
  59. while True:
  60. w = 1920
  61. h = 1080
  62. k = w*h
  63. ys = []
  64. us = []
  65. vs = []
  66. for i in ffmpegs:
  67. x = i.stdout.read(int(w*h*6//4)) # read bytes of single frames
  68. y = np.frombuffer(x[0:k], dtype=np.uint8).reshape((h, w))
  69. u = np.frombuffer(x[k:k+k//4], dtype=np.uint8).reshape((h//2, w//2))
  70. v = np.frombuffer(x[k+k//4:], dtype=np.uint8).reshape((h//2, w//2))
  71. u = np.reshape(cv2.resize(np.expand_dims(u, -1), (w, h)), (h, w))
  72. v = np.reshape(cv2.resize(np.expand_dims(v, -1), (w, h)), (h, w))
  73. image = np.stack([y, u, v], axis=-1)
  74. ys.append(y)
  75. us.append(u)
  76. vs.append(v)
  77. image = C.convert(ys, us, vs)
  78. image = np.concatenate(image, axis=0)
  79. image = cv2.resize(image, None, fx=1/2, fy=1/2)
  80. cv2.imshow("im", image)
  81. if cv2.waitKey(20) & 0xFF == ord('q'):
  82. break
  83.  
  84. cv2.destroyAllWindows()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement