-
Notifications
You must be signed in to change notification settings - Fork 5
/
analyzer.py
57 lines (43 loc) · 1.51 KB
/
analyzer.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
# -*- coding: utf-8 -*-
import logging
import os
from converter import Converter
from image_detector.categorizer import Categorizer
logger = logging.getLogger()
class ImageAnalyzer(Categorizer):
"""
Decide which image is worth publishing.
"""
def __init__(self, config):
self.config = config
Categorizer.__init__(self)
def get_best_result(self, results_dict):
if results_dict:
return max(results_dict, key=results_dict.get)
else:
return None
def get_preferred_key(self, percent_results):
"""
Since the classification from classifier.predict and
classifier.predict_proba differ, sometimes we want
to get the prediction of the highest value.
text / image / blank
UNUSED NOW
"""
return max(percent_results, key=percent_results.get)
def run(self):
results_dict = {}
converter = Converter(self.logger, self.config)
i = 0
for jpg_file_path in converter.iterate():
results = self.categorize_image(jpg_file_path)
self.logger.info("%s, %s " % (results['verdict'], jpg_file_path))
percent_results = results['percent']
if results['percent']['image'] >= 30.0:
results_dict[jpg_file_path] = percent_results['image']
i += 1
if i > 10:
break
else:
os.remove(jpg_file_path)
return self.get_best_result(results_dict)