@@ -35,16 +35,14 @@ def scan_photos():
35
35
for image_path in tqdm (image_paths ):
36
36
if image_path .lower ().endswith ('.jpg' ):
37
37
try :
38
- img_abs_path = os .path .abspath (os .path .join (image_dir ,image_path ))
39
- try :
40
- hash_md5 = hashlib .md5 ()
41
- with open (img_abs_path , "rb" ) as f :
42
- for chunk in iter (lambda : f .read (4096 ), b"" ):
43
- hash_md5 .update (chunk )
44
- image_hash = hash_md5 .hexdigest ()
45
- qs = Photo .objects .filter (image_hash = image_hash )
46
- except :
47
- raise Exception
38
+ img_abs_path = image_path
39
+ hash_md5 = hashlib .md5 ()
40
+ with open (img_abs_path , "rb" ) as f :
41
+ for chunk in iter (lambda : f .read (4096 ), b"" ):
42
+ hash_md5 .update (chunk )
43
+ image_hash = hash_md5 .hexdigest ()
44
+ qs = Photo .objects .filter (image_hash = image_hash )
45
+
48
46
if qs .count () < 1 :
49
47
photo = Photo (image_path = img_abs_path )
50
48
photo .added_on = datetime .datetime .now ()
@@ -54,18 +52,18 @@ def scan_photos():
54
52
start = datetime .datetime .now ()
55
53
photo ._generate_thumbnail ()
56
54
elapsed = (datetime .datetime .now () - start ).total_seconds ()
57
- # print('thumbnail get', elapsed)
55
+ print ('thumbnail get' , elapsed )
58
56
59
57
start = datetime .datetime .now ()
60
58
photo ._save_image_to_db ()
61
59
elapsed = (datetime .datetime .now () - start ).total_seconds ()
62
- # print('image save', elapsed)
60
+ print ('image save' , elapsed )
63
61
64
62
start = datetime .datetime .now ()
65
63
photo ._extract_exif ()
66
64
photo .save ()
67
65
elapsed = (datetime .datetime .now () - start ).total_seconds ()
68
- # print('exif extraction', elapsed)
66
+ print ('exif extraction' , elapsed )
69
67
70
68
# start = datetime.datetime.now()
71
69
# photo._geolocate()
@@ -76,7 +74,7 @@ def scan_photos():
76
74
start = datetime .datetime .now ()
77
75
photo ._extract_faces ()
78
76
elapsed = (datetime .datetime .now () - start ).total_seconds ()
79
- # print('face extraction', elapsed)
77
+ print ('face extraction' , elapsed )
80
78
81
79
start = datetime .datetime .now ()
82
80
photo ._add_to_album_date ()
@@ -87,6 +85,10 @@ def scan_photos():
87
85
print ("photo already exists in db" )
88
86
except Exception as e :
89
87
print ("could not load image %s" % image_path )
88
+ try :
89
+ print (e .message )
90
+ except :
91
+ pass
90
92
91
93
return {"new_photo_count" :added_photo_count , "status" :True }
92
94
# photos = Photo.objects.all()
@@ -96,3 +98,67 @@ def scan_photos():
96
98
# print(photo.face_set.all())
97
99
98
100
101
+ if __name__ == "__main__" :
102
+ image_paths = []
103
+ for image_dir in image_dirs :
104
+ image_paths .extend ([os .path .join (dp , f ) for dp , dn , fn in os .walk (image_dir ) for f in fn ]
105
+ )
106
+
107
+ added_photo_count = 0
108
+ for image_path in tqdm (image_paths ):
109
+ if image_path .lower ().endswith ('.jpg' ):
110
+ try :
111
+ img_abs_path = image_path
112
+ hash_md5 = hashlib .md5 ()
113
+ with open (img_abs_path , "rb" ) as f :
114
+ for chunk in iter (lambda : f .read (4096 ), b"" ):
115
+ hash_md5 .update (chunk )
116
+ image_hash = hash_md5 .hexdigest ()
117
+ qs = Photo .objects .filter (image_hash = image_hash )
118
+
119
+ if qs .count () < 1 :
120
+ photo = Photo (image_path = img_abs_path )
121
+ photo .added_on = datetime .datetime .now ()
122
+ photo .save ()
123
+ photo ._generate_md5 ()
124
+
125
+ start = datetime .datetime .now ()
126
+ photo ._generate_thumbnail ()
127
+ elapsed = (datetime .datetime .now () - start ).total_seconds ()
128
+ print ('thumbnail get' , elapsed )
129
+
130
+ start = datetime .datetime .now ()
131
+ photo ._save_image_to_db ()
132
+ elapsed = (datetime .datetime .now () - start ).total_seconds ()
133
+ print ('image save' , elapsed )
134
+
135
+ start = datetime .datetime .now ()
136
+ photo ._extract_exif ()
137
+ photo .save ()
138
+ elapsed = (datetime .datetime .now () - start ).total_seconds ()
139
+ print ('exif extraction' , elapsed )
140
+
141
+ # start = datetime.datetime.now()
142
+ # photo._geolocate()
143
+ # photo.save()
144
+ # elapsed = (datetime.datetime.now() - start).total_seconds()
145
+ # print('geolocation', elapsed)
146
+
147
+ start = datetime .datetime .now ()
148
+ photo ._extract_faces ()
149
+ elapsed = (datetime .datetime .now () - start ).total_seconds ()
150
+ print ('face extraction' , elapsed )
151
+
152
+ start = datetime .datetime .now ()
153
+ photo ._add_to_album_date ()
154
+ elapsed = (datetime .datetime .now () - start ).total_seconds ()
155
+ added_photo_count += 1
156
+ print (img_abs_path )
157
+ else :
158
+ print ("photo already exists in db" )
159
+ except Exception as e :
160
+ print ("could not load image %s" % image_path )
161
+ try :
162
+ print (e .message )
163
+ except :
164
+ pass
0 commit comments