Update python code styling
MODIFY
tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/classify_image.py
tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/classify_image.py
@@ -77,6 +77,7 @@ tf.app.flags.DEFINE_integer('num_top_predictions', 5,
DATA_URL = 'http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz'
# pylint: enable=line-too-long
+
def create_readable_names_for_imagenet_labels():
"""Create a dict mapping label id to human readable string.
@@ -104,11 +105,13 @@ def create_readable_names_for_imagenet_labels():
dest_directory = FLAGS.model_dir
- synset_list = [s.strip() for s in open(os.path.join(dest_directory, 'imagenet_lsvrc_2015_synsets.txt')).readlines()]
+ synset_list = [s.strip() for s in open(os.path.join(
+ dest_directory, 'imagenet_lsvrc_2015_synsets.txt')).readlines()]
num_synsets_in_ilsvrc = len(synset_list)
assert num_synsets_in_ilsvrc == 1000
- synset_to_human_list = open(os.path.join(dest_directory, 'imagenet_metadata.txt')).readlines()
+ synset_to_human_list = open(os.path.join(
+ dest_directory, 'imagenet_metadata.txt')).readlines()
num_synsets_in_all_imagenet = len(synset_to_human_list)
assert num_synsets_in_all_imagenet == 21842
@@ -129,6 +132,7 @@ def create_readable_names_for_imagenet_labels():
return labels_to_names
+
def run_inference_on_image(image):
"""Runs inference on an image.
@@ -148,12 +152,15 @@ def run_inference_on_image(image):
with tf.Graph().as_default():
image = tf.image.decode_jpeg(image_string, channels=3)
- processed_image = inception_preprocessing.preprocess_image(image, image_size, image_size, is_training=False)
+ processed_image = inception_preprocessing.preprocess_image(
+ image, image_size, image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
- # Create the model, use the default arg scope to configure the batch norm parameters.
+ # Create the model, use the default arg scope to configure the batch
+ # norm parameters.
with slim.arg_scope(inception.inception_v4_arg_scope()):
- logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False)
+ logits, _ = inception.inception_v4(
+ processed_images, num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
@@ -164,7 +171,8 @@ def run_inference_on_image(image):
init_fn(sess)
_, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
- sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
+ sorted_inds = [i[0] for i in sorted(
+ enumerate(-probabilities), key=lambda x:x[1])]
names = create_readable_names_for_imagenet_labels()
top_k = FLAGS.num_top_predictions
@@ -172,6 +180,7 @@ def run_inference_on_image(image):
index = sorted_inds[i]
print('%s (score = %.5f)' % (names[index], probabilities[index]))
+
def util_download(url, dest_directory):
"""Downloads the file.
@@ -193,6 +202,7 @@ def util_download(url, dest_directory):
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
+
def util_download_tar(url, dest_directory):
"""Downloads a file and extracts it.
@@ -215,6 +225,7 @@ def util_download_tar(url, dest_directory):
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(dest_directory)
+
def maybe_download_and_extract():
"""Download and extract model tar file."""
dest_directory = FLAGS.model_dir
@@ -224,13 +235,14 @@ def maybe_download_and_extract():
util_download_tar(DATA_URL, dest_directory)
# pylint: disable=line-too-long
if not tf.gfile.Exists(os.path.join(dest_directory, 'imagenet_lsvrc_2015_synsets.txt')):
- util_download('https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_lsvrc_2015_synsets.txt', dest_directory)
+ util_download(
+ 'https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_lsvrc_2015_synsets.txt', dest_directory)
if not tf.gfile.Exists(os.path.join(dest_directory, 'imagenet_metadata.txt')):
- util_download('https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_metadata.txt', dest_directory)
+ util_download(
+ 'https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_metadata.txt', dest_directory)
# pylint: enable=line-too-long
-
def main(_):
maybe_download_and_extract()
image = FLAGS.image_file

MODIFY
tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/inceptionapi.py
tika-parsers/src/main/resources/org/apache/tika/parser/recognition/tf/inceptionapi.py
@@ -49,11 +49,11 @@ slim = tf.contrib.slim
import requests
import json
-json.encoder.FLOAT_REPR = lambda o: format(o, '.2f') # JSON serialization of floats
+json.encoder.FLOAT_REPR = lambda o: format(
+ o, '.2f') # JSON serialization of floats
from time import time
-
import flask
@@ -71,12 +71,14 @@ tf.app.flags.DEFINE_string(
"""imagenet_lsvrc_2015_synsets.txt, and """
"""imagenet_metadata.txt.""")
tf.app.flags.DEFINE_integer('port', '8764', """Server PORT, default:8764""")
-tf.app.flags.DEFINE_string('log', 'inception.log', """Log file name, default: inception.log""")
+tf.app.flags.DEFINE_string('log', 'inception.log',
+ """Log file name, default: inception.log""")
# pylint: disable=line-too-long
DATA_URL = 'http://download.tensorflow.org/models/inception_v4_2016_09_09.tar.gz'
# pylint: enable=line-too-long
+
def create_readable_names_for_imagenet_labels():
"""Create a dict mapping label id to human readable string.
@@ -104,11 +106,13 @@ def create_readable_names_for_imagenet_labels():
dest_directory = FLAGS.model_dir
- synset_list = [s.strip() for s in open(os.path.join(dest_directory, 'imagenet_lsvrc_2015_synsets.txt')).readlines()]
+ synset_list = [s.strip() for s in open(os.path.join(
+ dest_directory, 'imagenet_lsvrc_2015_synsets.txt')).readlines()]
num_synsets_in_ilsvrc = len(synset_list)
assert num_synsets_in_ilsvrc == 1000
- synset_to_human_list = open(os.path.join(dest_directory, 'imagenet_metadata.txt')).readlines()
+ synset_to_human_list = open(os.path.join(
+ dest_directory, 'imagenet_metadata.txt')).readlines()
num_synsets_in_all_imagenet = len(synset_to_human_list)
assert num_synsets_in_all_imagenet == 21842
@@ -129,6 +133,7 @@ def create_readable_names_for_imagenet_labels():
return labels_to_names
+
def util_download(url, dest_directory):
"""Downloads the file.
@@ -150,6 +155,7 @@ def util_download(url, dest_directory):
statinfo = os.stat(filepath)
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
+
def util_download_tar(url, dest_directory):
"""Downloads a file and extracts it.
@@ -172,6 +178,7 @@ def util_download_tar(url, dest_directory):
print('Successfully downloaded', filename, statinfo.st_size, 'bytes.')
tarfile.open(filepath, 'r:gz').extractall(dest_directory)
+
def maybe_download_and_extract():
"""Download and extract model tar file."""
dest_directory = FLAGS.model_dir
@@ -181,29 +188,36 @@ def maybe_download_and_extract():
util_download_tar(DATA_URL, dest_directory)
# pylint: disable=line-too-long
if not tf.gfile.Exists(os.path.join(dest_directory, 'imagenet_lsvrc_2015_synsets.txt')):
- util_download('https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_lsvrc_2015_synsets.txt', dest_directory)
+ util_download(
+ 'https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_lsvrc_2015_synsets.txt', dest_directory)
if not tf.gfile.Exists(os.path.join(dest_directory, 'imagenet_metadata.txt')):
- util_download('https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_metadata.txt', dest_directory)
+ util_download(
+ 'https://raw.githubusercontent.com/tensorflow/models/master/inception/inception/data/imagenet_metadata.txt', dest_directory)
# pylint: enable=line-too-long
+
def current_time():
"""
Returns current time in milli seconds
"""
return int(1000 * time())
+
class Classifier(flask.Flask):
'''
Classifier Service class
'''
+
def __init__(self, name):
super(Classifier, self).__init__(name)
maybe_download_and_extract()
import logging
from logging.handlers import RotatingFileHandler
- file_handler = RotatingFileHandler(FLAGS.log, maxBytes=1024 * 1024 * 100, backupCount=20)
+ file_handler = RotatingFileHandler(
+ FLAGS.log, maxBytes=1024 * 1024 * 100, backupCount=20)
file_handler.setLevel(logging.INFO)
- formatter = logging.Formatter("%(asctime)s - %(name)s - %(levelname)s - %(message)s")
+ formatter = logging.Formatter(
+ "%(asctime)s - %(name)s - %(levelname)s - %(message)s")
file_handler.setFormatter(formatter)
self.logger.addHandler(file_handler)
self.names = imagenet.create_readable_names_for_imagenet_labels()
@@ -212,15 +226,17 @@ class Classifier(flask.Flask):
def classify(self, image_string, topk):
dest_directory = FLAGS.model_dir
-
with tf.Graph().as_default():
image = tf.image.decode_jpeg(image_string, channels=3)
- processed_image = inception_preprocessing.preprocess_image(image, self.image_size, self.image_size, is_training=False)
+ processed_image = inception_preprocessing.preprocess_image(
+ image, self.image_size, self.image_size, is_training=False)
processed_images = tf.expand_dims(processed_image, 0)
- # Create the model, use the default arg scope to configure the batch norm parameters.
+ # Create the model, use the default arg scope to configure the
+ # batch norm parameters.
with slim.arg_scope(inception.inception_v4_arg_scope()):
- logits, _ = inception.inception_v4(processed_images, num_classes=1001, is_training=False)
+ logits, _ = inception.inception_v4(
+ processed_images, num_classes=1001, is_training=False)
probabilities = tf.nn.softmax(logits)
init_fn = slim.assign_from_checkpoint_fn(
@@ -231,8 +247,8 @@ class Classifier(flask.Flask):
init_fn(sess)
_, probabilities = sess.run([image, probabilities])
probabilities = probabilities[0, 0:]
- sorted_inds = [i[0] for i in sorted(enumerate(-probabilities), key=lambda x:x[1])]
-
+ sorted_inds = [i[0] for i in sorted(
+ enumerate(-probabilities), key=lambda x:x[1])]
res = []
for i in range(topk):
@@ -245,6 +261,7 @@ class Classifier(flask.Flask):
from flask import Flask, request, abort, g, Response, jsonify
app = Classifier(__name__)
+
def get_remotefile(url, success=200, timeout=10):
"""
Given HTTP URL, this api gets the content of it
@@ -260,6 +277,7 @@ def get_remotefile(url, success=200, timeout=10):
pass
return None, None
+
@app.route("/")
def index():
"""
@@ -303,6 +321,7 @@ def ping_pong():
"""
return "pong"
+
@app.route("/inception/v4/classify", methods=["GET", "POST"])
def classify_image():
"""
@@ -343,6 +362,7 @@ def classify_image():
res['classnames'] = classnames
return Response(response=json.dumps(res), status=200, mimetype="application/json")
+
def main(_):
if not app.debug:
print("Serving on port %d" % FLAGS.port)

Update python code styling