source: internals/2016/aptoideimagesdetector/trunk/explicit_content_detector/API/Explicit_detector/Illustration2Vector/illustration2vec_master/analyse_image.py @ 16448

Last change on this file since 16448 was 16448, checked in by dferreira, 3 years ago

File organization totally changed

File size: 1.6 KB
Line 
1# Diogo Daniel Soares Ferreira
2# diogodanielsoaresferreira@ua.pt
3# Aptoide, 2016
4
5# This script analyses a list of images, returning a tuple containing safe/explicit and its percentage.
6# As argument passed in the command line sould be the name of all files wanted to analyse.
7# For example, python analyse_image.py url_of_image
8
9try:
10        import Illustration2Vector.illustration2vec_master.i2v
11except:
12        import i2v
13from PIL import Image
14import os
15import glob
16import sys
17import pickle
18import urllib, cStringIO
19
20
21
22def analyse_explicit(illust2vec, image_dir):
23
24        try:
25                # Get image from file firectory
26                img = Image.open(os.path.join(image_dir))
27        except:
28                try:
29                        # Get image from url
30                        print "Getting Images..."#+image_dir
31                        file = cStringIO.StringIO(urllib.urlopen(image_dir).read())
32                        img = Image.open(file)
33                except:
34                        print "Could not parse image" + image_dir
35                        return (('explicit',0.5), ('safe',0.5))
36
37        print "Analysing images..."
38        list = illust2vec.estimate_specific_tags([img], ["explicit", "safe"])
39   
40        return (('explicit',list[0]['explicit']), ('safe',list[0]['safe']))
41
42# Test
43
44if __name__ == "__main__":
45       
46        print "Loading Neural Network Model..."
47        try:
48                illust2vec_f = open("illust2vec.pickle", "rb")
49                illust2vec = pickle.load(illust2vec_f)
50                illust2vec_f.close()
51        except IOError:
52                illust2vec = i2v.make_i2v_with_chainer(
53            "illust2vec_tag_ver200.caffemodel", "tag_list.json")
54                save_model = open("illust2vec.pickle", "wb")
55                pickle.dump(illust2vec, save_model)
56                save_model.close()
57
58
59        for image_dir in sys.argv:
60                if image_dir!=sys.argv[0]:
61                        print "Analysing image..."
62                        analyse_explicit(illust2vec, image_dir)
Note: See TracBrowser for help on using the repository browser.