Advertisement
Guest User

Untitled

a guest
Feb 19th, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.01 KB | None | 0 0
  1. import io
  2. import socket
  3. import struct
  4. import time
  5. import picamera
  6. import argparse
  7. import logging
  8.  
  9. logging.basicConfig(level=logging.INFO, datefmt='%m/%d/%Y %I:%M:%S %p', format='%(asctime)s - %(name)s - %(message)s')
  10.  
  11. width = 640 # Frame width
  12. height = 480 # Frame height
  13. fps = 30 # Camera FPS
  14. ip = "192.168.1.216" # WebSocket (computer) server ip address
  15. port = 2281 # WebSocket (computer) server port
  16. vflip = 0 # Flip frame vertically (0-False, 1-True)
  17. hflip = 0 # Flip frame horizontally (0-False, 1-True)
  18. timeout = 1 # Timeout for camera warmup in seconds
  19.  
  20. parser = argparse.ArgumentParser(description='Streaming video from Raspberry Pi.')
  21. parser.add_argument("-width", type=int, default=width, help="Width of frame.")
  22. parser.add_argument("-height", type=int, default=height, help="Height of frame.")
  23. parser.add_argument("-fps", type=int, default=fps, help="FPS from cam.")
  24. parser.add_argument("-ip", type=str, default=ip, help="WebSocket (computer) server ip address.")
  25. parser.add_argument("-port", type=int, default=port, help="WebSocket (computer) port.")
  26. parser.add_argument("-vflip", type=int, default=vflip, help="Flip frame vertically.")
  27. parser.add_argument("-hflip", type=int, default=hflip, help="Flip frame horizontally.")
  28. parser.add_argument("-timeout", type=int, default=timeout, help="Timeout for camera warmup in seconds.")
  29. args = vars(parser.parse_args())
  30.  
  31. while True:
  32. try:
  33. client_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
  34. client_socket.connect((args["ip"], args["port"]))
  35. connection = client_socket.makefile('wb')
  36. logging.info("Connected to server successfully.")
  37. except ConnectionRefusedError:
  38. time.sleep(args["timeout"])
  39. logging.error("Cant connect to server, retry for "+str(args["timeout"])+"s.")
  40. else:
  41. break
  42.  
  43. try:
  44. with picamera.PiCamera() as camera:
  45. logging.info("Starting broadcast to "+str(args["ip"])+".")
  46. vflipV = True if args["vflip"] == 1 else False
  47. hflipV = True if args["hflip"] == 1 else False
  48. camera.vflip = vflipV
  49. camera.hflip = hflipV
  50. camera.resolution = (args["width"], args["height"])
  51. camera.framerate = args["fps"]
  52. time.sleep(args["timeout"])
  53. start = time.time()
  54. stream = io.BytesIO()
  55.  
  56. try:
  57. for foo in camera.capture_continuous(stream, 'jpeg', use_video_port=True):
  58. connection.write(struct.pack('<L', stream.tell()))
  59. connection.flush()
  60. stream.seek(0)
  61. connection.write(stream.read())
  62. stream.seek(0)
  63. stream.truncate()
  64.  
  65. connection.write(struct.pack('<L', 0))
  66. except BrokenPipeError:
  67. print(203)
  68. logging.error("Cant send frame to "+str(args["ip"])+".")
  69. pass
  70. finally:
  71. try:
  72. connection.close()
  73. except BrokenPipeError:
  74. logging.error("Disconected from "+str(args["ip"])+".")
  75. pass
  76. client_socket.close()
  77. logging.info("Broadcast service was stopped.")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement