def Log(self, proto): self._mutex.acquire() try: if not self._rewrite_uuid and not self._rewrite_timestamp: entry = proto else: entry = archive_entry_pb2.ArchiveEntry() entry.CopyFrom(proto) if self._rewrite_uuid: msbits = 0 lsbits = 0 uuid_bytes = uuid.uuid4().bytes for i in range(8): msbits = (msbits << 8) | uuid_bytes[i] lsbits = (lsbits << 8) | uuid_bytes[i + 8] if self._rewrite_timestamp: current_timestamp = time.time() entry.timestamp.seconds = int(math.floor(current_timestamp)) entry.timestamp.nanos = int( (current_timestamp - entry.timestamp.seconds) * 1000000000) if self._GetDateString() != self._last_date_string: self._CreateNewFile() self._WriteToOstream(entry) finally: self._mutex.release()
def testUniversalLogger(self): logger = UniversalLogger("hello/world") for i in range(100000): hello = archive_entry_pb2.ArchiveEntry() hello.string_entry = "Hello world for the {} time".format(i) logger.Log(hello) logger.Close()
def Read(self): header = archive_header_pb2.ArchiveHeader() header.ParseFromString(self._GetNextObject()) yield header while True: entry = archive_entry_pb2.ArchiveEntry() try: buf = self._GetNextObject() except (EOFError, IOError) as e: break if buf is None: break entry.ParseFromString(buf) yield entry
def LoopLogPsUtilStats(self): last_log_start_time = 0 while True: while time.time() - last_log_start_time < FLAGS.log_all_interval: time.sleep(1) last_log_start_time = time.time() stats_pb = self.GeneratePsUtilStatsProto() log_entry = archive_entry_pb2.ArchiveEntry() log_entry.psutil_stats.CopyFrom(stats_pb) logging.info("Log: %s", str(log_entry)) self._logger.Log(log_entry)
def _LogSmachLog(self, container_id, event, active_states=None, outcome=None): log_entry = archive_entry_pb2.ArchiveEntry() smach_log = log_entry.smach_log smach_log.container_id.extend(container_id) smach_log.event = event if active_states is not None: smach_log.active_states.extend(active_states) if outcome is not None: smach_log.outcome = outcome self._logger.Log(log_entry)
def LoopLogDockerStats(self): last_log_start_time = 0 while True: while time.time() - last_log_start_time < FLAGS.log_all_interval: time.sleep(1) last_log_start_time = time.time() try: for container in self._docker_client.containers.list(): stats = container.stats(stream=False, decode=True) try: stats_pb = self.DockerStatsToProto(stats) except KeyError as e: logging.error("DockerStatsToProto error: %s", str(e)) continue log_entry = archive_entry_pb2.ArchiveEntry() log_entry.docker_container_stats.CopyFrom(stats_pb) logging.info("Log: %s", str(log_entry)) self._logger.Log(log_entry) except docker.errors.DockerException as e: logging.error("Docker error: %s", str(e))
def GetEmbedding(self, request, context): response = openface_service_pb2.GetEmbeddingResponse() # Checks if the incoming image format is supported. supported_formats = (openface_common_pb2.BMP_IMAGE, openface_common_pb2.JPG_IMAGE) if request.image_format not in supported_formats: context.set_details("Image type not supported.") context.set_code(grpc.StatusCode.UNIMPLEMENTED) return response # Loads image. np_image_data = np.fromstring(request.image_data, np.uint8) cv_image = cv2.imdecode(np_image_data, cv2.CV_LOAD_IMAGE_COLOR) rgb_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB) image_size = rgb_image.shape[0] * rgb_image.shape[1] # Gets and sorts the bounding_boxs, the larger face will have smaller index. bounding_boxs = self._align.getAllFaceBoundingBoxes(rgb_image) bounding_boxs = sorted(bounding_boxs, key=lambda x: x.area(), reverse=True) for bounding_box in bounding_boxs: aligned_face = self._align.align( self._image_dim, rgb_image, bounding_box, landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE) if aligned_face is None: logging.error("Cannot align face.") continue size_ratio = float(bounding_box.area()) / image_size rep = self._net.forward(aligned_face) result_face = response.faces.add() result_face.embedding.extend(rep) result_face.face_size = size_ratio result_face.process_methods.append( openface_common_pb2. DLIB_ALIGN_SHAPE_PREDICTOR_68_LANDMARKS_OUTER_EYES_NOSE) result_face.process_methods.append( openface_common_pb2.OPENFACE_NET_FORWARD_NN4_SMALL2_V1_T7) # Write to log using UniversalLogger. if self._aligned_face_logger is not None: univseral_log = archive_entry_pb2.ArchiveEntry() univseral_log.aligned_face.process_methods.append( openface_common_pb2.DLIB_GET_ALL_FACE_BOUNDINGBOXES) univseral_log.aligned_face.process_methods.append( openface_common_pb2. DLIB_ALIGN_SHAPE_PREDICTOR_68_LANDMARKS_OUTER_EYES_NOSE) univseral_log.aligned_face.process_methods.append( openface_common_pb2.OPENFACE_NET_FORWARD_NN4_SMALL2_V1_T7) univseral_log.aligned_face.embedding.extend(rep) univseral_log.aligned_face.image_format = openface_common_pb2.JPG_IMAGE cv_bgrface = cv2.cvtColor(aligned_face, cv2.COLOR_RGB2BGR) _, buf = cv2.imencode(".jpg", cv_bgrface) univseral_log.aligned_face.image_data = buf.tostring() self._aligned_face_logger.Log(univseral_log) return response