-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathselectCardInteractive.py
More file actions
118 lines (93 loc) · 2.86 KB
/
Copy pathselectCardInteractive.py
File metadata and controls
118 lines (93 loc) · 2.86 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
import os
import sys
import math
import cv2
import random
def processCommandLine( argv ):
flagsOptions = {
}
if len( argv ) < 2:
print( "Usage: <src_img_dir>" )
exit( 0 )
# Get the command line parameters
returnValue = {
"srcImgDir": argv[ 1 ]
}
counter = 1
while ( len( argv ) > counter ):
if argv[ counter ] in flagsOptions:
try:
# If this is a boolean flag, meaning the next argument is unrelated
if returnValue[ flagsOptions[ argv[ counter ] ] ] == False:
returnValue[ flagsOptions[ argv[ counter ] ] ] = True
counter += 1
else:
returnValue[ flagsOptions[ argv[ counter ] ] ] = argv[ counter + 1 ]
counter += 2
except:
counter += 1
pass
else:
counter += 1
return returnValue
def randomize( numImgs ):
imgSequence = range( 0, numImgs )
random.seed( None )
return random.sample( imgSequence, numImgs )
def main():
# Process the command line arguments
args = processCommandLine( sys.argv )
# Get the list if input images
filenames = [ args[ "srcImgDir" ] + '/' + fileame for fileame in os.listdir( args[ "srcImgDir" ] ) ]
# Load the images into memory
images = [ cv2.imread( filename ) for filename in filenames ]
# Validate
for index, img in enumerate(images):
# None
if img is None:
print( " Skipping '" + filenames[index] + "' because it failed to load" )
continue
# Remove files that could not be loaded
index = 0
while index < len(images):
if images[index] is None:
images.pop( index )
filenames.pop( index )
else:
index += 1
# Get a random sequence
try:
randomSequence = randomize( len(images) )
except Exception as e:
print( e )
exit()
# Create a single window for display
windowName = "BINGO"
cv2.namedWindow( windowName )
# For every randomly-selected image index
i = 0
while i < len(randomSequence):
index = randomSequence[ i ]
# Output the filename in case something happens
print( "Bingo card " + str(i) + ": " + filenames[ index ] )
# Show the image
cv2.imshow( windowName, images[ index ] )
cv2.setWindowTitle( windowName, "Bingo card " + str(i) )
# Handle user input
iPrev = i
while i == iPrev:
# Wait for keyboard input
keyPressed = cv2.waitKey( 0 )
# enter, right-arrow, down-arrow
if keyPressed == 13 or keyPressed == 3 or keyPressed == 1:
i += 1
# left-arrow up-arrow
elif keyPressed == 2 or keyPressed == 0:
i = max(0, i-1)
# escape
elif keyPressed == 27:
i = 1000000000
# Clean up
cv2.destroyAllWindows()
if __name__ == "__main__":
main()