Optimized the code in detect-blank-pages.py
Made use of Numpy arrays
This commit is contained in:
parent
4e991e7ec2
commit
b7f62a635d
1 changed files with 8 additions and 18 deletions
|
@ -1,6 +1,7 @@
|
||||||
import cv2
|
import cv2
|
||||||
import sys
|
import sys
|
||||||
import argparse
|
import argparse
|
||||||
|
import numpy as np
|
||||||
|
|
||||||
def is_blank_image(image_path, threshold=10, white_percent=99, white_value=255, blur_size=5):
|
def is_blank_image(image_path, threshold=10, white_percent=99, white_value=255, blur_size=5):
|
||||||
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
image = cv2.imread(image_path, cv2.IMREAD_GRAYSCALE)
|
||||||
|
@ -15,19 +16,11 @@ def is_blank_image(image_path, threshold=10, white_percent=99, white_value=255,
|
||||||
_, thresholded_image = cv2.threshold(blurred_image, white_value - threshold, white_value, cv2.THRESH_BINARY)
|
_, thresholded_image = cv2.threshold(blurred_image, white_value - threshold, white_value, cv2.THRESH_BINARY)
|
||||||
|
|
||||||
# Calculate the percentage of white pixels in the thresholded image
|
# Calculate the percentage of white pixels in the thresholded image
|
||||||
white_pixels = 0
|
white_pixels = np.sum(thresholded_image == white_value)
|
||||||
total_pixels = thresholded_image.size
|
white_pixel_percentage = (white_pixels / thresholded_image.size) * 100
|
||||||
for i in range(0, thresholded_image.shape[0], 2):
|
|
||||||
for j in range(0, thresholded_image.shape[1], 2):
|
|
||||||
if thresholded_image[i, j] == white_value:
|
|
||||||
white_pixels += 1
|
|
||||||
white_pixel_percentage = (white_pixels / (i * thresholded_image.shape[1] + j + 1)) * 100
|
|
||||||
if white_pixel_percentage < white_percent:
|
|
||||||
return False
|
|
||||||
|
|
||||||
print(f"Page has white pixel percent of {white_pixel_percentage}")
|
print(f"Page has white pixel percent of {white_pixel_percentage}")
|
||||||
return True
|
return white_pixel_percentage >= white_percent
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
|
@ -39,9 +32,6 @@ if __name__ == "__main__":
|
||||||
|
|
||||||
blank = is_blank_image(args.image_path, args.threshold, args.white_percent)
|
blank = is_blank_image(args.image_path, args.threshold, args.white_percent)
|
||||||
|
|
||||||
if blank:
|
|
||||||
# Return code 1: The image is considered blank.
|
# Return code 1: The image is considered blank.
|
||||||
sys.exit(1)
|
|
||||||
else:
|
|
||||||
# Return code 0: The image is not considered blank.
|
# Return code 0: The image is not considered blank.
|
||||||
sys.exit(0)
|
sys.exit(int(blank))
|
||||||
|
|
Loading…
Reference in a new issue