forked from norkator/open-intelligence
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSimilarityProcess.py
More file actions
120 lines (99 loc) · 4.03 KB
/
SimilarityProcess.py
File metadata and controls
120 lines (99 loc) · 4.03 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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
from skimage.metrics import structural_similarity as ssim
from pathlib import Path
from module import database, process_utils
from objects import SrFile
import psycopg2
import shutil
import os
import cv2
import sys
import time
# Parse configs
app_config = database.get_application_config()
output_root_folder_path = database.find_config_value(app_config, 'output_folder')
process_sleep_seconds = database.find_config_value(app_config, 'process_sleep_seconds')
delete_files = database.find_config_value(app_config, 'delete_files') == 'True'
# Define paths
test_move_path = output_root_folder_path + 'recycle/'
# Check directory existence
Path(test_move_path).mkdir(parents=True, exist_ok=True)
# Similarity level
similarity_level = 0.30
# Image compare process
def image_is_similar(image_a, image_b, file_name):
# compute the mean squared error and structural similarity
# m = mse(image_a, image_b)
if image_a is not None or image_b is not None:
s = ssim(image_a, image_b)
print('Similarity: ' + str(s) + (' del ' + file_name if s > similarity_level else ''))
return True if s > similarity_level else False
else:
return False
def app():
# Do work
records = database.get_images_for_similarity_check_process()
similarity_image_objects = []
for row in records:
# Get db row fields
id = row[0]
label = row[1]
cropped_file_name = row[2]
# Construct paths
input_image = output_root_folder_path + label + '/' + cropped_file_name
# Make objects
similarity_image_object = SrFile.SrFile(id, label, cropped_file_name, input_image, None, None, None)
similarity_image_objects.append(similarity_image_object)
# Super resolution image
if len(similarity_image_objects) > 0:
index = 0
img1 = None
img2 = None
for similarity_image_object in similarity_image_objects:
try:
if index % 2:
img1 = cv2.imread(similarity_image_object.input_image)
img1 = cv2.resize(img1, (200, 200))
img1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
if image_is_similar(img1, img2, similarity_image_object.image_name):
if delete_files:
os.remove(similarity_image_object.input_image)
else:
shutil.move(similarity_image_object.input_image,
test_move_path + similarity_image_object.image_name)
try:
# Try delete also super resolution image
os.remove(
output_root_folder_path + similarity_image_object.label + '/super_resolution/' +
similarity_image_object.image_name)
except Exception as e:
pass
else:
img2 = cv2.imread(similarity_image_object.input_image)
img2 = cv2.resize(img2, (200, 200))
img2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
except Exception as e:
print(e)
# Increment as last
index = index + 1
# Write database, row no longer processed later
database.update_similarity_check_row_checked(similarity_image_object.id)
else:
print('No similar images to process')
# ---------------------------------------------------------------------
# Keeps program running
def main_loop():
while 1:
try:
process_utils.set_instance_status()
app()
print('... running')
except psycopg2.OperationalError as e:
print(e)
time.sleep(int(process_sleep_seconds))
if __name__ == '__main__':
try:
main_loop()
except KeyboardInterrupt:
print >> sys.stderr, '\nExiting by user request.\n'
sys.exit(0)
# ---------------------------------------------------------------------