Implement Web camera based on tornado
Learning Python in the near future. Find a framework to learn, I choose to be tornado. Because it is not just a web development framework, it is also a server, asynchronous event Library, a lot more.
I've been through OPENCV, and I want to engage them both as a Web camera, and that starts.
To implement a response to a URL in tornado, you need to implement your own handle. Based on the interfaces you provide externally. It would be nice to implement the relevant interface.
The following is the file content for the entire project:
ImportTornado.ioloopImportTornado.webImportTornado.genImportCv2 fromTornado.optionsImportDefine, Optionsdefine ("Port", default = the, help ="Run in tornado on XXXX Port", type = int) define ("id", default =0, help ="Camera ID", type = int) def auth(func): def _auth(self): if notSelf.current_user:re = {"Code":404,"Message":"Login failed!"} self.write (re)Else: Func (self)return_auth; class basehandler(tornado.web.RequestHandler): def get_current_user(self): returnSelf.get_secure_cookie ("User") class loginhandler(basehandler): def get(self):Self.write (' ' <input type= ' "Submit" value= "sign in" > " ' </form></body>) def post(self):Name = Self.get_argument ("Name","Error")ifName = ="Error": Re = {"Code":404,"Message":"Login failed!"}Else: Self.set_secure_cookie ("User", name) Re = {"Code": $,"Message":"Login successfully!"} self.write (re) class camerahandler(basehandler): @auth def get(self):RET, image = Self.application.cap.read ()ifRet:self.set_header ("Content-type","Image/jpeg") Self.set_header ("Refresh","1") Self.set_header ("Content-transfer-encoding","Binary") r, i = Cv2.imencode ('. jpg ', image)ifR:self.write (bytes (i.data))Else: Selt.write (' Sorry, encode faily! ')Else: Self.write (' Sorry, get camera data faily! ') class application(tornado.web.Application): def __init__(self, camera_id):handlers = [('/camera ', Camerahandler), ('/login ', Loginhandler)] Self.cap = Cv2. Videocapture (camera_id) self.camera_id = camera_id; Tornado.web.application.__init__ (self, handlers, debug =True, Cookie_secret ="61oetzkxqagaydkl5gemgejjfuyh7eqnp2xdtp1o") def __del__(self):Self.cap.release ()if__name__ = =' __main__ ': Tornado.options.parse_command_line (); App = Application (options.id) App.listen (options.port) tornado.ioloop.IOLoop.instance (). Start ()
A sample login is also included in this example. I studied tornado in order to use it to do the background of the app, so I implemented my own auth decorator, just to return a string of JSON characters. Instead of redirecting to the login page.
The device is set at the application construct, and http://localhost:xxx/camera
a picture is returned with the Get method on the URL.
def get(self):RET, image = Self.application.cap.read ()ifRet:self.set_header ("Content-type","Image/jpeg") Self.set_header ("Refresh","1") Self.set_header ("Content-transfer-encoding","Binary") r, i = Cv2.imencode ('. jpg ', image)ifR:self.write (bytes (i.data))Else: Selt.write (' Sorry, encode faily! ')Else: Self.write (' Sorry, get camera data faily! ')
This code is this function, at the beginning of the time I just take out the data to send, not to Imencode. Causes the browser not to display correctly. Use refresh to implement your own active refresh.
Project Address: Https://git.oschina.net/zhouX/web_camera.git
No picture, no truth:
watermark/2/text/ahr0cdovl2jsb2cuy3nkbi5uzxqvemh4nja0na==/font/5a6l5l2t/fontsize/400/fill/i0jbqkfcma==/ Dissolve/70/gravity/center "alt=" here to enter the description of the picture "title=" ">
Implement Web camera based on tornado