Video be freeze over Socket

asked 2019-09-01 16:19:31 -0500

I find the following code for streaming video over a socket. When I run it, the video will be freeze at the beginning in the server-side. I traced the code and understood that in the, the third while loop condition creates an infinite loop because of the condition while len(data) < msg_size: is always satisfied. In other words, len(data) is always less than msg_size.
Could anyone help me to solve this issue?

from flask import Flask, render_template, Response
from streamer import Streamer

app = Flask(__name__)

def gen():
 streamer = Streamer('localhost', 8089)

 while True:
     if streamer.client_connected():
         yield (b'--frame\r\n'b'Content-Type: image/jpeg\r\n\r\n' + streamer.get_jpeg() + b'\r\n\r\n')

def index():
   return render_template('index.html')

 def video_feed():
    return Response(gen(), mimetype='multipart/x-mixed-replace; boundary=frame')

 if __name__ == '__main__':'localhost', threaded=True)

and the is:

import cv2
import threading
import socket
import struct
import StringIO
import json
import numpy
class Streamer (threading.Thread):
   def __init__(self, hostname, port):

       self.hostname = hostname
       self.port = port
       self.connected = False
       self.jpeg = None

   def run(self):

        self.isRunning = True

        s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        print 'Socket created'

       s.bind((self.hostname, self.port))
       print 'Socket bind complete'

       data = ""
       payload_size = struct.calcsize("L")

       print 'Socket now listening'

      while self.isRunning:

        conn, addr = s.accept()
        print 'while 1...'

     while True:

         data = conn.recv(4096)

         print 'while 2...'
         if data:
            packed_msg_size = data[:payload_size]
            data = data[payload_size:]
           msg_size = struct.unpack("L", packed_msg_size)[0]

           while len(data) < msg_size: # here is the infinite loop
           data += conn.recv(4096)
           print ("lenght of data is " , len(data) )
           print ("message size is  " ,  msg_size )

      frame_data = data[:msg_size]
      #frame_data = data[:len(data)]
      memfile = StringIO.StringIO()
      frame = numpy.load(memfile)

      ret, jpeg = cv2.imencode('.jpg', frame)
      self.jpeg = jpeg

      self.connected = True
      print 'recieving...'

      self.connected = False
      print 'connected=false...'

 self.connected = False

def stop(self):
    self.isRunning = False

def client_connected(self):
   return self.connected

def get_jpeg(self):
   return self.jpeg.tobytes()

and the is:

import cv2
import numpy as np
import socket 
import sys
import pickle
import struct
import StringIO
import json
import time



   memfile = StringIO.StringIO(), frame)
   data = json.dumps('latin-1'))

  clientsocket.sendall(struct.pack("L", len(data))+data)
  if cv2.waitKey(1) & 0xFF == ord('q'):


and the javascript is:

         <title>Video Streaming Demonstration</title>
        <h1>Video Streaming Demonstration</h1>
        <img src="{{ url_for('video_feed') }}">
edit retag flag offensive close merge delete


In You have missing brace bracket in print function. And indetation isn't set properly, becasue you set to 4. And If/else is outside of block condition.. And while condition block isn't properly set indentation.

supra56 gravatar imagesupra56 ( 2019-09-03 08:56:52 -0500 )edit