-
Notifications
You must be signed in to change notification settings - Fork 11
/
IntelliP.py
2217 lines (1699 loc) · 82.6 KB
/
IntelliP.py
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
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
(c) Moses and Joh Olafenwa 2018
Website : https://moses.specpal.science , https://john.specpal.science
---------------------------
This is the file that contains all the python code
for the IntelliP.
"""
#Below is the imports needed for the program
from kivy.app import App
from kivy.uix.label import Label
from kivy.uix.gridlayout import GridLayout
from kivy.uix.image import AsyncImage
from kivy.core.window import Window
from kivy.uix.scrollview import ScrollView
from kivy.app import runTouchApp
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.graphics import Color, Rectangle
from kivy.clock import mainthread
from kivy.config import Config
import os
from imageai.Prediction import ImagePrediction
import threading
import json
# Below we obtain the working directory of our python program fro use later in the program.
execution_path = os.getcwd()
# This value is created to enable us to track loaded pages in each gallery category
global gallerySteps
gallerySteps = 1
# Below is an aray of all the folders in the computer we intend to sacn for photos
folders_array = []
pictures_folder = os.environ["USERPROFILE"] + "\\Pictures\\"
folders_array.append(pictures_folder)
download_folder = os.environ["USERPROFILE"] + "\\Downloads\\"
folders_array.append(download_folder)
documents_folder = os.environ["USERPROFILE"] + "\\Documents\\"
folders_array.append(documents_folder)
videos_folder = os.environ["USERPROFILE"] + "\\Videos\\"
folders_array.append(videos_folder)
desktop_folder = os.environ["USERPROFILE"] + "\\Desktop\\"
folders_array.append(desktop_folder)
# Below is a dictionary of arrays of photos extracted and categorized by our image prediction object
pictures_array = []
pictures_object_array = []
pictures_dictionary = {}
pictures_animals_array = []
pictures_dictionary["animals"] = pictures_animals_array
pictures_seaanimals_array = []
pictures_dictionary["seaanimals"] = pictures_seaanimals_array
pictures_birds_array = []
pictures_dictionary["birds"] = pictures_birds_array
pictures_objects_array = []
pictures_dictionary["objects"] = pictures_objects_array
pictures_electronics_array = []
pictures_dictionary["electronics"] = pictures_electronics_array
pictures_dresses_array = []
pictures_dictionary["dresses"] = pictures_dresses_array
pictures_foods_array = []
pictures_dictionary["foods"] = pictures_foods_array
pictures_plants_array = []
pictures_dictionary["plants"] = pictures_plants_array
pictures_aircrafts_array = []
pictures_dictionary["aircrafts"] = pictures_aircrafts_array
pictures_places_array = []
pictures_dictionary["places"] = pictures_places_array
pictures_vehicles_array = []
pictures_dictionary["vehicles"] = pictures_vehicles_array
pictures_people_array = []
pictures_dictionary["people"] = pictures_people_array
# Below is our image prediction object
imagePrediction = ImagePrediction()
imagePrediction.setModelTypeAsResNet()
imagePrediction.setModelPath(execution_path + "\\resnet50_weights_tf_dim_ordering_tf_kernels.h5")
imagePrediction.setJsonPath(execution_path + "\\imagenet_class_index.json")
# Below is our main application layout
mainLayout = FloatLayout()
mainLayout.size_hint = (1, None)
mainLayout.size = (Window.width, Window.height)
# Below is our layout when we are scanning the computer for photos. It also shows the progress.
scanLayout = GridLayout()
scanLayout.cols = 1
scanLayout.size_hint = (1, None)
scanLayout.size = (Window.width, Window.height)
with scanLayout.canvas.before:
Color(0,1,0,1)
scanLayout.rect = Rectangle(size= Window.size, pos = scanLayout.pos)
label1 = Label()
label1.size_hint_x = 1
label1.font_size = 15
label1.text = "IntelliP is scanning your pictures.\n" \
"It will perform this operation once.\n" \
"Once done, it won't need to scan again\n" \
"It will load your pictures in intelligent\n" \
"categories."
scanLayout.add_widget(label1)
label2 = Label()
label2.size_hint_x = 1
label2.font_size = 20
label2.text = "Pictures found : "
scanLayout.add_widget(label2)
scanLoader = AsyncImage()
try:
scanLoader.source = execution_path + "\\loading.gif"
scanLoader.size_hint = (3, None)
scanLoader.height = 300
scanLayout.add_widget(scanLoader)
except:
print("Skipped")
# Below is the class of the Button than displays the "About" information of the Application
class AboutButton(Button):
def __init__(self, **kwargs):
super(AboutButton, self).__init__(**kwargs)
def on_press(self):
aboutLayout = GridLayout()
aboutLayout.cols = 1
aboutLayout.size_hint = (1, None)
aboutLayout.size = (Window.width, Window.height)
with aboutLayout.canvas.before:
Color(0, 1, 0, 1)
aboutLayout.rect = Rectangle(size=Window.size, pos=aboutLayout.pos)
aboutText1 = Label()
aboutText1.font_size = 30
aboutText1.text = "IntelliP"
aboutText2 = Label()
aboutText2.font_size = 20
aboutText2.text = "(Intelligent Photos)"
aboutText3 = Label()
aboutText3.font_size = 15
aboutText3.text = "IntelliP is an intelligent photo \n" \
"Application that organizes your \n" \
"system photos into 12 distinct \n" \
"categories using AI. This Application \n" \
"serves as a demo App for the ImageAI \n" \
"library by Moses & John Olafenwa."
aboutText4 = Label()
aboutText4.font_size = 20
aboutText4.text = "(c) Moses & John Olafenwa, 2018."
closeAbout = CloseAbout(aboutLayout)
closeAbout.text = "Back"
closeAbout.font_size = 20
closeAbout.size_hint = (1, None)
closeAbout.height = 50
aboutLayout.add_widget(aboutText1)
aboutLayout.add_widget(aboutText2)
aboutLayout.add_widget(aboutText3)
aboutLayout.add_widget(aboutText4)
aboutLayout.add_widget(closeAbout)
mainLayout.add_widget(aboutLayout)
# Below is the class of the button that closes the "About" application layout when clicked
class CloseAbout(Button):
def __init__(self, aboutLayout, **kwargs):
super(CloseAbout, self).__init__(**kwargs)
self.about_layout = aboutLayout
def on_press(self):
mainLayout.remove_widget(self.about_layout)
# Below is the class of the layout used to display each page of at most 10 pictures in each photo category
class Scroller(ScrollView,):
def __init__(self, actionLayout, **kwargs):
super(Scroller, self).__init__(**kwargs)
self.size_hint = (1, None)
self.size = (Window.width, Window.height)
self.scroll_type = ["bars", "content"]
self.action_layout = actionLayout
def on_scroll_stop(self, touch, check_children=True):
threshold = self.vbar[0] * 100
if(int(threshold) < 5):
response = loadNext()
if response:
mainLayout.remove_widget(self)
mainLayout.remove_widget(self.action_layout)
elif(int(threshold) == 70):
response = loadPrevious()
if response:
mainLayout.remove_widget(self)
mainLayout.remove_widget(self.action_layout)
super(Scroller, self).on_scroll_stop(touch)
# Below is the layout that shows all the photo categories
galleryScroll = ScrollView()
galleryScroll.size_hint = (1, None)
galleryScroll.size = (Window.width, Window.height)
galleryScroll.scroll_type = ["bars", "content"]
galleryScroll.bar_width = 20
galleryScroll.bar_inactive_color = [0.3, 0.9, 0,5, 0.9]
with galleryScroll.canvas.before:
Color(0,1,0,1)
galleryScroll.rect = Rectangle(size= Window.size, pos = galleryScroll.pos)
galleryGrid = GridLayout(cols=2, spacing=10, size_hint_y=None)
galleryGrid.cols = 2
galleryGrid.bind(minimum_height = galleryGrid.setter('height'))
# Below is the Thread that scans the computer for photos, run image prediction on them and store them in
# "pictures.json" file and "pictures_monitor.json" file.
class ScanThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
# Below function is used to update the scan layout on the progress of the comuter scanning and image predictions
@mainthread
def updateUI(self, message):
label2.text = message
# The function below is used after the predictions are complete to show the photo gallery layout
@mainthread
def finalUpdateUI(self, value):
label2.text = "Total Pictures found : " + str(value)
# The code below writes the photo categories dictionary to a json file for use later
with open(execution_path + "\\pictures.json", "w+") as outfile:
json.dump(pictures_dictionary, outfile, indent=4, sort_keys=True, separators=(",", " : "), ensure_ascii=True)
outfile.close()
# The code below make checks and show the photo categories with pictures in them
if(len(pictures_animals_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_animals_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Animals"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Animals", pictures_animals_array)
loadButton.text = "View Animals"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_seaanimals_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_seaanimals_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Sea Animals"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Sea Animals", pictures_seaanimals_array)
loadButton.text = "View Sea Animals"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_birds_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_birds_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Birds"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Birds", pictures_birds_array)
loadButton.text = "View Birds"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_objects_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_objects_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print ("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Objects"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Objects", pictures_objects_array)
loadButton.text = "View Objects"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_electronics_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_electronics_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Electronics"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Electronics", pictures_electronics_array)
loadButton.text = "View Electronics"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_dresses_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_dresses_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Dresses"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Dresses", pictures_dresses_array)
loadButton.text = "View Dresses"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_foods_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_foods_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Foods"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Foods", pictures_foods_array)
loadButton.text = "View Foods"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_plants_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_plants_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Plants"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Plants", pictures_plants_array)
loadButton.text = "Plants"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_aircrafts_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_aircrafts_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
print("Skipped")
categoryLabel = Label()
categoryLabel.text = " >>> Aircrafts"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Aircrafts", pictures_aircrafts_array)
loadButton.text = "View Aircrafts"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_places_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_places_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Places"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Places", pictures_places_array)
loadButton.text = "View Places"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_vehicles_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_vehicles_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Vehicles"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Vehicles", pictures_vehicles_array)
loadButton.text = "View Vehicles"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
if (len(pictures_people_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_people_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> People"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> People", pictures_people_array)
loadButton.text = "View People"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
galleryGrid.add_widget(categoryGrid)
# Below is the code for extra button actions and dummy labels for interface optimization
dummyGridObject = Label()
dummyGridObject.size_hint_y = None
dummyGridObject.height = "100"
dummyGridObject.text = " . \n" \
" . \n" \
" . \n"
galleryGrid.add_widget(dummyGridObject)
detailsGrid = GridLayout()
detailsGrid.cols = 3
detailsGrid.size_hint = (1, None)
dummy1 = Label()
dummy1.size_hint = (1, None)
detailsGrid.add_widget(dummy1)
aboutbutton = AboutButton()
aboutbutton.text = "About"
aboutbutton.font_size = 15
aboutbutton.size_hint = (2, None)
aboutbutton.height = 30
detailsGrid.add_widget(aboutbutton)
dummy2 = Label()
dummy2.size_hint = (1, None)
detailsGrid.add_widget(dummy2)
mainLayout.remove_widget(scanLayout)
galleryScroll.add_widget(galleryGrid)
mainLayout.add_widget(galleryScroll)
mainLayout.add_widget(detailsGrid)
# This is the function that starts the thread and initate the image scanning and image prediction process
def run(self):
count = 0
# The Code below obtains the pictures from each folder in the folders_array and add it to the "pictures_array"
for eachFolder in folders_array:
if eachFolder == os.environ["USERPROFILE"] + "\\Pictures\\":
for top_dir, sub_dir_array, files_array in os.walk(eachFolder, topdown=True, followlinks=True):
for file in files_array:
if file.endswith(".png") or file.endswith(".jpg") or file.endswith(".gif") or file.endswith(
".PNG") or file.endswith(".JPG") or file.endswith(".GIF"):
count += 1
if ((count % 10) == 0):
self.updateUI("Pictures found : " + str(count))
pictures_array.append(os.path.join(top_dir, file))
else:
files = os.listdir(eachFolder)
for file in files:
if file.endswith(".png") or file.endswith(".jpg") or file.endswith(".gif") or file.endswith(
".PNG") or file.endswith(".JPG") or file.endswith(".GIF"):
count += 1
if ((count % 10) == 0):
self.updateUI("Pictures found : " + str(count))
pictures_array.append(eachFolder + file)
counter = 0
self.updateUI("Loading Intelligence Module....")
imagePrediction.loadModel()
self.updateUI("Intelligence Module loaded....")
# The code below obtains our manually edited " gallery_class.json " in preparation for photo
# categorization after image prediction
with open(execution_path + "\\gallery_class.json") as inputFile:
gallery_class = json.load(inputFile)
# The code below performs image prediction for all images in the " pictures_array "
for eachFile in pictures_array:
counter += 1
try:
predictions, percentage_probabilities = imagePrediction.predictImage(eachFile,
result_count=1)
# The code below creates a special Image Object for after each image prediction
# and adds it to the corresponding picture category array
for index in range(len(predictions)):
print(predictions[index])
imageDictionary = {}
imageDictionary["path"] = str(eachFile)
imageDictionary["prediction"] = predictions[index]
imageCategory = gallery_class[predictions[index]]
imageDictionary["category"] = imageCategory
if (imageCategory == "animals"):
pictures_animals_array.append(imageDictionary)
elif (imageCategory == "sea animals"):
pictures_seaanimals_array.append(imageDictionary)
elif (imageCategory == "birds"):
pictures_birds_array.append(imageDictionary)
elif (imageCategory == "objects"):
pictures_objects_array.append(imageDictionary)
elif (imageCategory == "electronics"):
pictures_electronics_array.append(imageDictionary)
elif (imageCategory == "dresses"):
pictures_dresses_array.append(imageDictionary)
elif (imageCategory == "foods"):
pictures_foods_array.append(imageDictionary)
elif (imageCategory == "plants"):
pictures_plants_array.append(imageDictionary)
elif (imageCategory == "aircrafts"):
pictures_aircrafts_array.append(imageDictionary)
elif (imageCategory == "places"):
pictures_places_array.append(imageDictionary)
elif (imageCategory == "vehicles"):
pictures_vehicles_array.append(imageDictionary)
elif (imageCategory == "people"):
pictures_people_array.append(imageDictionary)
except:
continue
self.updateUI("Processing " + str(counter) + " of " + str(len(pictures_array)) + " pictures")
# The code below creates " pictures_monitor.json " file that is used to keep track of processed photos
# during a computer rescan.
pictures_monitor_dictionary = {}
for eachItem in pictures_array:
newItem = eachItem
pictures_monitor_dictionary[str(newItem)] = ""
with open(execution_path + "\\pictures_monitor.json", "w+") as monitorfile:
json.dump(pictures_monitor_dictionary, monitorfile, indent=4, sort_keys=True, separators=(",", " : "), ensure_ascii=True)
monitorfile.close()
self.finalUpdateUI(len(pictures_array))
# The class below performs a computer rescan and image prediction to add new photos to its list. It does
# so and updates the "pictures.json" file and "pictures_json.json" file accordingly.
class ReScanThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
@mainthread
def updateUI(self, message):
label2.text = message
@mainthread
def updateFinalUI(self):
with open(execution_path + "\\pictures.json") as inputFile:
json_data = json.load(inputFile)
rescangalleryScroll = ScrollView()
rescangalleryScroll.size_hint = (1, None)
rescangalleryScroll.size = (Window.width, Window.height)
rescangalleryScroll.scroll_type = ["bars", "content"]
rescangalleryScroll.bar_width = 20
rescangalleryScroll.bar_inactive_color = [0.3, 0.9, 0, 5, 0.9]
with galleryScroll.canvas.before:
Color(0, 1, 0, 1)
rescangalleryScroll.rect = Rectangle(size=Window.size, pos=rescangalleryScroll.pos)
rescangalleryGrid = GridLayout(cols=2, spacing=10, size_hint_y=None)
rescangalleryGrid.cols = 2
rescangalleryGrid.bind(minimum_height=rescangalleryGrid.setter('height'))
animals_data = json_data["animals"]
for eachObject in animals_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "animals"
pictures_animals_array.append(imageDictionary)
seaanimals_data = json_data["seaanimals"]
for eachObject in seaanimals_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "seaanimals"
pictures_seaanimals_array.append(imageDictionary)
birds_data = json_data["birds"]
for eachObject in birds_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "birds"
pictures_birds_array.append(imageDictionary)
objects_data = json_data["objects"]
for eachObject in objects_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "objects"
pictures_objects_array.append(imageDictionary)
electronics_data = json_data["electronics"]
for eachObject in electronics_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "electronics"
pictures_electronics_array.append(imageDictionary)
dresses_data = json_data["dresses"]
for eachObject in dresses_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "dresses"
pictures_dresses_array.append(imageDictionary)
foods_data = json_data["foods"]
for eachObject in foods_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "foods"
pictures_foods_array.append(imageDictionary)
plants_data = json_data["plants"]
for eachObject in plants_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "plants"
pictures_plants_array.append(imageDictionary)
aircrafts_data = json_data["aircrafts"]
for eachObject in aircrafts_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "aircrafts"
pictures_aircrafts_array.append(imageDictionary)
places_data = json_data["places"]
for eachObject in places_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "places"
pictures_places_array.append(imageDictionary)
vehicles_data = json_data["vehicles"]
for eachObject in vehicles_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "vehicles"
pictures_vehicles_array.append(imageDictionary)
people_data = json_data["people"]
for eachObject in people_data:
imageDictionary = {}
imageDictionary["path"] = eachObject["path"]
imageDictionary["prediction"] = eachObject["prediction"]
imageDictionary["category"] = "people"
pictures_people_array.append(imageDictionary)
if (len(pictures_animals_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_animals_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Animals"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Animals", pictures_animals_array)
loadButton.text = "View Animals"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
if (len(pictures_seaanimals_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_seaanimals_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Sea Animals"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Sea Animals", pictures_seaanimals_array)
loadButton.text = "View Sea Animals"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
## Adding the Categories to UI
if (len(pictures_birds_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_birds_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Birds"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Birds", pictures_birds_array)
loadButton.text = "View Birds"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
if (len(pictures_objects_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_objects_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Objects"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Objects", pictures_objects_array)
loadButton.text = "View Objects"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
if (len(pictures_electronics_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_electronics_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Electronics"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Electronics", pictures_electronics_array)
loadButton.text = "View Electronics"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
if (len(pictures_dresses_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_dresses_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Dresses"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Dresses", pictures_dresses_array)
loadButton.text = "View Dresses"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
if (len(pictures_foods_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_foods_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Foods"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Foods", pictures_foods_array)
loadButton.text = "View Foods"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)
if (len(pictures_plants_array) > 0):
categoryGrid = GridLayout(size_hint_y=None, height=400)
categoryGrid.cols = 1
categoryImage = AsyncImage(size_hint_y=None, height=300)
try:
categoryImage.source = pictures_plants_array[0]["path"]
categoryGrid.add_widget(categoryImage)
except:
None
categoryLabel = Label()
categoryLabel.text = " >>> Plants"
categoryLabel.font_size = 15
categoryGrid.add_widget(categoryLabel)
loadButton = LoadGalleryButton(" >> Plants", pictures_plants_array)
loadButton.text = "Plants"
loadButton.font_size = 15
categoryGrid.add_widget(loadButton)
rescangalleryGrid.add_widget(categoryGrid)