first commit
This commit is contained in:
commit
253206e6c5
8 files changed
+2960
No files matched your search
@@ -0,0 +1,4 @@
|
|||||||
|
/target
|
||||||
|
.env
|
||||||
|
/models
|
||||||
|
/debug
|
||||||
Generated
+2710
File diff suppressed because it is too large.
Load diff
+13
@@ -0,0 +1,13 @@
|
|||||||
|
[package]
|
||||||
|
name = "watermeter_watcher"
|
||||||
|
version = "0.1.0"
|
||||||
|
edition = "2024"
|
||||||
|
|
||||||
|
[dependencies]
|
||||||
|
anyhow = "1.0.104"
|
||||||
|
dotenvy = "0.15.7"
|
||||||
|
image = "0.25.10"
|
||||||
|
ocrs = "0.13.1"
|
||||||
|
reqwest = { version = "0.13.5", features = ["blocking"] }
|
||||||
|
rten = "0.26.0"
|
||||||
|
rten-imageproc = "0.26.0"
|
||||||
Executable
+6
@@ -0,0 +1,6 @@
|
|||||||
|
#!/bin/sh
|
||||||
|
# Download the ocrs text recognition model into ./models (or $OCRS_MODELS_DIR).
|
||||||
|
set -e
|
||||||
|
DIR="${OCRS_MODELS_DIR:-models}"
|
||||||
|
mkdir -p "$DIR"
|
||||||
|
curl -fL "https://ocrs-models.s3-accelerate.amazonaws.com/text-recognition.onnx" -o "$DIR/text-recognition.onnx"
|
||||||
@@ -0,0 +1,82 @@
|
|||||||
|
use image::RgbImage;
|
||||||
|
|
||||||
|
/// Minimum blue channel value for an overlay pixel.
|
||||||
|
const BLUE_MIN: u8 = 150;
|
||||||
|
/// How much the blue channel must exceed red and green.
|
||||||
|
const BLUE_MARGIN: i16 = 60;
|
||||||
|
/// Components smaller than this (in pixels of their bounding box side) are noise.
|
||||||
|
const MIN_SIDE: u32 = 10;
|
||||||
|
/// Pixels trimmed from each side of a box to drop the blue outline.
|
||||||
|
const BORDER: u32 = 3;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone, Copy)]
|
||||||
|
pub struct BBox {
|
||||||
|
pub x: u32,
|
||||||
|
pub y: u32,
|
||||||
|
pub w: u32,
|
||||||
|
pub h: u32,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl BBox {
|
||||||
|
/// The box shrunk by `BORDER` on each side, i.e. the area inside the outline.
|
||||||
|
pub fn interior(&self) -> Option<BBox> {
|
||||||
|
(self.w > 2 * BORDER && self.h > 2 * BORDER).then(|| BBox {
|
||||||
|
x: self.x + BORDER,
|
||||||
|
y: self.y + BORDER,
|
||||||
|
w: self.w - 2 * BORDER,
|
||||||
|
h: self.h - 2 * BORDER,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_overlay_blue(p: &image::Rgb<u8>) -> bool {
|
||||||
|
let [r, g, b] = p.0.map(i16::from);
|
||||||
|
b >= i16::from(BLUE_MIN) && b - r >= BLUE_MARGIN && b - g >= BLUE_MARGIN
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Find the blue overlay rectangles, sorted left to right.
|
||||||
|
pub fn find_blue_boxes(img: &RgbImage) -> Vec<BBox> {
|
||||||
|
let (w, h) = img.dimensions();
|
||||||
|
let idx = |x: u32, y: u32| (y * w + x) as usize;
|
||||||
|
let mask: Vec<bool> = img.pixels().map(is_overlay_blue).collect();
|
||||||
|
let mut seen = vec![false; mask.len()];
|
||||||
|
let mut boxes = Vec::new();
|
||||||
|
let mut stack = Vec::new();
|
||||||
|
|
||||||
|
for y in 0..h {
|
||||||
|
for x in 0..w {
|
||||||
|
if !mask[idx(x, y)] || seen[idx(x, y)] {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let (mut x0, mut y0, mut x1, mut y1) = (x, y, x, y);
|
||||||
|
seen[idx(x, y)] = true;
|
||||||
|
stack.push((x, y));
|
||||||
|
while let Some((cx, cy)) = stack.pop() {
|
||||||
|
x0 = x0.min(cx);
|
||||||
|
y0 = y0.min(cy);
|
||||||
|
x1 = x1.max(cx);
|
||||||
|
y1 = y1.max(cy);
|
||||||
|
for dy in -1i32..=1 {
|
||||||
|
for dx in -1i32..=1 {
|
||||||
|
let (nx, ny) = (cx as i32 + dx, cy as i32 + dy);
|
||||||
|
if nx < 0 || ny < 0 || nx >= w as i32 || ny >= h as i32 {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let i = idx(nx as u32, ny as u32);
|
||||||
|
if mask[i] && !seen[i] {
|
||||||
|
seen[i] = true;
|
||||||
|
stack.push((nx as u32, ny as u32));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
let b = BBox { x: x0, y: y0, w: x1 - x0 + 1, h: y1 - y0 + 1 };
|
||||||
|
if b.w >= MIN_SIDE && b.h >= MIN_SIDE {
|
||||||
|
boxes.push(b);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
boxes.sort_by_key(|b| b.x);
|
||||||
|
boxes
|
||||||
|
}
|
||||||
@@ -0,0 +1,11 @@
|
|||||||
|
use anyhow::{Context, Result};
|
||||||
|
use image::DynamicImage;
|
||||||
|
|
||||||
|
/// Download the meter picture and decode it.
|
||||||
|
pub fn fetch_image(url: &str) -> Result<DynamicImage> {
|
||||||
|
let bytes = reqwest::blocking::get(url)
|
||||||
|
.and_then(|r| r.error_for_status())
|
||||||
|
.with_context(|| format!("failed to download {url}"))?
|
||||||
|
.bytes()?;
|
||||||
|
image::load_from_memory(&bytes).context("failed to decode image")
|
||||||
|
}
|
||||||
+82
@@ -0,0 +1,82 @@
|
|||||||
|
mod boxes;
|
||||||
|
mod fetch;
|
||||||
|
mod ocr;
|
||||||
|
|
||||||
|
use std::path::PathBuf;
|
||||||
|
use std::process::ExitCode;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use image::{Rgb, RgbImage};
|
||||||
|
|
||||||
|
use boxes::{BBox, find_blue_boxes};
|
||||||
|
use ocr::DigitReader;
|
||||||
|
|
||||||
|
const DEBUG_DIR: &str = "debug";
|
||||||
|
|
||||||
|
fn main() -> ExitCode {
|
||||||
|
match run() {
|
||||||
|
Ok(true) => ExitCode::SUCCESS,
|
||||||
|
Ok(false) => ExitCode::from(2),
|
||||||
|
Err(e) => {
|
||||||
|
eprintln!("error: {e:#}");
|
||||||
|
ExitCode::FAILURE
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Returns whether every box could be read.
|
||||||
|
fn run() -> Result<bool> {
|
||||||
|
dotenvy::dotenv().ok();
|
||||||
|
let debug = std::env::args().skip(1).any(|a| a == "--debug");
|
||||||
|
let url = std::env::var("WATERMETER_URL").context("WATERMETER_URL is not set")?;
|
||||||
|
let models_dir = std::env::var_os("OCRS_MODELS_DIR")
|
||||||
|
.map(PathBuf::from)
|
||||||
|
.unwrap_or_else(|| PathBuf::from("models"));
|
||||||
|
|
||||||
|
let img = fetch::fetch_image(&url)?.into_rgb8();
|
||||||
|
let boxes: Vec<BBox> = find_blue_boxes(&img)
|
||||||
|
.iter()
|
||||||
|
.filter_map(BBox::interior)
|
||||||
|
.collect();
|
||||||
|
anyhow::ensure!(!boxes.is_empty(), "no blue box found in image");
|
||||||
|
|
||||||
|
let reader = DigitReader::new(&models_dir)?;
|
||||||
|
let digits = reader.read_boxes(&img, &boxes)?;
|
||||||
|
|
||||||
|
if debug {
|
||||||
|
save_debug(&img, &boxes, &digits)?;
|
||||||
|
}
|
||||||
|
|
||||||
|
let reading: String = digits
|
||||||
|
.iter()
|
||||||
|
.map(|d| d.as_deref().unwrap_or("?"))
|
||||||
|
.collect();
|
||||||
|
println!("{reading}");
|
||||||
|
Ok(digits.iter().all(Option::is_some))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn save_debug(img: &RgbImage, boxes: &[BBox], digits: &[Option<String>]) -> Result<()> {
|
||||||
|
std::fs::create_dir_all(DEBUG_DIR)?;
|
||||||
|
let mut annotated = img.clone();
|
||||||
|
for (i, (b, d)) in boxes.iter().zip(digits).enumerate() {
|
||||||
|
image::imageops::crop_imm(img, b.x, b.y, b.w, b.h)
|
||||||
|
.to_image()
|
||||||
|
.save(format!("{DEBUG_DIR}/box_{i}.png"))?;
|
||||||
|
draw_rect(&mut annotated, b, Rgb([255, 0, 0]));
|
||||||
|
eprintln!("box {i}: {b:?} -> {}", d.as_deref().unwrap_or("?"));
|
||||||
|
}
|
||||||
|
annotated.save(format!("{DEBUG_DIR}/full.png"))?;
|
||||||
|
Ok(())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn draw_rect(img: &mut RgbImage, b: &BBox, color: Rgb<u8>) {
|
||||||
|
let (x1, y1) = (b.x + b.w - 1, b.y + b.h - 1);
|
||||||
|
for x in b.x..=x1 {
|
||||||
|
img.put_pixel(x, b.y, color);
|
||||||
|
img.put_pixel(x, y1, color);
|
||||||
|
}
|
||||||
|
for y in b.y..=y1 {
|
||||||
|
img.put_pixel(b.x, y, color);
|
||||||
|
img.put_pixel(x1, y, color);
|
||||||
|
}
|
||||||
|
}
|
||||||
+52
@@ -0,0 +1,52 @@
|
|||||||
|
use std::path::Path;
|
||||||
|
|
||||||
|
use anyhow::{Context, Result};
|
||||||
|
use image::RgbImage;
|
||||||
|
use ocrs::{ImageSource, OcrEngine, OcrEngineParams};
|
||||||
|
use rten::Model;
|
||||||
|
use rten_imageproc::{RectF, RotatedRect};
|
||||||
|
|
||||||
|
use crate::boxes::BBox;
|
||||||
|
|
||||||
|
pub struct DigitReader {
|
||||||
|
engine: OcrEngine,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl DigitReader {
|
||||||
|
pub fn new(models_dir: &Path) -> Result<Self> {
|
||||||
|
let rec_path = models_dir.join("text-recognition.onnx");
|
||||||
|
let recognition_model = Model::load_file(&rec_path)
|
||||||
|
.with_context(|| format!("failed to load {} (run ./download-models.sh)", rec_path.display()))?;
|
||||||
|
let engine = OcrEngine::new(OcrEngineParams {
|
||||||
|
recognition_model: Some(recognition_model),
|
||||||
|
allowed_chars: Some("0123456789".into()),
|
||||||
|
..Default::default()
|
||||||
|
})?;
|
||||||
|
Ok(Self { engine })
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Read the digits inside each box. Each box yields its recognized digits,
|
||||||
|
/// or `None` when nothing could be read.
|
||||||
|
///
|
||||||
|
/// Text detection is skipped: every box is fed directly to the recognizer
|
||||||
|
/// as a single line of text.
|
||||||
|
pub fn read_boxes(&self, img: &RgbImage, boxes: &[BBox]) -> Result<Vec<Option<String>>> {
|
||||||
|
let source = ImageSource::from_bytes(img.as_raw(), img.dimensions())?;
|
||||||
|
let input = self.engine.prepare_input(source)?;
|
||||||
|
let lines: Vec<Vec<RotatedRect>> = boxes
|
||||||
|
.iter()
|
||||||
|
.map(|b| {
|
||||||
|
let rect = RectF::from_tlhw(b.y as f32, b.x as f32, b.h as f32, b.w as f32);
|
||||||
|
vec![RotatedRect::from_rect(rect)]
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let texts = self.engine.recognize_text(&input, &lines)?;
|
||||||
|
Ok(texts
|
||||||
|
.into_iter()
|
||||||
|
.map(|line| {
|
||||||
|
line.map(|l| l.to_string().chars().filter(char::is_ascii_digit).collect::<String>())
|
||||||
|
.filter(|s| !s.is_empty())
|
||||||
|
})
|
||||||
|
.collect())
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user