-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathextractSharpImages
More file actions
127 lines (99 loc) · 4.32 KB
/
Copy pathextractSharpImages
File metadata and controls
127 lines (99 loc) · 4.32 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
121
122
123
124
125
126
127
# USAGE
# python detect_blur.py -i images -p percentage to extract
"""
EG:
python /home/mark/Desktop/atk_usefulInfo/detecting-blur/detect_blur.py -i /home/mark/Desktop/atk_usefulInfo/detecting-blur/images -p50
"""
# import the necessary packagess
import argparse
import cv2
import shutil
import os
import operator
#------
#Paths from imutils https://github.com/jrosebr1/imutils/blob/master/imutils/paths.py
image_types = (".jpg", ".jpeg", ".png", ".bmp", ".tif", ".tiff")
def list_images(basePath, contains=None):
# return the set of files that are valid
return list_files(basePath, validExts=image_types, contains=contains)
def list_files(basePath, validExts=None, contains=None):
# loop over the directory structure
for (rootDir, dirNames, filenames) in os.walk(basePath):
# loop over the filenames in the current directory
for filename in filenames:
# if the contains string is not none and the filename does not contain
# the supplied string, then ignore the file
if contains is not None and filename.find(contains) == -1:
continue
# determine the file extension of the current file
ext = filename[filename.rfind("."):].lower()
# check to see if the file is an image and should be processed
if validExts is None or ext.endswith(validExts):
# construct the path to the image and yield it
imagePath = os.path.join(rootDir, filename)
yield imagePath
#-----
def percentage(percent, whole):
return (percent * whole) / 100.0
def copyFile(imagePath):
# Make a new folder called output+pc
# This line below is problematic if the percentage required has already been run:
# It creates an output_ folder in the already-made output_ folder.
extractParts = str(imagePath).split("/")
#print extractParts
folderName = "output_"+str(percentageArg)+"pc"
folderOnly = str(imagePath.split(extractParts[-1])[0])
folderToCreate = folderOnly+folderName
fileOnly = extractParts[-1]
outputPath = folderOnly+ "output_"+str(percentageArg)+"pc/"+fileOnly
if not os.path.exists(folderToCreate):
#print "Creating output folder: "+folderToCreate
os.makedirs(folderToCreate)
if not os.path.exists(outputPath):
shutil.copy(imagePath,outputPath)
print("Copied: "+ imagePath)
else:
print "File exists!"
return
def variance_of_laplacian(image):
# compute the Laplacian of the image and then return the focus
# measure, which is simply the variance of the Laplacian
return cv2.Laplacian(image, cv2.CV_64F).var()
#--------------------- code starts here
# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-i", "--images", required=True,
help="Path to input directory of images.")
#If reintroducing threshold option:
#ap.add_argument("-t", "--threshold", type=float, default=100.0,
# help="Focus measures that fall below this value will be considered 'blurry'.")
ap.add_argument("-p", "--percentage", required=True,
help="Percentage of images to return.")
args = vars(ap.parse_args())
# calculate the number of files to extract
imagesList = list(list_images(args["images"]))
numberOfFiles = len(imagesList)
percentageArg = int(args["percentage"])
numberToCopy = int(percentage(percentageArg, numberOfFiles))
print "Number of files "+str(numberOfFiles)
print "Percentage of best images to extract: "+str(args["percentage"])
print "Number of files to copy "+str(int(percentage(percentageArg, numberOfFiles)))
print "Reading folder contents..."
imagesDict = {}
#print imagesDict
# loop over the input images
for imagePath in list_images(args["images"]):
# load the image, convert it to grayscale, and compute the
# focus measure of the image using the Variance of Laplacian method
image = cv2.imread(imagePath)
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
fm = variance_of_laplacian(gray)
text = "Not Blurry"
imagesDict[imagePath] = fm
sortedByWorstToLeastBlurry = sorted(imagesDict.items(), key=operator.itemgetter(1))
filesToCopy = sortedByWorstToLeastBlurry[-numberToCopy:numberOfFiles]
# Extract just the str, to get a list of only the filepaths to copy
pathsOnly = [((t[0],)+t[2:]) for t in filesToCopy]
for each in pathsOnly:
each = str(each[0])
copyFile(each)