Ejemplo n.º 1
0
    def run(self):
        try:
            while (self.key):

                Network.send_frame(self.connection,self.frames.get())

        except(KeyboardInterrupt,IOError,OSError) as e:
            pass

        finally:
            self.frames.close()
            self.connection.close()
            print('sending Frames is stopped')
Ejemplo n.º 2
0
    def run(self):
        try:
            msglen_sum = 0 #the size of total frames received 
            y = time() #Total time of the receiving
            # The loop to receive frames from the connection
            #self.key.value is a flag to be used by the main process to break the loop and terminate the parallel process
            while (self.key.value):
                x = time() # start recording the time of receiving each frame 
                frame_,msglen = Network.recv_frame(self.client) # receiving a frame 
                msglen_sum += msglen # updating the size of the total frames received

                
                #adding the (frame received,the size of the frame and the total time it took to receive it) in the shared memory buffer
                self.frames.put([frame_,msglen,time()-x]) 

            #calculating and printing the average speed of the connection 
            print('The secound process is terminated \n',
                  'The total average Rate is ',msglen_sum/((time-y)*1000),'KB/s')
            self.client.close() #closing the connection if the for loop is broken
            self.frames.close() #declaring that there is no data will be added to the queue from this process 


        #breaking the connection and terminating the process if there is an error , interruption or connection break 
        except ( KeyboardInterrupt,IOError,OSError)as e:
            
            self.frames.close() #declaring that there is no data will be added to the queue from this process 
            print('The secound process is terminated \n',   #calculating and printing the average speed of the connection 
                  'The total average Rate is ',msglen_sum/((time()-y)*1000),'KB/s')
            self.client.close() #closing the connection if the for loop is broken
        return
Ejemplo n.º 3
0
 def run(self):
     try:
         while (self.key_):
             flag = Network.recv_msg(self.connection, 1 , 1)
             flag = int(unpack(">B",flag)[0])
             fmb = self.fmb[flag]
             results = Network.recv_msg(self.connection,calcsize(fmb), 2048)
             results = unpack(fmb,results)
             with self.cond:
                 self.result_ = results
                 self.count -=1
     except(KeyboardInterrupt,IOError,OSError) as e:
         pass
     finally:
         self.connection.close()
         print('receiving results is stopped')
Ejemplo n.º 4
0
    def __init__(self,ip,port,status=True,w_max=30): 
        self.client = Network.set_client(ip, port) #setting the connection from the client end 
        self.frames = mp.Queue(0) # Setting the Queue object(Shared memory between process) 
        self.key  = mp.Value('b',True) #the flag across process to manage closing from the parallel process smoothly

        # invoking the base class constructor (defining the class as a multiprocessing class
        # inheriting the prvious initializations from the code besides the shared memory frames Queue and key
        mp.Process.__init__(self)
        self.status = status 
        if self.status:  # If the flag is true then initialize the mean object 
            self.m = Segmentation.mean(w_max)
Ejemplo n.º 5
0
    def get(self,rgb=True):
        frame_ = self.frames.get() #blocking until getting the data with time out of 60 s 
        if frame_ is 0:
            return 0,()
        frame_ , msglen , spf = frame_
        frame_ = Network.decode_frame(frame_) #decoding the frames
        if rgb:
            frame_ = frame_[...,::-1]    # Converting from BGR to RGB
        [msglen_rate,spf] = self.m.mean([msglen,spf])
        status = (1/spf,msglen_rate/(spf*1000))        

        return frame_ ,status                                #Returning the frame as an output
Ejemplo n.º 6
0
def First_step():
  num_crop = args.test_crops  
  test_segments = args.test_segments
  
  #this function do forward propagation and returns scores
  def eval_video(data):
      """
      Evaluate single video
      video_data : Tuple has 3 elments (data in shape (crop_number,num_segments*length,H,W), label)
      return     : predictions and labels
      """
      if args.modality == 'RGB':
          length = 3
      elif args.modality == 'RGBDiff':
          length = 18
      else:
          raise ValueError("Unknown modality " + args.modality)
    
      with torch.no_grad():
          #reshape data to be in shape of (num_segments*crop_number,length,H,W)
          input = data.view(-1, length, data.size(1), data.size(2))
          #Forword Propagation
          output = model(input)
          output_np = output.data.cpu().numpy().copy()
          #Reshape numpy array to (num_crop,num_segments,num_classes)
          output_np = output_np.reshape((num_crop, test_segments, num_class))
          #Take mean of cropped images to be in shape (num_segments,1,num_classes)
          output_np = output_np.mean(axis=0).reshape((test_segments,1,num_class))
          output_np = output_np.mean(axis=0)
      return output_np   
    
    
  action_label = label_dic(args.classInd_file)

  if args.dataset == 'ucf101':
      num_class = 101
  else:
      raise ValueError('Unkown dataset: ' + args.dataset)
  
  model = TSN_model(num_class, 1, args.modality,
                    base_model_name=args.arch, consensus_type='avg', dropout=args.dropout)
  
  #load the weights of your model training
  checkpoint = torch.load(args.weights)
  print("epoch {}, best acc1@: {}" .format(checkpoint['epoch'], checkpoint['best_acc1']))

  base_dict = {'.'.join(k.split('.')[1:]): v for k,v in list(checkpoint['state_dict'].items())}
  model.load_state_dict(base_dict)
  
  #test_crops is set to 1 for fast video evaluation
  if args.test_crops == 1:
      cropping = torchvision.transforms.Compose([
          GroupScale(model.scale_size),
          GroupCenterCrop(model.input_size),
      ])
  elif args.test_crops == 10:
      cropping = torchvision.transforms.Compose([
          GroupOverSample(model.input_size, model.scale_size)
      ])
  else:
      raise ValueError("Only 1 and 10 crops are supported while we got {}".format(test_crops))
      
  #Required transformations
  transform = torchvision.transforms.Compose([
           cropping,
           Stack(roll=args.arch == 'BNInception'),
           ToTorchFormatTensor(div=args.arch != 'BNInception'),
           GroupNormalize(model.input_mean, model.input_std),
                   ])
    
    
  if args.gpus is not None:
      devices = [args.gpus[i] for i in range(args.workers)]
  else:
      devices = list(range(args.workers))
    
  model = torch.nn.DataParallel(model.cuda(devices[0]), device_ids=devices)
         
  model.eval()    

  softmax = torch.nn.Softmax()
  scores = torch.tensor(np.zeros((1,101)), dtype=torch.float32).cuda()
   
  frames = []  
  frame_count = 0 
  try: 
    top5_actions = Top_N(args.classInd_file)
    Tunnel_ = True
    conn,T_thr = Network.set_server(port=6666,Tunnel=Tunnel_,n=1)
    rcv_frames = Streaming.rcv_frames_thread(connection=conn[0])
    send_results = Streaming.send_results_thread(connection=conn[1])
    while (rcv_frames.isAlive() and send_results.isAlive()):
        frame,status = rcv_frames.get()
        if frame is 0:
          break
        frame_count += 1
        frame = Image.fromarray(frame)
        
        if args.modality == 'RGB':             
            frames.append(frame)
            if frame_count % 5 == 0 and frame_count != 0:
                frames = transform(frames).cuda()
                scores = eval_video(frames)
                scores = softmax(torch.FloatTensor(scores))
                scores = scores.data.cpu().numpy().copy()
                top5_actions.import_scores(scores[0,])
                indecies,_,scores = top5_actions.get_top_N_actions()
                send_results.put(status=status,scores=(*indecies,*scores))
                
                frames = []
            else:
                send_results.put(status=status)
  except (KeyboardInterrupt,IOError,OSError):
    pass
  finally:
    rcv_frames.close()
    send_results.close()
    conn[0].close()
    conn[1].close()