add adjust_gamma

This commit is contained in:
千住柱間 2025-04-21 01:20:50 -04:00
commit 138037f512
Signed by: hashirama
GPG key ID: 53E62470A86BC185

View file

@ -93,11 +93,16 @@ class TextProcessor:
self.vocab[idx] if 1 <= idx <= len(self.vocab) else self.unk_token
for idx in indices
)
almost_done = self._remove_specials(raw_text)
final_text = post_process(almost_done)
final_text = self._remove_specials(raw_text)
return final_text
def adjust_gamma(image, gamma=1.0):
invGamma = 1.0 / gamma
table = np.array([((i / 255.0) ** invGamma) * 255 for i in np.arange(0, 256)]).astype("uint8")
return cv2.LUT(image, table)
@ -106,13 +111,13 @@ def preprocess_image(image_path):
gray = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
# Convert grayscale to 3-channel RGB by duplicating the gray channel
img = cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB)
img = adjust_gamma(cv2.cvtColor(gray, cv2.COLOR_GRAY2RGB),gamma=2.2)
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] * 1
norm_vals = [1/255, 1/255, 1/255] * 1
norm_vals = [1/255, 1/255, 1/255] * 3
mat_in.substract_mean_normalize(mean_vals, norm_vals)
return mat_in
@ -145,12 +150,6 @@ def decode_tokens(logits, tokenizer_path="model"):
return tkz.decode(token_ids, skip_special_tokens=True)
def post_process(text):
text = "".join(text.split()).replace("", "...")
text = re.sub("[・.]{2,}", lambda x: (x.end() - x.start()) * ".", text)
return jaconv.h2z(text, ascii=True, digit=True)
def main(image_path):
mat_in = preprocess_image(image_path)
logits = run_inference(mat_in)