feat: round output JSON floats to 2dp + unify ensure_ascii for asr/asrx

This commit is contained in:
Accusys
2026-08-16 06:38:39 +08:00
parent 39e9ce4c3b
commit 5e6a7b7d51
18 changed files with 249 additions and 45 deletions
+27 -1
View File
@@ -401,6 +401,30 @@ pub struct FaceTraceProfile {
pub status: String, // pending / bound / merged
}
/// Recursively round all float numbers in a JSON value to 2 decimal places.
/// Used for trace_profile.json output only; in-memory values are preserved.
pub(crate) fn round_json_value(v: &serde_json::Value) -> serde_json::Value {
match v {
serde_json::Value::Number(n) => {
if let Some(f) = n.as_f64() {
let rounded = (f * 100.0).round() / 100.0;
serde_json::Value::from(rounded)
} else {
v.clone()
}
}
serde_json::Value::Array(items) => {
serde_json::Value::Array(items.iter().map(round_json_value).collect())
}
serde_json::Value::Object(map) => serde_json::Value::Object(
map.iter()
.map(|(k, val)| (k.clone(), round_json_value(val)))
.collect(),
),
_ => v.clone(),
}
}
impl FaceTraceProfile {
/// Directory: `output/{file_uuid}/{trace_id}/`
pub fn trace_dir(output_dir: &str, file_uuid: &str, trace_id: i64) -> PathBuf {
@@ -488,7 +512,8 @@ impl FaceTraceProfile {
// Write trace_profile.json
let json =
serde_json::to_string_pretty(self).context("Failed to serialize FaceTraceProfile")?;
serde_json::to_string_pretty(&round_json_value(&serde_json::to_value(&*self)?))
.context("Failed to serialize FaceTraceProfile")?;
let profile_path = Self::profile_path(output_dir, &self.file_uuid, self.trace_id);
std::fs::write(&profile_path, json)
.with_context(|| format!("Failed to write {}", profile_path.display()))?;
@@ -636,6 +661,7 @@ mod tests {
profile.face_filter = Some(FaceFilterSettings {
min_confidence: 85.0,
min_size: 96,
min_capture_quality: 0.0,
});
let json = serde_json::to_string(&profile).unwrap();
let restored: FileProfile = serde_json::from_str(&json).unwrap();
+20 -5
View File
@@ -10,6 +10,7 @@ const CUT_TIMEOUT: Duration = Duration::from_secs(3600);
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct CutResult {
pub frame_count: u64,
#[serde(serialize_with = "round_two")]
pub fps: f64,
pub scenes: Vec<CutScene>,
}
@@ -19,10 +20,21 @@ pub struct CutScene {
pub scene_number: u32,
pub start_frame: u64,
pub end_frame: u64,
#[serde(serialize_with = "round_two")]
pub start_time: f64,
#[serde(serialize_with = "round_two")]
pub end_time: f64,
}
/// Serialize an f64 rounded to 2 decimal places (JSON output only).
/// In-memory values are preserved; only the serialized form is rounded.
fn round_two<S>(v: &f64, serializer: S) -> Result<S::Ok, S::Error>
where
S: serde::Serializer,
{
serializer.serialize_f64((v * 100.0).round() / 100.0)
}
pub async fn process_cut(
video_path: &str,
output_path: &str,
@@ -157,8 +169,11 @@ fn try_native_cut(video_path: &str) -> Result<CutResult> {
// Step 3: Build scenes from cut points
let mut scenes: Vec<CutScene> = Vec::new();
let round_time = |t: f64| (t * 1000.0).round() / 1000.0;
let mut prev_time = 0.0;
let mut prev_frame: u64 = 0;
let round_time = |t: f64| (t * 1000.0).round() / 1000.0;
for (i, &cut_time) in scene_times.iter().enumerate() {
let end_frame = (cut_time * fps).round() as u64;
@@ -169,8 +184,8 @@ fn try_native_cut(video_path: &str) -> Result<CutResult> {
scene_number: (i + 1) as u32,
start_frame: prev_frame,
end_frame: end_frame.saturating_sub(1),
start_time: prev_time,
end_time: cut_time - (1.0 / fps),
start_time: round_time(prev_time),
end_time: round_time(cut_time - (1.0 / fps)),
});
}
@@ -184,8 +199,8 @@ fn try_native_cut(video_path: &str) -> Result<CutResult> {
scene_number: (scenes.len() + 1) as u32,
start_frame: prev_frame,
end_frame: total_frames.saturating_sub(1),
start_time: prev_time,
end_time: duration,
start_time: round_time(prev_time),
end_time: round_time(duration),
});
}
@@ -196,7 +211,7 @@ fn try_native_cut(video_path: &str) -> Result<CutResult> {
start_frame: 0,
end_frame: total_frames.saturating_sub(1),
start_time: 0.0,
end_time: duration,
end_time: round_time(duration),
});
}
+5 -1
View File
@@ -1564,7 +1564,11 @@ async fn build_face_track_nodes_from_qdrant(
if kface_path.exists() {
trace_profile.key_face = Some("key_face.jpg".to_string());
}
if let Ok(json) = serde_json::to_string_pretty(&trace_profile) {
if let Ok(json) = serde_json::to_string_pretty(
&crate::core::file_profile::round_json_value(
&serde_json::to_value(&trace_profile).unwrap_or_default(),
),
) {
let _ = std::fs::write(&tp_path, json);
if tp_path.exists() {
tracing::info!("[TKG-VERIFY] trace {}: trace_profile.json rewritten", tid);