-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcount_images_per_day.py
More file actions
161 lines (147 loc) · 6.45 KB
/
count_images_per_day.py
File metadata and controls
161 lines (147 loc) · 6.45 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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
'''Author: Chandra Krintz, UCSB, ckrintz@cs.ucsb.edu, AppScale BSD license'''
import random,argparse,json,os,sys,time
import exifread
from contextlib import contextmanager
DEBUG = False
skip = False
total = 0
count = 0
daycount = 0
nightcount = 0
datemap = {}
######################## timer utility ############################
@contextmanager
def timeblock(label):
global total,count
start = time.time() #time.process_time() available in python 3
try:
yield
finally:
end = time.time()
total += end-start
count += 1
if count % 100 == 0:
print 'Processed {0} images...'.format(count)
if DEBUG:
print ('{0} : {1:.10f} secs'.format(label, end - start))
######################## process_local_dir ############################
def process_local_dir(fn,prefix,preflong,pictype):
global daycount,nightcount,datemap
lst = []
for root, subFolders, files in os.walk(fn):
for ele in files:
fname = os.path.join(root, ele) #full path name
if ele.endswith(".JPG") and (preflong in fname):
hr = 10
#check that its day time
with timeblock('EXIF_JPEG'):
with open(fname, 'rb') as fjpeg:
tags = exifread.process_file(fjpeg)
stop_tag = 'Image DateTime'
dt_tag = vars(tags[stop_tag])['printable']
#dt_tag: 2014:08:01 19:06:50
#t: 19:06:50
d = dt_tag.split()[0]
t = dt_tag.split()[1]
t = t.split(':')
hr = int(t[0])
if DEBUG:
print 'HOUR: {0}'.format(hr)
if d in datemap:
dn = datemap[d]
else:
dn = (0,0)
if hr >= 9 and hr <= 16: #between 9am and 4pm, inclusive
if DEBUG:
print '\tAppending'
if not skip:
lst.append(fname)
daycount += 1
dn = (dn[0]+1,dn[1]) #incr day
else:
nightcount += 1
dn = (dn[0],dn[1]+1) #incr night
datemap[d] = dn
return lst
######################## main ############################
def main():
global DEBUG,skip
parser = argparse.ArgumentParser(description='Find all of the pictures under each camera prefix is map and count them')
parser.add_argument('imgdir',action='store',help='Directory')
parser.add_argument('map',action='store',help='json map filename with Box directories to check')
parser.add_argument('out_fname',action='store',help='output filename')
#json map format: "Lisque": ["7413964473","Lisque Mesa 1"],
# prefix ID actual fname prefix
'''The output of this program is a file called outfile (name passed in) that holds the full path to the file.
A client program should read in this file, read each line, identify the file in the filesystem, and
pass it to the processing program (TensorFlow) for processing. The images in the file are randomly
selected and were taking during the hours of 9am and 4pm.'''
'''The map file passed in should contain this line for the Main camera:
"Main":["7413970457","Main Road Water Hole","2"]
'''
#optional arguments
parser.add_argument('--count',action='store',default=10000,help='number of images to return')
parser.add_argument('--skip',action='store_true',default=False,help='set to skip the picking, ocount only')
parser.add_argument('--debug',action='store_true',default=False,help='Turn debugging on (default: off)')
args = parser.parse_args()
DEBUG = args.debug
#skip = args.skip
skip = True
#read in the map
with open(args.map,'r') as map_file:
'''
mymap keys are location prefix names, values are lists of strings.
The list contains three elements: (1) box_folder_id of root/parent folder,
(2) the full prefix of the original file name (this allows us to skip files that don't belong to the location)
and (3) the pictype (type of picture indicating the parameter (camera identifier)
for OCR of the temperature value in the image.
'''
mymap = json.loads(map_file.read())
szsum = 0
cntsum = 0
with open(args.out_fname,'w') as ofile:
for key in mymap: #process a camera at a time
#key is the location prefix specified in the config file
val = mymap[key] #list of three strings
myfolder_id = val[0] #folder ID cut/pasted from box after creating
#the folder manually (its in the URL when in folder in box)
prefix = key
full_prefix = val[1] #used to check if a file belongs to this folder/key
pictype = int(val[2]) #index indicating which OCR to use
if DEBUG:
print '{0}: {1}: {2}'.format(myfolder_id,prefix,full_prefix)
#get the list containing the full_filename for this map key (camera location)
the_list = process_local_dir(args.imgdir,prefix,full_prefix,pictype)
if not skip:
new_list = random.sample(the_list, args.count)
for item in new_list:
ofile.write(item+'\n')
print 'Total time for running exif on all Main JPGs: {0}secs for {1} images = {2} secs per image'.format(total,count,float(total/count))
print 'Total count of day images: {0} non-day images: {1}'.format(daycount,nightcount)
sumperday = 0
sumpernight = 0
days = 0
for key in datemap:
dn = datemap[key] #pair (daycount,nightcount)
sumperday += dn[0]
sumpernight += dn[1]
days += 1
print 'Avg # of day images per day: {0} avg # of non-day images per day: {1} avg per 24hrs {2}'.format(sumperday/days,sumpernight/days,(sumperday+sumpernight)/days)
print '# of days: {0}'.format(days)
'''The following reads in the file we just wrote and processes each file. Modify this example to
process the file with TensorFlow. Uncomment to print out the sizes of each file listed in out_fname.
'''
'''
with open(args.out_fname,'r') as ifile:
for line in ifile:
#strip off the newline character
fname = line.strip()
with open(args.out_fname,'r') as open_file:
pass #unused, but file is open here if needed
#get the size of the file in bytes
size = os.path.getsize(fname)
print size
'''
##################################
if __name__ == '__main__':
main()