def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .1 if self.served_image_timestamp + interval < time.time(): # Can't serve the OpenCV numpy array # Tornando: "... only accepts bytes, unicode, and dict objects" (from Tornado error Traceback) img = cv2.imencode('.jpg', self.application.img_arr)[1].tostring() # I have no idea what these lines do, but other people seem to use them, they # came with this copied code and I don't want to break something by removing self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) # Serve the image self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( 'Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0' ) self.set_header('Connection', 'close') self.set_header( 'Content-Type', 'multipart/x-mixed-replace;boundary=--boundarydonotcross') self.set_header('Expires', 'Mon, 1 Jan 2000 00:00:00 GMT') self.set_header('Pragma', 'no-cache') self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross\n" while True: img = open('/tmp/mjpeg.jpg', 'rb').read() interval = 0.5 if self.served_image_timestamp + interval > time.time(): self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(str(img)) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .1 if self.served_image_timestamp + interval < time.time(): now = datetime.datetime.now().strftime('%H:%M:%S.%f') # print(now + " Video API get IN served_img_time {} + {} vs {}".format(self.served_image_timestamp,interval,time.time())) img = util.img.arr_to_binary(self.application.img_arr) self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header("Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .1 if self.served_image_timestamp + interval < time.time(): # Can't serve the OpenCV numpy array # Tornando: "... only accepts bytes, unicode, and dict objects" (from Tornado error Traceback) img = cv2.imencode('.jpg', self.application.img_arr)[1].tostring() # I have no idea what these lines do, but other people seem to use them, they # came with this copied code and I don't want to break something by removing self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) # Serve the image self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self, vehicle_id): ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .2 if self.served_image_timestamp + interval < time.time(): img = self.application.vehicles[vehicle_id]['img'] img = dk.utils.img_to_binary(img) self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Cache-Control", "no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0", ) self.set_header("Connection", "close") self.set_header( "Content-Type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.set_header("Expires", "Mon, 3 Jan 2000 12:34:56 GMT") self.set_header("Pragma", "no-cache") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross\n" while True: img = camera.get_jpeg_image_bytes() interval = 1.0 if self.served_image_timestamp + interval < time.time(): self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(str(img)) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( 'Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0' ) self.set_header('Pragma', 'no-cache') self.set_header('Content-Type', 'multipart/x-mixed-replace;boundary=--jpgboundary') self.set_header('Connection', 'close') self.served_image_timestamp = time.time() my_boundary = "--jpgboundary" while True: # Generating images for mjpeg stream and wraps them into http resp if self.get_argument('fd') == "true": img = cam.get_frame(True) else: img = cam.get_frame(False) interval = 0.1 if self.served_image_timestamp + interval < time.time(): self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .1 if self.served_image_timestamp + interval < time.time(): img = util.img.arr_to_binary(self.application.img_arr) self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(img) self.served_image_timestamp = time.time() #print("web.py len(img)={}".format(len(img))) yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header("Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .1 if self.served_image_timestamp + interval < time.time(): try: img = utils.arr_to_binary(self.application.img_arr) self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(img) self.served_image_timestamp = time.time() except tornado.iostream.StreamClosedError as e: print('StreamClosedError:', e) break except tornado.websocket.WebSocketClosedError as e: print('WebSocketClosedError:', self) break except KeyError as e: print('KeyError:', e) break yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def clear_lcd_screen(ioloop): from libs.lcd_display import clear_display if (myButton.value() == 1): print myButton.name(), ' value is ', myButton.value() clear_display(myLcd) time.sleep(0.5) # Schedule next callback_time = 0 ioloop.call_at(ioloop.time() + callback_time, clear_lcd_screen, ioloop)
def main(): http_server = tornado.httpserver.HTTPServer(Tutu()) http_server.listen(options.port) ioloop = tornado.ioloop.IOLoop.instance() if not TEST: ioloop.add_timeout(ioloop.time(), registry_marathon_event_handler) ioloop.start()
def clear_lcd_screen(ioloop): from libs.lcd_display import clear_display if(myButton.value() == 1): print myButton.name(), ' value is ', myButton.value() clear_display(myLcd) time.sleep(0.5) # Schedule next callback_time = 0 ioloop.call_at(ioloop.time() + callback_time, clear_lcd_screen, ioloop)
def update_projectiles(self): for projectile in App.Projectiles: if projectile.is_crossed_boundary(): App.Projectiles.remove(projectile) self.check_players_for_hit(projectile) dt = ioloop.time() - projectile.time projectile.update(dt) mes = ProjectileMoved(projectile) self.notify_all_players(mes)
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" while True: interval = .1 if self.served_image_timestamp + interval < time.time(): print('.') # For AWS Seoul Summit img_orig = self.application.img_arr.copy() img_orig = cv2.resize(img_orig, None, fx=6.0, fy=6.0, interpolation=cv2.INTER_LINEAR) overlay = img_orig.copy() cv2.rectangle(overlay, (10, 10), (880, 140), (0, 0, 0), -1) alpha = 0.3 cv2.addWeighted(overlay, alpha, img_orig, 1 - alpha, 0, img_orig) cv2.putText( img_orig, 'Predicted Steering Angle : ' + str(np.round(car_metric.global_angle, 2)), (20, 50), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText( img_orig, 'Predicted Throttle : ' + str(np.round(car_metric.global_throttle, 2)), (20, 100), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) cv2.putText(img_orig, '(c) AWS Korea', (700, 700), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2, cv2.LINE_AA) img = utils.arr_to_binary(img_orig) #img = utils.arr_to_binary(self.application.img_arr) self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def _Update(self) : for deviceName,device in self.application.controller.devices.iteritems() : self._UpdateDeviceDescription(deviceName, device) for variableName,variable in device.GetVariables().iteritems() : self._UpdateVarDescription(deviceName, variableName, variable) self._UpdateVarValue(deviceName, variableName, device.Get(variableName)) ioloop = tornado.ioloop.IOLoop.instance() self.timeout = ioloop.add_timeout(ioloop.time() + .25, self._Update)
def test_wraps(): def exit_callback(): client = tornado.httpclient.AsyncHTTPClient() ret = client.fetch('http://localhost:8000/home/welcome') nose.tools.assert_is_not_none(ret) tornado.ioloop.IOLoop.current().stop() httpserver = tornado.httpserver.HTTPServer( blueprint.wraps(tornado.web.Application())) httpserver.listen(8000) ioloop = tornado.ioloop.IOLoop.current() ioloop.add_timeout(ioloop.time() + 1, exit_callback) ioloop.start()
def update_players(self): for player in WSConnectionHandler.players: time_elapsed = ioloop.time() - player.time if player.state == Player.STATE_MOVE: player.calculate_position(time_elapsed) player.time = ioloop.time() mes = PlayerPositionMessage((player.position.x, player.position.y), player.direction) player.send_message(mes) pmvd = PlayerMoved(player.uuid, (player.position.x, player.position.y), player.direction) self.notify_all_players(pmvd) if player.state == Player.STATE_ATTACK: # attack last 1 sec bullet = player.shoot() App.Projectiles.append(bullet) if time_elapsed > 0.3: player.state = Player.STATE_IDLE mes = StateChangeMessage(player.state) player.send_message(mes) if player.state == Player.STATE_HURT: if time_elapsed > 1000: player.state = Player.STATE_HURT
def main_loop(ioloop, iot_client): ''' Main Loop :param ioloop: Tornado ioloop instance ''' devices = iot_client.getDevices() chord_ind = 0 xyz_count = 0 # check shake for 5 sec for shake_slot in range(0, 15): myDigitalAccelerometer.getRawValues(x, y, z) outputStr = ("Raw values: x = {0}" " y = {1}" " z = {2}").format(upmMMA7660.intp_value(x), upmMMA7660.intp_value(y), upmMMA7660.intp_value(z)) if (abs(upmMMA7660.intp_value(x)) > SHAKE_THRESHOLD) or \ (abs(upmMMA7660.intp_value(y)) > SHAKE_THRESHOLD) or \ (abs(upmMMA7660.intp_value(z)) > SHAKE_THRESHOLD): print "value exceeded" print xyz_count xyz_count = xyz_count + 1 if (xyz_count >= xyz_thresh): print "increasing thresh" missing_devices = check_devices(devices) if len(missing_devices) > 0: print "Missing devices" + "\n" print missing_devices for chord_ind in range(0, 15): print myBuzzer.playSound(chords[chord_ind], 100000) print "buzzing" #time.sleep(0.1) chord_ind += 1 data = create_message('Alert!', 'Missing item', True, '10.10.40.3') enqueue_message(data) myBuzzer.stopSound() xyz_count = 0 print outputStr time.sleep(0.05) print "loop over" xyz_count = 0 # Schedule next callback_time = 0 ioloop.call_at(ioloop.time() + callback_time, main_loop, ioloop, iot_client) return
def main_loop(ioloop, iot_client): ''' Main Loop :param ioloop: Tornado ioloop instance ''' devices = iot_client.getDevices() chord_ind = 0 xyz_count = 0 # check shake for 5 sec for shake_slot in range (0, 15): myDigitalAccelerometer.getRawValues(x, y, z) outputStr = ("Raw values: x = {0}" " y = {1}" " z = {2}").format(upmMMA7660.intp_value(x), upmMMA7660.intp_value(y), upmMMA7660.intp_value(z)) if (abs(upmMMA7660.intp_value(x)) > SHAKE_THRESHOLD) or \ (abs(upmMMA7660.intp_value(y)) > SHAKE_THRESHOLD) or \ (abs(upmMMA7660.intp_value(z)) > SHAKE_THRESHOLD): print "value exceeded" print xyz_count xyz_count = xyz_count + 1 if (xyz_count >= xyz_thresh): print "increasing thresh" missing_devices = check_devices(devices) if len(missing_devices) > 0: print "Missing devices"+"\n" print missing_devices for chord_ind in range (0,15): print myBuzzer.playSound(chords[chord_ind], 100000) print "buzzing" #time.sleep(0.1) chord_ind += 1 data = create_message('Alert!', 'Missing item', True, '10.10.40.3') enqueue_message(data) myBuzzer.stopSound() xyz_count = 0 print outputStr time.sleep(0.05) print "loop over" xyz_count = 0 # Schedule next callback_time = 0 ioloop.call_at(ioloop.time() + callback_time, main_loop, ioloop, iot_client) return
def ping(): #logger.debug("ping for %s", self.id) now = ioloop.time() if self._expecting_pong: logger.warning("kernel %s died unexpectedly", self.id) self.stop() elif now > self.hard_deadline: logger.info("hard deadline reached for %s", self.id) self.stop() elif (self.timeout > 0 and now > self.deadline and self.status == "idle"): logger.info("kernel %s timed out", self.id) self.stop() else: hb.send(b'ping') self._expecting_pong = True
def get(self): print('{timestamp} - Received request'.format( timestamp=datetime.now().strftime("%Y-%m-%d %H:%M:%S.%f"))) ioloop = tornado.ioloop.IOLoop.current() self.set_header( "Content-type", "multipart/x-mixed-replace;boundary=--boundarydonotcross") self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross" # TODO: Remove this hardcoded URL while True: interval = .1 if self.served_image_timestamp + interval < time.time(): # Can't serve the OpenCV numpy array # Tornando: "... only accepts bytes, unicode, and dict objects" (from Tornado error Traceback) # The result of cv2.imencode is a tuple like: (True, some_image), but I have no idea what True refers to img = cv2.imencode( '.jpg', self.application.video_cache.read_cache())[1].tostring() # I have no idea what these lines do, but other people seem to use them, they # came with this copied code and I don't want to break something by removing self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) # Serve the image self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): """ functionality: generates GET response with mjpeg stream input: None :return: yields mjpeg stream with http header """ ioloop = tornado.ioloop.IOLoop.current() self._logger.debug("mjpeg stream started.") stream_type = self.get_argument('type', True) # Set http header fields self.set_header( 'Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0' ) self.set_header('Pragma', 'no-cache') self.set_header('Content-Type', 'multipart/x-mixed-replace;boundary=--jpgboundary') self.set_header('Connection', 'close') while not self.stop_mjpeg: try: interval = 1.0 if self.served_image_timestamp + interval < time.time(): # Generating images for mjpeg stream and wraps them into http resp img = self.getFrame(stream_type) if img is None: continue self.write("--jpgboundary\r\n") self.write("Content-type: image/jpeg\r\n") self.write("Content-length: {0}\r\n\r\n".format(len(img))) self.write(img) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval) except Exception as e: self._logger.warning("mjpeg stream stopped: {0}".format(e))
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header('Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0') self.set_header('Connection', 'close') self.set_header('Content-Type', 'multipart/x-mixed-replace;boundary=--boundarydonotcross') self.set_header('Expires', 'Mon, 1 Jan 2000 00:00:00 GMT') self.set_header('Pragma', 'no-cache') self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross\n" while True: img = open('/tmp/mjpeg.jpg', 'rb').read() interval = 0.5 if self.served_image_timestamp + interval > time.time(): self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(str(img)) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
import tornado.ioloop import tornado.web import json from datetime import datetime class MainHandler(tornado.web.RequestHandler): def get(self): self.write( json.dumps({ "current UTC": datetime.utcnow().strftime('%Y-%m-%d'), "IP address": self.request.remote_ip })) def make_app(): return tornado.web.Application([ (r"/", MainHandler), ]) if __name__ == "__main__": app = make_app() app.listen(8888) ioloop = tornado.ioloop.IOLoop.current() ioloop.start() print(ioloop.time()) ioloop.close()
def get(self): ioloop = tornado.ioloop.IOLoop.current() self.set_header( 'Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0' ) self.set_header('Pragma', 'no-cache') self.set_header('Content-Type', 'multipart/x-mixed-replace;boundary=--jpgboundary') self.set_header('Connection', 'close') self.served_image_timestamp = time.time() my_boundary = "--jpgboundary" while True: bild = self.application.bild bild = cv2.cvtColor(bild, cv2.COLOR_BGR2RGB) lenkung = self.application.lenkung beschleunigung = self.application.beschleunigung beschleunigung1 = int(beschleunigung * 160) lenkung = round(lenkung, 2) lenkung1 = int(lenkung * 80) if self.get_argument('fd') == "false": cv2.line(bild, (160, 460), (480, 460), (0, 0, 255), 2) cv2.line(bild, (160, 450), (160, 470), (0, 0, 255), 2) cv2.line(bild, (200, 455), (200, 465), (0, 0, 255), 2) cv2.line(bild, (240, 450), (240, 470), (0, 0, 255), 2) cv2.line(bild, (280, 455), (280, 465), (0, 0, 255), 2) cv2.line(bild, (320, 450), (320, 470), (0, 0, 255), 2) cv2.line(bild, (360, 455), (360, 465), (0, 0, 255), 2) cv2.line(bild, (400, 450), (400, 470), (0, 0, 255), 2) cv2.line(bild, (440, 455), (440, 465), (0, 0, 255), 2) cv2.line(bild, (480, 450), (480, 470), (0, 0, 255), 2) cv2.line(bild, (30, 160), (30, 320), (255, 0, 0), 2) cv2.line(bild, (20, 160), (40, 160), (255, 0, 0), 2) cv2.line(bild, (25, 180), (35, 180), (255, 0, 0), 2) cv2.line(bild, (20, 200), (40, 200), (255, 0, 0), 2) cv2.line(bild, (25, 220), (35, 220), (255, 0, 0), 2) cv2.line(bild, (20, 240), (40, 240), (255, 0, 0), 2) cv2.line(bild, (25, 260), (35, 260), (255, 0, 0), 2) cv2.line(bild, (20, 280), (40, 280), (255, 0, 0), 2) cv2.line(bild, (25, 300), (35, 300), (255, 0, 0), 2) cv2.line(bild, (20, 320), (40, 320), (255, 0, 0), 2) cv2.line(bild, (20, 240 + lenkung1), (40, 240 + lenkung1), (0, 255, 0), 6) cv2.line(bild, (320 + beschleunigung1, 470), (320 + beschleunigung1, 450), (0, 255, 0), 6) if self.get_argument('fd') == "true": bild = cv2.cvtColor(bild, cv2.COLOR_RGB2YUV) cv2.line(bild, (160, 460), (480, 460), (0, 0, 255), 2) cv2.line(bild, (160, 450), (160, 470), (0, 0, 255), 2) cv2.line(bild, (200, 455), (200, 465), (0, 0, 255), 2) cv2.line(bild, (240, 450), (240, 470), (0, 0, 255), 2) cv2.line(bild, (280, 455), (280, 465), (0, 0, 255), 2) cv2.line(bild, (320, 450), (320, 470), (0, 0, 255), 2) cv2.line(bild, (360, 455), (360, 465), (0, 0, 255), 2) cv2.line(bild, (400, 450), (400, 470), (0, 0, 255), 2) cv2.line(bild, (440, 455), (440, 465), (0, 0, 255), 2) cv2.line(bild, (480, 450), (480, 470), (0, 0, 255), 2) cv2.line(bild, (30, 160), (30, 320), (255, 0, 0), 2) cv2.line(bild, (20, 160), (40, 160), (255, 0, 0), 2) cv2.line(bild, (25, 180), (35, 180), (255, 0, 0), 2) cv2.line(bild, (20, 200), (40, 200), (255, 0, 0), 2) cv2.line(bild, (25, 220), (35, 220), (255, 0, 0), 2) cv2.line(bild, (20, 240), (40, 240), (255, 0, 0), 2) cv2.line(bild, (25, 260), (35, 260), (255, 0, 0), 2) cv2.line(bild, (20, 280), (40, 280), (255, 0, 0), 2) cv2.line(bild, (25, 300), (35, 300), (255, 0, 0), 2) cv2.line(bild, (20, 320), (40, 320), (255, 0, 0), 2) cv2.line(bild, (20, 240 + lenkung1), (40, 240 + lenkung1), (0, 255, 0), 6) cv2.line(bild, (320 + beschleunigung1, 470), (320 + beschleunigung1, 450), (0, 255, 0), 6) erfolg, jpeg = cv2.imencode('.jpg', bild) bild = jpeg.tobytes() interval = 0.1 if self.served_image_timestamp + interval < time.time(): self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(bild)) self.write(bild) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval)
def get(self): # Get otherProgram Id (Note: Program and Other Program are reversed) # otherProgramId = self.get_argument('programId') # Get program Id (Note: Program and Other Program are reversed) # programId = self.get_argument('otherProgramId') # Get program # program = self.program # if program._id != programId : # body = { # "Title":"An error has occured", # "error":"program is unknown" # } # self.set_status(400) # self.write(json.dumps(body)) # self.finish() # # Get otherProgram # otherProgram = program.get_other_program_by_id(otherProgramId) # if not otherProgram : # body = { # "Title":"An error has occured", # "error":"other program is unkown to machine" # } # self.set_status(400) # self.write(json.dumps(body)) # self.finish() # # Check if http request is allowed # if not otherProgram.accept["httpConnection"]: # body = { # "Title":"An error has occured", # "error":"Program does not allow a request by you. I'm sorry." # } # self.set_status(400) # self.write(json.dumps(body)) # self.finish() ioloop = tornado.ioloop.IOLoop.current() self.set_header( 'Cache-Control', 'no-store, no-cache, must-revalidate, pre-check=0, post-check=0, max-age=0' ) self.set_header('Connection', 'close') self.set_header( 'Content-Type', 'multipart/x-mixed-replace;boundary=--boundarydonotcross') self.set_header('Pragma', 'no-cache') self.served_image_timestamp = time.time() my_boundary = "--boundarydonotcross\n" while True: img = self.camera.get_frame() interval = 0.001 if self.served_image_timestamp + interval < time.time(): self.write(my_boundary) self.write("Content-type: image/jpeg\r\n") self.write("Content-length: %s\r\n\r\n" % len(img)) self.write(str(img)) self.served_image_timestamp = time.time() yield tornado.gen.Task(self.flush) else: yield tornado.gen.Task(ioloop.add_timeout, ioloop.time() + interval) ioloop = tornado.ioloop.IOLoop.current()