1 | # Diogo Daniel Soares Ferreira |
---|
2 | # diogodanielsoaresferreira@ua.pt |
---|
3 | # Aptoide, 2016 |
---|
4 | |
---|
5 | # This script runs a test on all the images |
---|
6 | # and categorizes the images as false/true positive/negative. It also contains information about the time. |
---|
7 | |
---|
8 | import i2v |
---|
9 | from datetime import datetime, time |
---|
10 | from PIL import Image |
---|
11 | import os |
---|
12 | import glob |
---|
13 | import pickle |
---|
14 | |
---|
15 | |
---|
16 | print "Loading Neural Network Model..." |
---|
17 | now = datetime.now() |
---|
18 | |
---|
19 | # Tries to load object with database. |
---|
20 | # If it does not exist, creates one himself. |
---|
21 | |
---|
22 | try: |
---|
23 | illust2vec_f = open("illust2vec.pickle", "rb") |
---|
24 | illust2vec = pickle.load(illust2vec_f) |
---|
25 | illust2vec_f.close() |
---|
26 | except IOError: |
---|
27 | illust2vec = i2v.make_i2v_with_chainer( |
---|
28 | "illust2vec_tag_ver200.caffemodel", "tag_list.json") |
---|
29 | save_model = open("illust2vec.pickle", "wb") |
---|
30 | pickle.dump(illust2vec, save_model) |
---|
31 | save_model.close() |
---|
32 | |
---|
33 | print "Time loading Neural Network Model: "+str(datetime.now()-now) |
---|
34 | |
---|
35 | global_time = datetime.now() |
---|
36 | |
---|
37 | # True/false positive/negative |
---|
38 | fp = 0 |
---|
39 | fn = 0 |
---|
40 | tp = 0 |
---|
41 | tn = 0 |
---|
42 | |
---|
43 | types = ('*.png', '*.jpg') |
---|
44 | |
---|
45 | # File to save the results. |
---|
46 | f = open('test_results.txt','w') |
---|
47 | |
---|
48 | for image_dir in ('../../API to download database/Big_Database/images/icons_explicit', |
---|
49 | '../../API to download database/Big_Database/images/icons_non-explicit'): |
---|
50 | for t in types: |
---|
51 | for image_file in glob.glob(os.path.join(image_dir,t)): |
---|
52 | |
---|
53 | print "Processing image..."+str(image_file) |
---|
54 | try: |
---|
55 | now = datetime.now() |
---|
56 | |
---|
57 | img = Image.open(image_file) |
---|
58 | list = illust2vec.estimate_specific_tags([img], ["explicit", "safe"]) |
---|
59 | print "Time processing image: "+str(datetime.now()-now) |
---|
60 | f.write('\n') |
---|
61 | f.write('\n') |
---|
62 | f.write(image_file) |
---|
63 | f.write('\n') |
---|
64 | if(list[0]['explicit']>list[0]['safe']): |
---|
65 | print "explicit" |
---|
66 | f.write("explicit") |
---|
67 | if(image_dir=='../../API to download database/Big_Database/images/icons_non-explicit'): |
---|
68 | fp+=1 |
---|
69 | else: |
---|
70 | tp+=1 |
---|
71 | else: |
---|
72 | print "safe" |
---|
73 | f.write("safe") |
---|
74 | if(image_dir=='../../API to download database/Big_Database/images/icons_explicit'): |
---|
75 | fn+=1 |
---|
76 | else: |
---|
77 | tn+=1 |
---|
78 | except: |
---|
79 | print "Could not open image "+str(image_file) |
---|
80 | |
---|
81 | |
---|
82 | print "Time processing all icons: "+str(datetime.now()-global_time) |
---|
83 | print str(fp)+" false positives" |
---|
84 | print str(fn)+" false negatives" |
---|
85 | print str(tn)+" true negatives" |
---|
86 | print str(tp)+" true positives" |
---|
87 | |
---|
88 | for image_dir in ('../../API to download database/Big_Database/images/screenshot_explicit', |
---|
89 | '../../API to download database/Big_Database/images/screenshot_non-explicit'): |
---|
90 | for t in types: |
---|
91 | for image_file in glob.glob(os.path.join(image_dir,t)): |
---|
92 | |
---|
93 | print "Processing image..."+str(image_file) |
---|
94 | try: |
---|
95 | now = datetime.now() |
---|
96 | |
---|
97 | img = Image.open(image_file) |
---|
98 | list = illust2vec.estimate_specific_tags([img], ["explicit", "safe"]) |
---|
99 | print "Time processing image: "+str(datetime.now()-now) |
---|
100 | f.write('\n') |
---|
101 | f.write('\n') |
---|
102 | f.write(image_file) |
---|
103 | f.write('\n') |
---|
104 | if(list[0]['explicit']>list[0]['safe']): |
---|
105 | print "explicit" |
---|
106 | f.write("explicit") |
---|
107 | if(image_dir=='../../API to download database/Big_Database/images/screenshot_non-explicit'): |
---|
108 | fp+=1 |
---|
109 | else: |
---|
110 | tp+=1 |
---|
111 | else: |
---|
112 | print "safe" |
---|
113 | f.write("safe") |
---|
114 | if(image_dir=='../../API to download database/Big_Database/images/screenshot_explicit'): |
---|
115 | fn+=1 |
---|
116 | else: |
---|
117 | tn+=1 |
---|
118 | except: |
---|
119 | print "Could not open image "+str(image_file) |
---|
120 | |
---|
121 | |
---|
122 | print "Time processing all screenshots: "+str(datetime.now()-global_time) |
---|
123 | print str(fp)+" false positives" |
---|
124 | print str(fn)+" false negatives" |
---|
125 | print str(tn)+" true negatives" |
---|
126 | print str(tp)+" true positives" |
---|
127 | |
---|
128 | f.close() |
---|