diff --git a/src/main/resources/python/models/1547856517/saved_model.pb b/src/main/resources/python/models/1547856517/saved_model.pb new file mode 100644 index 0000000000000000000000000000000000000000..8ee8de2e0cdcff9ee426ab003b2a17dd51d258e9 Binary files /dev/null and b/src/main/resources/python/models/1547856517/saved_model.pb differ diff --git a/src/main/resources/python/models/1547856517/variables/variables.data-00000-of-00001 b/src/main/resources/python/models/1547856517/variables/variables.data-00000-of-00001 new file mode 100644 index 0000000000000000000000000000000000000000..93097a141f5bca87bf47c4270f139ae7977e3614 Binary files /dev/null and b/src/main/resources/python/models/1547856517/variables/variables.data-00000-of-00001 differ diff --git a/src/main/resources/python/models/1547856517/variables/variables.index b/src/main/resources/python/models/1547856517/variables/variables.index new file mode 100644 index 0000000000000000000000000000000000000000..cc9a33424c6f3e5a40c56347922fa9bbd2412fce Binary files /dev/null and b/src/main/resources/python/models/1547856517/variables/variables.index differ diff --git a/src/main/resources/python/yellow.py b/src/main/resources/python/yellow.py new file mode 100644 index 0000000000000000000000000000000000000000..32a04002292285a56aae7c45387371f654ca95dd --- /dev/null +++ b/src/main/resources/python/yellow.py @@ -0,0 +1,44 @@ +import sys +import json +import requests + +from PIL import Image +import numpy as np + +_IMAGE_SIZE = 64 +# TensorFlow-serving 调用地址,这里要替换成自己的,后面会讲到如何安装 +SERVER_URL = 'http://172.17.0.4:8501/v1/models/image:predict' +_LABEL_MAP = {0: 'drawings', 1: 'hentai', 2: 'neutral', 3: 'porn', 4: 'sexy'} + + +def standardize(img): + mean = np.mean(img) + std = np.std(img) + img = (img - mean) / std + return img + +# 导入 +def load_image(image_path): + img = Image.open(image_path) + img = img.resize((_IMAGE_SIZE, _IMAGE_SIZE)) + img.load() + data = np.asarray(img, dtype="float32") + data = standardize(data) + data = data.astype(np.float16, copy=False) + return data + +# 分析 +def nsfw_predict(image_data): + pay_load = json.dumps({"inputs": [image_data.tolist()]}) + response = requests.post(SERVER_URL, data=pay_load) + data = response.json() + outputs = data['outputs'] + predict_result = {"classes": _LABEL_MAP.get(outputs['classes'][0])} + predict_result['probabilities'] = {_LABEL_MAP.get(i): l for i, l in enumerate(outputs['probabilities'][0])} + return predict_result + + +if __name__ == '__main__': + image_data = load_image(sys.argv[1]) + predict = nsfw_predict(image_data) + print(predict) diff --git "a/src/main/resources/python/\346\234\272\345\231\250\345\255\246\344\271\240.txt" "b/src/main/resources/python/\346\234\272\345\231\250\345\255\246\344\271\240.txt" new file mode 100644 index 0000000000000000000000000000000000000000..3f3bd037873ed2b8535e625f4a8b382da469b0bc --- /dev/null +++ "b/src/main/resources/python/\346\234\272\345\231\250\345\255\246\344\271\240.txt" @@ -0,0 +1,21 @@ + +docker run -d --name serving_image tensorflow/serving + +# 2000 样本 +docker cp /home/nsfw/data/models/ serving_image:/models/image + +docker cp /home/nsfw/data/models/1547856517 serving_image:/models/image + +docker exec -it serving_image /bin/bash + +tensorflow_model_server --port=8500 --rest_api_port=8501 --model_name=image --model_base_path=/models/image + + +pip install pillow +pip install numpy + +## 参考 + +https://www.cnblogs.com/weiyinfu/p/9928363.html + +https://www.oschina.net/news/103861/nsfw-opensource \ No newline at end of file