Advertisement
Guest User

Untitled

a guest
Jul 14th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.70 KB | None | 0 0
  1. import paramiko
  2. from threading import Thread
  3. import numpy as np
  4. import subprocess as sp
  5. import cv2
  6. import time
  7.  
  8. PI_USER = "pi"
  9. PI_PASS = "raspberry"
  10. PI_HOST = "192.168.0.111"
  11. MY_IP = "192.168.0.106"
  12. PI_PORT = 8888
  13.  
  14. def setupCamera():
  15. sshClient = paramiko.SSHClient()
  16. sshClient.load_system_host_keys()
  17. sshClient.set_missing_host_key_policy(paramiko.AutoAddPolicy())
  18. sshClient.connect(PI_HOST, username=PI_USER, password=PI_PASS)
  19. channel = sshClient.get_transport().open_session()
  20. if channel is not None:
  21. command = "raspivid -vf -n -w 1280 -h 720 -o - -t 0 -b 2000000 | nc "+ MY_IP + " " + str(PI_PORT)
  22. channel.exec_command(command)
  23. print(" Connected to camera.")
  24. else:
  25. print(" Could not connect to PI. Quitting...")
  26.  
  27. print("Camera setup completed. Returning.")
  28. return
  29. class Cam:
  30. def __init__(self , port , size):
  31. self.port = port
  32. self.w , self.h = size
  33. self.img = np.array((self.w, self.h, 3), np.uint8)
  34. print(" IN :: Camera Module. Started Threading for updateFrame")
  35.  
  36. print("Inside updateFrame")
  37. NETCAT_BIN = "nc"
  38. ncCommand = [NETCAT_BIN,
  39. '-lp',str(self.port)]
  40. nPipe = sp.Popen(ncCommand, stdout = sp.PIPE , bufsize = 10**8)
  41. print("Created sp pipe")
  42. FFMPEG_BIN = "ffmpeg"
  43. fCommand = [ FFMPEG_BIN,
  44. '-i', 'pipe:0', # fifo is the named pipe
  45. '-loglevel' , 'quiet',
  46. '-pix_fmt', 'bgr24', # opencv requires bgr24 pixel format.
  47. '-vcodec', 'rawvideo',
  48. '-an','-sn', # we want to disable audio processing (there is no audio)
  49. '-f', 'image2pipe', '-']
  50. self.fPipe = sp.Popen(fCommand, stdin = nPipe.stdout , stdout = sp.PIPE, stderr=None, bufsize=11**8)
  51. time.sleep(2)
  52. setupCamera()
  53. print("Threading for image extraction begun...")
  54. Thread(target=self.updateFrame).start()
  55.  
  56. def getFrame(self):
  57. return self.img
  58.  
  59. def updateFrame(self):
  60. print("Inside updateframe")
  61. while True:
  62. raw_image = self.fPipe.stdout.read(self.h * self.w * 3)
  63. image = np.fromstring(raw_image,dtype = 'uint8')
  64. image = image.reshape((self.h,self.w,3))
  65. self.fPipe.stdout.flush()
  66. if image is not None:
  67. self.img = image
  68.  
  69. if __name__ == "__main__":
  70. print("this works?")
  71. camera = Cam(8888, (1280, 720))
  72. while True:
  73. image = camera.getFrame()
  74. print(image)
  75. cv2.imshow('frame',image)
  76. if cv2.waitKey(1) & 0xFF == ord('q'):
  77. exit(0)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement