File size: 2,039 Bytes
a0f311d
82a076d
a0f311d
 
 
 
 
 
 
 
 
 
82a076d
a0f311d
82a076d
 
 
 
 
 
 
 
 
 
 
 
a0f311d
82a076d
 
 
 
 
 
 
 
 
 
 
 
 
a0f311d
82a076d
a0f311d
82a076d
 
a0f311d
 
82a076d
 
 
 
 
 
 
 
 
 
a0f311d
 
 
82a076d
a0f311d
 
 
 
 
 
 
 
 
 
82a076d
a0f311d
82a076d
 
 
a0f311d
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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
from flask import Flask, request, jsonify, send_from_directory
from nsfw_detector import predict
import requests
import io
import random
import os
from PIL import Image
from datetime import datetime
import string
import re

app = Flask(__name__)
model = predict.load_model('./nsfw_mobilenet2.224x224.h5')

@app.route("/imagebuffer", methods=["POST"])
def check():
    try:
        data = request.get_json()
        imgdata = data["data"]

        #imgdata is like 'data:image/jpeg;base64,/9j/4AAQSkZJRgABAQAAAQABAAD/2wBD...', save it to a file in ./TEMP
        imgdata = re.sub('^data:image/.+;base64,', '', imgdata)
        imgdata = bytes(imgdata, encoding="utf-8")
        img = Image.open(io.BytesIO(imgdata))
        filename = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) + ".jpg"
        img.save(f"./TEMP/{filename}")

        #predict the image
        result = predict.predict(model, f"./TEMP/{filename}")

        response = {
            "success": True,
            "result": result
        }

    except Exception as e:
        response = {
            "success": False,
            "error": str(e)
        }

    return jsonify(response)

@app.route("/imageurl", methods=["POST"])
def checkUrl():
    try:
        data = request.get_json()
        url = data["url"]

        #download the image
        response = requests.get(url)
        img = Image.open(io.BytesIO(response.content))
        filename = ''.join(random.choices(string.ascii_uppercase + string.digits, k=10)) + ".jpg"
        img.save(f"./TEMP/{filename}")

        #predict the image
        result = predict.predict(model, f"./TEMP/{filename}")

        response = {
            "success": True,
            "result": result
        }

    except Exception as e:
        response = {
            "success": False,
            "error": str(e)
        }

    return jsonify(response)


if __name__ == "__main__":
    if not os.path.exists("./TEMP"):
        os.makedirs("./TEMP")

    app.run(debug=True, host="0.0.0.0", port=7860)