Advertisement
Guest User

Untitled

a guest
May 8th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.96 KB | None | 0 0
  1. [mkaczanowski@vm ~]$ cat camera.py
  2. import cv2
  3. import urllib.request
  4. import numpy as np
  5. import threading
  6.  
  7. # Key <-> code
  8. control = {
  9. 65362 : 0, # up
  10. 65364 : 2, # down
  11. 65361 : 4, # left
  12. 65363 : 6, # right
  13. ord('z'): 14,
  14. ord('x'): 12
  15.  
  16. }
  17. def http_get(url):
  18. urllib.request.urlopen(url).read()
  19.  
  20. class Server(object):
  21. def __init__(self, user, password, ip, encoder_port=81):
  22. self.user = user
  23. self.password = password
  24. self.ip = ip
  25. self.encoder_port = encoder_port
  26.  
  27. @property
  28. def base_url(self):
  29. return "http://%s:%d" % (self.ip, self.encoder_port)
  30.  
  31. @property
  32. def videostream_url(self):
  33. return "%s/videostream.cgi?user=%s&pwd=%s&resolution=32&rate=0" % (
  34. self.base_url, self.user, self.password
  35. )
  36.  
  37. def control_url(self, command):
  38. return "%s/decoder_control.cgi?user=%s&pwd=%s&command=%d&onestep=1" % (
  39. self.base_url, self.user, self.password, command
  40. )
  41.  
  42. def control(self, key):
  43. if key in control.keys():
  44. url = self.control_url(control[key])
  45. threading.Thread(target=http_get, args=(url,)).start()
  46.  
  47. def display(self):
  48. stream = urllib.request.urlopen(self.videostream_url)
  49. buff = bytes()
  50.  
  51. while True:
  52. buff += stream.read(500)
  53. a = buff.find(b'\xff\xd8')
  54. b = buff.find(b'\xff\xd9')
  55. if a != -1 and b != -1:
  56. jpg = buff[a:b+2]
  57. buff = buff[b+2:]
  58. i = cv2.imdecode(
  59. np.fromstring(jpg, dtype=np.uint8),
  60. cv2.IMREAD_COLOR
  61. )
  62. cv2.imshow('i', i)
  63. key = cv2.waitKey(1)
  64. if key == 27:
  65. exit(0)
  66. else:
  67. self.control(key)
  68.  
  69. if __name__ == "__main__":
  70. Server("admin", "", "192.168.4.116").display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement