def run(self): log_lib.debug("Starting ...") start_time = time.time() try: while not self.stop_event.is_set(): frame_size_bytes = self.socket.recv(4) frame_size = int.from_bytes(frame_size_bytes, byteorder='little', signed=False) frame_data = self.socket.recv(frame_size) log_lib.debug( "Received a frame of size of {} B. Expected frame size was {} B" .format(len(frame_data), frame_size)) self.queue.put(frame_data) log_lib.debug( "Added data to Queue. Current Queue size {}".format( queue.qsize())) time.sleep(self.sleeping_time) except Exception as e: log_lib.fatal(str(e)) finally: elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def tcp_receiver(stop_event, queue, sock, sleeping_time=0.0001): log_lib.debug("Starting ...") start_time = time.time() while not stop_event.is_set(): log_lib.debug("Current Queue size {}".format(queue.qsize())) frame_size_bytes = b'' len_frame_size_bytes = 0 while len_frame_size_bytes < 4: frame_size_bytes_left_to_recv = 4 - len_frame_size_bytes tmp_frame_size_bytes = sock.recv(frame_size_bytes_left_to_recv) frame_size_bytes += tmp_frame_size_bytes len_frame_size_bytes = len(frame_size_bytes) if frame_size_bytes == b'': raise Exception( "Bad data received on socket. Got no bytes for Frame Size") frame_size = int.from_bytes(frame_size_bytes, byteorder='little', signed=False) frame_data = b'' len_frame_data = 0 while len_frame_data < frame_size: frame_data_left_to_recv = frame_size - len_frame_data tmp_frame_data = sock.recv(frame_data_left_to_recv) frame_data += tmp_frame_data len_frame_data = len(frame_data) if frame_data == b'': log_lib.error( "Bad data recevid on socket. Got no bytes for an Expected Frame of size {}" .format(frame_size)) log_lib.debug( "Received a frame of size of {} B. Expected frame size was {} B". format(len(frame_data), frame_size)) # Give just last frame to the processor while queue.qsize() > 0: try: queue.get(False) except Empty: continue queue.put(frame_data) log_lib.debug("Added data to Queue. Current Queue size {}".format( queue.qsize())) time.sleep(sleeping_time) elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def test(): log_lib.info("Starting ...") start_time = time.time() remote_addr = 'localhost' remote_port = 1717 receiver_lib.tcp_receiver(remote_addr, remote_port) elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def queues_monitoring(stop_event, queue_in, queue_out, sleeping_time=0.0001): log_lib.info("Starting ...") start_time = time.time() while not stop_event.is_set(): log_lib.info("Input Queue size: %d | Output Queue size: %d" % (queue_in.qsize(), queue_out.qsize()), overwrite=True) time.sleep(sleeping_time) elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def frame_processor(stop_event, queue_in, queue_out, sleeping_time=0.0001): log_lib.debug("Starting ...") start_time = time.time() while not stop_event.is_set(): try: log_lib.debug( "Trying to pull data from the Input Queue. Current Input Queue size {}" .format(queue_in.qsize())) if queue_in.qsize() == 0: time.sleep(sleeping_time) continue frame_data = queue_in.get(block=True) log_lib.debug( "Pulled a frame of size of {} B from Input Queue. Current Input Queue size {}" .format(len(frame_data), queue_in.qsize())) frame = cv2.imdecode(np.fromstring(frame_data, dtype=np.uint8), -1) # Give just last frame to the viewer if type(queue_out) is LifoQueue: while queue_out.qsize() > 0: try: queue_out.get(False) except Empty: continue queue_out.put(frame) log_lib.debug( "Added data to Output Queue. Current Output Queue size {}". format(queue_out.qsize())) time.sleep(sleeping_time) except Exception as e: log_lib.error(str(e)) break elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def frame_viewer(stop_event, queue, sleeping_time=0.0001): log_lib.debug("Starting ...") start_time = time.time() w = int(1920 / 5) h = int(1080 / 5) windowName = 'Window' cv2.namedWindow(windowName, cv2.WINDOW_NORMAL) cv2.resizeWindow(windowName, w, h) while not stop_event.is_set(): try: log_lib.debug( "Trying to pull data from Queue. Current Queue size {}".format( queue.qsize())) if queue.qsize() == 0: time.sleep(sleeping_time) continue frame = queue.get(block=True) time.sleep(sleeping_time) log_lib.debug( "Pulled a frame of size of {} B from Queue. Current Queue size {}" .format(len(frame), queue.qsize())) cv2.imshow(windowName, frame) cv2.waitKey(1) except Exception as e: log_lib.error(str(e)) break try: cv2.destroyAllWindows() except: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def run(self): log_lib.debug("Starting ...") start_time = time.time() while not self.stop_event.is_set(): try: log_lib.debug( "Trying to pull data from Queue. Current Queue size {}". format(queue.qsize())) if queue.qsize() == 0: time.sleep(sleeping_time) continue frame_data = queue.get(block=True) log_lib.debug( "Pulled a frame of size of {} B from Queue. Current Queue size {}" .format(len(frame_data), queue.qsize())) frame = cv2.imdecode(np.fromstring(frame_data, dtype=np.uint8), -1) cv2.imshow('Window', frame) cv2.wait(1) #time.sleep(sleeping_time) except Exception as e: log_lib.fatal(str(e)) break try: cv2.destroyAllWindows() except: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def test(): log_lib.info("Starting ...") start_time = time.time() try: remote_addr = 'localhost' remote_port = 1717 # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (remote_addr, remote_port) log_lib.info("Starting TCP Connection to {}:{} ...".format( remote_addr, remote_port)) sock.connect(server_address) log_lib.info( "TCP Connection to {}:{} successfully established!".format( remote_addr, remote_port)) # Retrieve Global Header Informartions #time.sleep(sleeping_time) globalHeader = sock.recv(24) version = globalHeader[0] header_size = globalHeader[1] pid = int.from_bytes(globalHeader[2:5], byteorder='little', signed=False) real_width = int.from_bytes(globalHeader[6:9], byteorder='little', signed=False) real_height = int.from_bytes(globalHeader[10:13], byteorder='little', signed=False) virtual_width = int.from_bytes(globalHeader[14:17], byteorder='little', signed=False) virtual_height = int.from_bytes(globalHeader[18:21], byteorder='little', signed=False) display_orientation = globalHeader[22] quirk_flag = globalHeader[23] window_name = str(pid) + " " + str(virtual_width) + "x" + str( virtual_height) # Thread for retrieving data from TCP time.sleep(main_loop_sleeping_time) # Thread for Processing/Displaying frames time.sleep(main_loop_sleeping_time) # Thread for Displaying frames time.sleep(main_loop_sleeping_time) while True: time.sleep(main_loop_sleeping_time) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") except Exception as e: log_lib.fatal(str(e)) finally: log_lib.info("Closing TCP socket {}:{} ...".format( remote_addr, remote_port)) sock.close() elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))
def test(): log_lib.info("Starting ...") start_time = time.time() main_loop_sleeping_time = 0.001 try: q_in = Queue() q_out = Queue() # Thread for retrieving data from TCP receiver_worker_stop = Event() receiver_worker = Thread(target=receiver_lib.tcp_receiver, args=( receiver_worker_stop, q_in, None, )) receiver_worker.setDaemon(False) receiver_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Processing processing_worker_stop = Event() processing_worker = Thread(target=processing_lib.frame_processor, args=( processing_worker_stop, q_in, q_out, )) processing_worker.setDaemon(False) processing_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Displaying frames viewer_worker_stop = Event() viewer_worker = Thread(target=processing_lib.frame_viewer, args=( viewer_worker_stop, q_out, )) viewer_worker.setDaemon(False) viewer_worker.start() time.sleep(main_loop_sleeping_time) while True: time.sleep(main_loop_sleeping_time) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") receiver_worker_stop.set() log_lib.info("Receiver Thread terminating ...") receiver_worker.join() log_lib.info("Receiver Thread is done!") time.sleep(0.2) processing_worker_stop.set() log_lib.info("Processing Thread terminating ...") processing_worker.join() log_lib.info("Processing Thread is done!") time.sleep(0.2) viewer_worker_stop.set() log_lib.info("Viewer Thread terminating ...") viewer_worker.join() log_lib.info("Viewer Thread is done!") time.sleep(0.2) except Exception as e: log_lib.fatal(str(e)) finally: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def test(): log_lib.info("Starting ...") start_time = time.time() try: log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 1", overwrite=True) log_lib.error( "Test Line Test Line Test Line Test Line Line Test Line Test Line 2" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 3", overwrite=True) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 4", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 5", overwrite=True) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 6", overwrite=True) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 7", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 8", overwrite=True) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 9", overwrite=True) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 10" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 11", overwrite=True) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 12", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 13", overwrite=True) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 14" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 15", overwrite=True) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 16", overwrite=True) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 17" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 18", overwrite=True) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 19", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 20", overwrite=True) log_lib.error( "Test Line Test Line Test Line Test Line Line Test Line Test Line 21" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 22", overwrite=True) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 23" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 24", overwrite=True) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 25" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 26", overwrite=True) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 27", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 28", overwrite=True) log_lib.error( "Test Line Test Line Test Line Test Line Line Test Line Test Line 29" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 30" ) for i in range(1, 101, 5): utils_lib.print_progress_bar(i, 100) time.sleep(0.02) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 31" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 32", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 33", overwrite=True) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 34" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 35", overwrite=True) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") except Exception as e: log_lib.fatal(str(e)) finally: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def test(): log_lib.info("Starting ...") start_time = time.time() main_loop_sleeping_time = 0.0001 receiver_worker_sleeping_time = 0.0001 processing_worker_sleeping_time = 0.0001 viewer_worker_sleeping_time = 0.02 monitoring_worker_sleeping_time = 0.01 remote_addr = 'localhost' remote_port = 1717 try: # Create a TCP/IP socket sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM) server_address = (remote_addr, remote_port) log_lib.info("Starting TCP Connection to {}:{} ...".format( remote_addr, remote_port)) sock.connect(server_address) log_lib.info( "TCP Connection to {}:{} successfully established!".format( remote_addr, remote_port)) # Retrieve Global Header Informartions #time.sleep(sleeping_time) globalHeader = sock.recv(24) version = globalHeader[0] header_size = globalHeader[1] pid = int.from_bytes(globalHeader[2:5], byteorder='little', signed=False) real_width = int.from_bytes(globalHeader[6:9], byteorder='little', signed=False) real_height = int.from_bytes(globalHeader[10:13], byteorder='little', signed=False) virtual_width = int.from_bytes(globalHeader[14:17], byteorder='little', signed=False) virtual_height = int.from_bytes(globalHeader[18:21], byteorder='little', signed=False) display_orientation = globalHeader[22] quirk_flag = globalHeader[23] log_lib.info("Information from the Server: version {}".format(version)) log_lib.info( "Information from the Server: header_size {}".format(header_size)) log_lib.info("Information from the Server: pid {}".format(pid)) log_lib.info( "Information from the Server: real_width {}".format(real_width)) log_lib.info( "Information from the Server: real_height {}".format(real_height)) log_lib.info("Information from the Server: virtual_width {}".format( virtual_width)) log_lib.info("Information from the Server: virtual_height {}".format( virtual_height)) log_lib.info( "Information from the Server: display_orientation {}".format( display_orientation)) log_lib.info( "Information from the Server: quirk_flag {}".format(quirk_flag)) window_name = str(pid) + " " + str(virtual_width) + "x" + str( virtual_height) q_in = LifoQueue() q_out = LifoQueue() log_lib.info("Worker {} sleeping time {}".format( 'RECEIVER', receiver_worker_sleeping_time)) log_lib.info("Worker {} sleeping time {}".format( 'PROCESSING', processing_worker_sleeping_time)) log_lib.info("Worker {} sleeping time {}".format( 'VIEWER', viewer_worker_sleeping_time)) log_lib.info("Worker {} sleeping time {}".format( 'MONITORING', monitoring_worker_sleeping_time)) # Thread for retrieving data from TCP receiver_worker_stop = Event() receiver_worker = Thread(target=receiver_lib.tcp_receiver, args=( receiver_worker_stop, q_in, sock, receiver_worker_sleeping_time, )) receiver_worker.setDaemon(False) receiver_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Processing processing_worker_stop = Event() processing_worker = Thread(target=processing_lib.frame_processor, args=( processing_worker_stop, q_in, q_out, processing_worker_sleeping_time, )) processing_worker.setDaemon(False) processing_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Displaying frames viewer_worker_stop = Event() viewer_worker = Thread(target=processing_lib.frame_viewer, args=( viewer_worker_stop, q_out, viewer_worker_sleeping_time, )) viewer_worker.setDaemon(False) viewer_worker.start() time.sleep(main_loop_sleeping_time) # Thread for Monitoring queues status monitoring_worker_stop = Event() monitoring_worker = Thread(target=monitoring_lib.queues_monitoring, args=( monitoring_worker_stop, q_in, q_out, monitoring_worker_sleeping_time, )) monitoring_worker.setDaemon(False) monitoring_worker.start() time.sleep(main_loop_sleeping_time) while True: time.sleep(main_loop_sleeping_time) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") receiver_worker_stop.set() log_lib.info("Receiver Thread terminating ...") receiver_worker.join() log_lib.info("Receiver Thread is done!") time.sleep(0.2) processing_worker_stop.set() log_lib.info("Processing Thread terminating ...") processing_worker.join() log_lib.info("Processing Thread is done!") time.sleep(0.2) viewer_worker_stop.set() log_lib.info("Viewer Thread terminating ...") viewer_worker.join() log_lib.info("Viewer Thread is done!") time.sleep(0.2) monitoring_worker_stop.set() log_lib.info("Monitoring Thread terminating ...") monitoring_worker.join() log_lib.info("Monitoring Thread is done!") time.sleep(0.2) except Exception as e: log_lib.fatal(str(e)) finally: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def test(): log_lib.info("Starting ...") start_time = time.time() try: log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 1" ) log_lib.error( "Test Line Test Line Test Line Test Line Line Test Line Test Line 2" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 3" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 4" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 5" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 6" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 7" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 8" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 9" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 10" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 11" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 12" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 13" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 14" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 15" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 16" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 17" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 18" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 19" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 20" ) log_lib.error( "Test Line Test Line Test Line Test Line Line Test Line Test Line 21" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 22" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 23" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 24" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 25" ) log_lib.warning( "Test Line Test Line Test Line Test Line Line Test Line Test Line 26" ) log_lib.debug( "Test Line Test Line Test Line Test Line Line Test Line Test Line 27" ) log_lib.info( "Test Line Test Line Test Line Test Line Line Test Line Test Line 28" ) log_lib.error( "Test Line Test Line Test Line Test Line Line Test Line Test Line 29" ) log_lib.fatal( "Test Line Test Line Test Line Test Line Line Test Line Test Line 30" ) except KeyboardInterrupt: log_lib.info("Keyboard interrupt received. Terminating ...") except Exception as e: log_lib.fatal(str(e)) finally: pass elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time)) return
def tcp_receiver(stop_event, queue, sock, sleeping_time=0.001): log_lib.info("Starting ...") start_time = time.time() input_path = 'input' input_filename = '20190827_215900.mp4' w = int(1920 / 5) h = int(1080 / 5) # Command for extract a sequence of jpgs from a video file # ffmpeg -i .\input\video.mp4 -c:v mjpeg -f image2pipe -s Width x Height pipe:1 input_fullfilename = os.path.join(input_path, input_filename) cmd = [ 'ffmpeg', '-i', input_fullfilename, '-c:v', 'mjpeg', '-q:v', '1', '-f', 'image2pipe', '-s', '{}x{}'.format(w, h), 'pipe:1' ] input_process = subprocess.Popen(cmd, shell=True, stdout=subprocess.PIPE) [input_data, input_err] = input_process.communicate(input=input_process) if input_err: log_lib.fatal(input_err) return log_lib.debug("Got %d Bytes of data" % len(input_data)) # Gather an array of JPGs # A JPG is delimited by 2 Sequences: # SOI (Start of Image) 0xFF 0xD8 # EOI (End of Image) 0xFF 0xD9 frames = [] soi_pattern = br'\xFF\xD8' regex = re.compile(soi_pattern) start_indexes = [m.start(0) for m in re.finditer(soi_pattern, input_data)] #print(start_indexes) #print(len(start_indexes)) eoi_pattern = br'\xFF\xD9' regex = re.compile(eoi_pattern) end_indexes = [m.end(0) for m in re.finditer(eoi_pattern, input_data)] #print(end_indexes) #print(len(end_indexes)) for i in range(0, len(start_indexes), 1): start = start_indexes[i] end = end_indexes[i] frame_data = input_data[start:end] frames.append(frame_data) log_lib.debug("Extracted %d jpg frames" % len(frames)) i = 0 while not stop_event.is_set(): if i >= len(frames): i = 0 frame_data = frames[i] log_lib.debug("Current Queue size {}".format(queue.qsize())) log_lib.debug( "Received a frame of size of {} B. Expected frame size was {} B". format(len(frame_data), len(frame_data))) queue.put(frame_data) log_lib.debug("Added data to Queue. Current Queue size {}".format( queue.qsize())) i += 1 time.sleep(sleeping_time) elapsed_time = time.time() - start_time log_lib.info("Completed in %s" % utils_lib.elapsed_time_string(elapsed_time))