Merge in RR/cv-analysis from remove_pil to master
Squashed commit of the following:
commit 83c8d88f3d48404251470176c70979ee75ae068b
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Thu Jul 21 10:51:51 2022 +0200
remove deprecated server tests
commit cebc03b5399ac257a74036b41997201f882f5b74
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Thu Jul 21 10:51:08 2022 +0200
remove deprecated server tests
commit ce2845b0c51f001b7b5b8b195d6bf7e034ec4e39
Author: Julius Unverfehrt <julius.unverfehrt@iqser.com>
Date: Wed Jul 20 17:05:00 2022 +0200
repair tests to work without pillow WIP
commit 023fdab8322f28359a24c63e32635a3d0deccbe4
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Wed Jul 20 16:40:36 2022 +0200
fixed typo
commit 33850ca83a175f74789ae6b9bebd057ed84b7fb3
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Wed Jul 20 16:38:37 2022 +0200
fixed import from refactored open_img.py
commit dbc6d345f074e538948e2c4f94ebed8a5ef520bc
Author: Isaac Riley <Isaac.Riley@iqser.com>
Date: Wed Jul 20 16:32:42 2022 +0200
removed PIL from production code, now inly in scripts
30 lines
841 B
Python
30 lines
841 B
Python
from numpy import frombuffer, ndarray
|
|
import cv2
|
|
|
|
|
|
def preprocess_page_array(page):
|
|
if len(page.shape) > 2:
|
|
page = cv2.cvtColor(page, cv2.COLOR_BGR2GRAY)
|
|
page = cv2.fastNlMeansDenoising(page, h=3)
|
|
return page
|
|
|
|
|
|
def page2image(page):
|
|
|
|
if type(page) == bytes:
|
|
page = frombuffer(page)
|
|
elif type(page) == ndarray:
|
|
page = page
|
|
elif type(page) == str:
|
|
if page.lower().endswith((".png", ".jpg", ".jpeg")):
|
|
page = cv2.imread(page)
|
|
else:
|
|
raise IOError(
|
|
"PDFs are not a valid input type for cv-analysis."
|
|
" Use PNGs for tests and NumPy arrays for deployment."
|
|
)
|
|
else:
|
|
raise TypeError("Incompatible datatype. Expected bytes, numpy.ndarray, or path to an image file.")
|
|
|
|
return preprocess_page_array(page)
|