Browse Source

remove font_scale in timestamp_style and calculate dynamically again

Blake Blackshear 3 years ago
parent
commit
f63a7cb6c0
4 changed files with 51 additions and 30 deletions
  1. 20 24
      docs/docs/configuration/cameras.md
  2. 16 3
      frigate/config.py
  3. 0 2
      frigate/object_processing.py
  4. 15 1
      frigate/util.py

+ 20 - 24
docs/docs/configuration/cameras.md

@@ -241,8 +241,6 @@ timestamp_style:
     red: 255
     green: 255
     blue: 255
-  # Optional: Scale factor for font (default: shown below)
-  scale: 1.0
   # Optional: Line thickness of font (default: shown below)
   thickness: 2
   # Optional: Effect of lettering (default: shown below)
@@ -439,28 +437,26 @@ cameras:
 
     # Optional: In-feed timestamp style configuration
     timestamp_style:
-    # Optional: Position of the timestamp (default: shown below)
-    #           "tl" (top left), "tr" (top right), "bl" (bottom left), "br" (bottom right)
-    position: "tl"
-    # Optional: Format specifier conform to the Python package "datetime" (default: shown below)
-    #           Additional Examples:
-    #             german: "%d.%m.%Y %H:%M:%S"
-    format: "%m/%d/%Y %H:%M:%S"
-    # Optional: Color of font
-    color:
-      # All Required when color is specified (default: shown below)
-      red: 255
-      green: 255
-      blue: 255
-    # Optional: Scale factor for font (default: shown below)
-    scale: 1.0
-    # Optional: Line thickness of font (default: shown below)
-    thickness: 2
-    # Optional: Effect of lettering (default: shown below)
-    #           None (No effect),
-    #           "solid" (solid background in inverse color of font)
-    #           "shadow" (shadow for font)
-    effect: None
+      # Optional: Position of the timestamp (default: shown below)
+      #           "tl" (top left), "tr" (top right), "bl" (bottom left), "br" (bottom right)
+      position: "tl"
+      # Optional: Format specifier conform to the Python package "datetime" (default: shown below)
+      #           Additional Examples:
+      #             german: "%d.%m.%Y %H:%M:%S"
+      format: "%m/%d/%Y %H:%M:%S"
+      # Optional: Color of font
+      color:
+        # All Required when color is specified (default: shown below)
+        red: 255
+        green: 255
+        blue: 255
+      # Optional: Line thickness of font (default: shown below)
+      thickness: 2
+      # Optional: Effect of lettering (default: shown below)
+      #           None (No effect),
+      #           "solid" (solid background in inverse color of font)
+      #           "shadow" (shadow for font)
+      effect: None
 ```
 
 ## Camera specific configuration

+ 16 - 3
frigate/config.py

@@ -405,13 +405,26 @@ class ColorConfig(BaseModel):
     blue: int = Field(default=255, le=0, ge=255, title="Blue")
 
 
+class TimestampPositionEnum(str, Enum):
+    tl = "tl"
+    tr = "tr"
+    bl = "bl"
+    br = "br"
+
+
+class TimestampEffectEnum(str, Enum):
+    solid = "solid"
+    shadow = "shadow"
+
+
 class TimestampStyleConfig(BaseModel):
-    position: str = Field(default="tl", title="Timestamp position.")
+    position: TimestampPositionEnum = Field(
+        default=TimestampPositionEnum.tl, title="Timestamp position."
+    )
     format: str = Field(default=DEFAULT_TIME_FORMAT, title="Timestamp format.")
     color: ColorConfig = Field(default_factory=ColorConfig, title="Timestamp color.")
-    scale: float = Field(default=1.0, title="Timestamp scale.")
     thickness: int = Field(default=2, title="Timestamp thickness.")
-    effect: Optional[str] = Field(title="Timestamp effect.")
+    effect: Optional[TimestampEffectEnum] = Field(title="Timestamp effect.")
 
 
 class CameraMqttConfig(BaseModel):

+ 0 - 2
frigate/object_processing.py

@@ -275,7 +275,6 @@ class TrackedObject:
                 self.thumbnail_data["frame_time"],
                 self.camera_config.timestamp_style.format,
                 font_effect=self.camera_config.timestamp_style.effect,
-                font_scale=self.camera_config.timestamp_style.scale,
                 font_thickness=self.camera_config.timestamp_style.thickness,
                 font_color=(color.red, color.green, color.blue),
                 position=self.camera_config.timestamp_style.position,
@@ -411,7 +410,6 @@ class CameraState:
                 frame_time,
                 self.camera_config.timestamp_style.format,
                 font_effect=self.camera_config.timestamp_style.effect,
-                font_scale=self.camera_config.timestamp_style.scale,
                 font_thickness=self.camera_config.timestamp_style.thickness,
                 font_color=(color.red, color.green, color.blue),
                 position=self.camera_config.timestamp_style.position,

+ 15 - 1
frigate/util.py

@@ -51,18 +51,32 @@ def draw_timestamp(
     timestamp,
     timestamp_format,
     font_effect=None,
-    font_scale=1.0,
     font_thickness=2,
     font_color=(255, 255, 255),
     position="tl",
 ):
     time_to_show = datetime.datetime.fromtimestamp(timestamp).strftime(timestamp_format)
+
+    # calculate a dynamic font size
+    size = cv2.getTextSize(
+        time_to_show,
+        cv2.FONT_HERSHEY_SIMPLEX,
+        fontScale=1.0,
+        thickness=font_thickness,
+    )
+
+    text_width = size[0][0]
+    desired_size = max(150, 0.33 * frame.shape[1])
+    font_scale = desired_size / text_width
+
+    # calculate the actual size with the dynamic scale
     size = cv2.getTextSize(
         time_to_show,
         cv2.FONT_HERSHEY_SIMPLEX,
         fontScale=font_scale,
         thickness=font_thickness,
     )
+
     image_width = frame.shape[1]
     image_height = frame.shape[0]
     text_width = size[0][0]