forked from ylevalle/Fingerprint-Enhancement-Python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain_enhancement.py
More file actions
executable file
·59 lines (49 loc) · 2.04 KB
/
main_enhancement.py
File metadata and controls
executable file
·59 lines (49 loc) · 2.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#! ./env/bin/python3
import os
from os.path import abspath, dirname, join, exists
import cv2
import numpy as np
from numpy import ndarray as NumpyArray
from src import image_enhance
ROOT = abspath(dirname(__file__))
IMG_FORMATS = ['jpg', 'png', 'jpeg']
IMGS_PATH = join(ROOT, 'images')
ENHANCED_PATH = join(ROOT, 'enhanced')
SUFFIX = '_enhanced.'
def image_enhance_from(image_file:str):
img: NumpyArray = cv2.imread(image_file)
if(len(img.shape)>2):
img = np.dot(img[...,:3], [0.299, 0.587, 0.114])
rows, cols = img.shape
aspect_ratio = rows / cols
new_rows: int = 350
new_cols: int = int(new_rows / aspect_ratio)
img: NumpyArray = cv2.resize(img, (new_cols, new_rows))
enhanced_img: NumpyArray = image_enhance(img)
enhanced_img = ((1 - enhanced_img) * 255).astype(np.uint8)
return enhanced_img
def main():
image_names = os.listdir(IMGS_PATH)
image_names = list(filter(lambda fname: fname.split('.')
[-1] in IMG_FORMATS, image_names))
print(f'Found {len(image_names)} images in "images" folder')
print('Processing images ...')
images_processed = False
for i, img_name in enumerate(image_names):
enhanced_img_name = img_name.split('.')
enhanced_img_name = enhanced_img_name[0] + SUFFIX + enhanced_img_name[-1]
if exists(join(ENHANCED_PATH, enhanced_img_name)):
print(f'{img_name} is already processed: ({i+1}/{len(image_names)})')
continue # if an image exists its enhanced, nothing is done
images_processed = True
print(f'Processing {img_name}: ({i+1}/{len(image_names)})')
enhanced_img = image_enhance_from(join(IMGS_PATH, img_name))
cv2.imwrite(join(ENHANCED_PATH, enhanced_img_name), enhanced_img)
if images_processed:
print('\tAll enhanced images saved in the "enhanced" folder.')
else:
print('\tNo image was processed, it probably already exists')
print('\tits enhanced versions in the "enhanced" folder.')
print('done.')
if __name__ == '__main__':
main()