-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
66 lines (47 loc) · 1.63 KB
/
app.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
58
59
60
61
62
63
64
65
66
from NudeNet import NudeClassifier
from flask import Flask, render_template, request, make_response, send_file
from PIL import Image
from io import BytesIO
from glob import glob
import pathlib, os, requests, json, mimetypes
NUDE_THRESHOLD = 0.8
model = NudeClassifier()
app = Flask(__name__)
app.debug = True
@app.route("/")
def index():
return render_template("index.html", threshold=NUDE_THRESHOLD)
@app.route("/local")
def local():
images = glob("images/*.png")
images.extend(glob("images/*.jpg"))
images.extend(glob("images/*.jpeg"))
result = model.classify(images)
return render_template("local.html", result=result, threshold=NUDE_THRESHOLD)
@app.route("/process", methods=["POST"])
def process():
image_urls = request.get_json()
imageFiles = []
image_directory = "images"
if not os.path.exists(image_directory):
os.mkdir(image_directory)
for image_url in image_urls:
r = requests.get(image_url)
i = Image.open(BytesIO(r.content))
_, tail = os.path.split(image_url)
extension = mimetypes.guess_extension(
r.headers.get("content-type", "").split(";")[0]
)
imagePath = os.path.join(image_directory, tail.split("?")[0] + extension)
i.save(imagePath)
imageFiles.append(imagePath)
results = model.classify(imageFiles)
return make_response(json.dumps(results))
@app.route("/images/<name>", methods=["GET"])
def images(name=None):
path = f"./images/{name}"
if os.path.isfile(path):
return send_file(path)
return make_response("Image Not Found", 404)
if __name__ == "__main__":
app.run()