debug option

This commit is contained in:
千住柱間 2025-04-21 19:41:19 -04:00
commit 6bc878930d
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -13,40 +13,13 @@ import fnmatch
import glob
import subprocess
from collections import Counter
import matplotlib.pyplot as plt
import re
import numpy as np
WATCH_DIR = "/tmp/mote-ocr-screenshots/"
def deconvolve_image_path(input_path, psf_size=5, iterations=10, output_path=None):
"""
Reads the image at input_path, applies RL deconvolution, and
returns an 8bit numpy array. Optionally saves to output_path.
"""
img = io.imread(input_path)
if img.ndim == 3:
gray = color.rgb2gray(img)
else:
gray = img_as_float(img)
psf = np.ones((psf_size, psf_size), dtype=float)
psf /= psf.sum()
deconv = richardson_lucy(gray, psf, iterations=iterations)
deconv_u8 = img_as_ubyte(np.clip(deconv, 0, 1))
if output_path:
io.imsave(output_path, deconv_u8)
return deconv_u8
def deconvolve_image_array(gray_float, psf_size=5, iterations=10):
"""
Takes a grayscale float image in [0,1], runs RL deconv, and
returns an 8bit numpy array.
"""
psf = np.ones((psf_size, psf_size), dtype=float)
psf /= psf.sum()
deconv = richardson_lucy(gray_float, psf, iterations=iterations)
return img_as_ubyte(np.clip(deconv, 0, 1))
def contrast_stretch(gray):
p2, p98 = np.percentile(gray, (2, 98))
@ -173,13 +146,17 @@ def preprocess_image(image_path):
gray = enhance_text(image_path)
resampled = bicubic(gray)
# Convert grayscale to 3-channel RGB by duplicating the gray channel
img = contrast_stretch(adjust_gamma(cv2.cvtColor(resampled, cv2.COLOR_GRAY2RGB),gamma=1,alpha=1.65,beta=-1)) # perfect tunning
img = contrast_stretch(adjust_gamma(cv2.cvtColor(resampled, cv2.COLOR_GRAY2RGB),gamma=1.68,alpha=1.75,beta=-5)) # perfect tunning
plt.imshow(img)
plt.axis("off")
plt.savefig("/tmp/output.png", bbox_inches="tight", pad_inches=0)
mat_in = ncnn.Mat.from_pixels_resize(
img, ncnn.Mat.PixelType.PIXEL_RGB, img.shape[1], img.shape[0], 224, 224
)
mean_vals = [0.5, 0.5, 0.5] * 4
norm_vals = [1/255, 1/255, 1/255] * 4
mean_vals = [0.5] * 3
norm_vals = [1/255] * 3
mat_in.substract_mean_normalize(mean_vals, norm_vals)
return mat_in