Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [mkaczanowski@vm ~]$ cat camera.py
- import cv2
- import urllib.request
- import numpy as np
- import threading
- # Key <-> code
- control = {
- 65362 : 0, # up
- 65364 : 2, # down
- 65361 : 4, # left
- 65363 : 6, # right
- ord('z'): 14,
- ord('x'): 12
- }
- def http_get(url):
- urllib.request.urlopen(url).read()
- class Server(object):
- def __init__(self, user, password, ip, encoder_port=81):
- self.user = user
- self.password = password
- self.ip = ip
- self.encoder_port = encoder_port
- @property
- def base_url(self):
- return "http://%s:%d" % (self.ip, self.encoder_port)
- @property
- def videostream_url(self):
- return "%s/videostream.cgi?user=%s&pwd=%s&resolution=32&rate=0" % (
- self.base_url, self.user, self.password
- )
- def control_url(self, command):
- return "%s/decoder_control.cgi?user=%s&pwd=%s&command=%d&onestep=1" % (
- self.base_url, self.user, self.password, command
- )
- def control(self, key):
- if key in control.keys():
- url = self.control_url(control[key])
- threading.Thread(target=http_get, args=(url,)).start()
- def display(self):
- stream = urllib.request.urlopen(self.videostream_url)
- buff = bytes()
- while True:
- buff += stream.read(500)
- a = buff.find(b'\xff\xd8')
- b = buff.find(b'\xff\xd9')
- if a != -1 and b != -1:
- jpg = buff[a:b+2]
- buff = buff[b+2:]
- i = cv2.imdecode(
- np.fromstring(jpg, dtype=np.uint8),
- cv2.IMREAD_COLOR
- )
- cv2.imshow('i', i)
- key = cv2.waitKey(1)
- if key == 27:
- exit(0)
- else:
- self.control(key)
- if __name__ == "__main__":
- Server("admin", "", "192.168.4.116").display()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement