Data hosted with ♥ by Pastebin.com - Download Raw - See Original
  1. # Color frames (ImgFrame), object detection (ImgDetections) and age/gender recognition (NNData)
  2. # messages arrive to the host all with some additional delay.
  3. # For each ImgFrame there's one ImgDetections msg, which has multiple detections, and for each
  4. # detection there's a NNData msg which contains age/gender recognition results.
  5.  
  6. # How it works:
  7. # Every ImgFrame, ImgDetections and NNData message has it's own sequence number, by which we can sync messages.
  8.  
  9. class TwoStageHostSeqSync:
  10.     def __init__(self):
  11.         self.msgs = {}
  12.     # name: color, detection, or recognition
  13.     def add_msg(self, msg, name):
  14.         seq = str(msg.getSequenceNum())
  15.         if seq not in self.msgs:
  16.             self.msgs[seq] = {} # Create directory for msgs
  17.         if "recognition" not in self.msgs[seq]:
  18.             self.msgs[seq]["recognition"] = [] # Create recognition array
  19.  
  20.         if "emotions" not in self.msgs[seq]:
  21.             self.msgs[seq]["emotions"] = [] # Create emotions array
  22.  
  23.         if name == "recognition":
  24.             # Append recognition msgs to an array
  25.             self.msgs[seq]["recognition"].append(msg)
  26.             # print(f'Added recognition seq {seq}, total len {len(self.msgs[seq]["recognition"])}')
  27.  
  28.         elif name == "emotions":
  29.             # Append emotions msgs to an array
  30.             self.msgs[seq]["emotions"].append(msg)
  31.             # print(f'Added emotions seq {seq}, total len {len(self.msgs[seq]["emotions"])}')
  32.  
  33.         elif name == "detection":
  34.             # Save detection msg in the directory
  35.             self.msgs[seq][name] = msg
  36.             self.msgs[seq]["len"] = len(msg.detections)
  37.             # print(f'Added detection seq {seq}')
  38.  
  39.         elif name == "color": # color
  40.             # Save color frame in the directory
  41.             self.msgs[seq][name] = msg
  42.             # print(f'Added frame seq {seq}')
  43.  
  44.  
  45.     def get_msgs(self):
  46.         seq_remove = [] # Arr of sequence numbers to get deleted
  47.  
  48.         for seq, msgs in self.msgs.items():
  49.             seq_remove.append(seq) # Will get removed from dict if we find synced msgs pair
  50.  
  51.             # Check if we have both detections and color frame with this sequence number
  52.             if "color" in msgs and "len" in msgs:
  53.  
  54.                 # Check if all detected objects (faces) have finished recognition (age/gender) inference
  55.                
  56.                 if msgs["len"] == len(msgs["recognition"]) and msgs["len"] == len(msgs["emotions"]) :
  57.                     #print(f"Synced msgs with sequence number {seq}", msgs)
  58.                     # We have synced msgs, remove previous msgs (memory cleaning)
  59.                     for rm in seq_remove:
  60.                         del self.msgs[rm]
  61.  
  62.                     return msgs # Returned synced msgs
  63.  
  64.         return None # No synced msgs