remove font_scale in timestamp_style and calculate dynamically again

This commit is contained in:
Blake Blackshear 2021-09-04 16:34:48 -05:00
parent 7fc5297f60
commit f63a7cb6c0
4 changed files with 51 additions and 30 deletions

View File

@ -241,8 +241,6 @@ timestamp_style:
red: 255 red: 255
green: 255 green: 255
blue: 255 blue: 255
# Optional: Scale factor for font (default: shown below)
scale: 1.0
# Optional: Line thickness of font (default: shown below) # Optional: Line thickness of font (default: shown below)
thickness: 2 thickness: 2
# Optional: Effect of lettering (default: shown below) # Optional: Effect of lettering (default: shown below)
@ -452,8 +450,6 @@ cameras:
red: 255 red: 255
green: 255 green: 255
blue: 255 blue: 255
# Optional: Scale factor for font (default: shown below)
scale: 1.0
# Optional: Line thickness of font (default: shown below) # Optional: Line thickness of font (default: shown below)
thickness: 2 thickness: 2
# Optional: Effect of lettering (default: shown below) # Optional: Effect of lettering (default: shown below)

View File

@ -405,13 +405,26 @@ class ColorConfig(BaseModel):
blue: int = Field(default=255, le=0, ge=255, title="Blue") 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): 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.") format: str = Field(default=DEFAULT_TIME_FORMAT, title="Timestamp format.")
color: ColorConfig = Field(default_factory=ColorConfig, title="Timestamp color.") 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.") 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): class CameraMqttConfig(BaseModel):

View File

@ -275,7 +275,6 @@ class TrackedObject:
self.thumbnail_data["frame_time"], self.thumbnail_data["frame_time"],
self.camera_config.timestamp_style.format, self.camera_config.timestamp_style.format,
font_effect=self.camera_config.timestamp_style.effect, 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_thickness=self.camera_config.timestamp_style.thickness,
font_color=(color.red, color.green, color.blue), font_color=(color.red, color.green, color.blue),
position=self.camera_config.timestamp_style.position, position=self.camera_config.timestamp_style.position,
@ -411,7 +410,6 @@ class CameraState:
frame_time, frame_time,
self.camera_config.timestamp_style.format, self.camera_config.timestamp_style.format,
font_effect=self.camera_config.timestamp_style.effect, 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_thickness=self.camera_config.timestamp_style.thickness,
font_color=(color.red, color.green, color.blue), font_color=(color.red, color.green, color.blue),
position=self.camera_config.timestamp_style.position, position=self.camera_config.timestamp_style.position,

View File

@ -51,18 +51,32 @@ def draw_timestamp(
timestamp, timestamp,
timestamp_format, timestamp_format,
font_effect=None, font_effect=None,
font_scale=1.0,
font_thickness=2, font_thickness=2,
font_color=(255, 255, 255), font_color=(255, 255, 255),
position="tl", position="tl",
): ):
time_to_show = datetime.datetime.fromtimestamp(timestamp).strftime(timestamp_format) 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( size = cv2.getTextSize(
time_to_show, time_to_show,
cv2.FONT_HERSHEY_SIMPLEX, cv2.FONT_HERSHEY_SIMPLEX,
fontScale=font_scale, fontScale=font_scale,
thickness=font_thickness, thickness=font_thickness,
) )
image_width = frame.shape[1] image_width = frame.shape[1]
image_height = frame.shape[0] image_height = frame.shape[0]
text_width = size[0][0] text_width = size[0][0]