-
Notifications
You must be signed in to change notification settings - Fork 1
/
app.py
111 lines (90 loc) · 3.2 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
from fastai.vision.all import *
from flask import Flask
app = Flask(__name__)
import json
from flask import request
import os
import time
from PIL import Image
from os import listdir
from os.path import isfile, join
from fastcore.all import *
from fastdownload import download_url
# obj = dict(error = False, isCat = None)
import __main__
def is_cat(x): return x[0].isupper()
__main__.is_cat = is_cat
learn = load_learner('model.pkl')
@app.route("/", methods=['GET', 'POST'])
def hello():
obj = {
"error": False,
"message": None,
"isCat": None
}
randomInt = random.randint(10, 1000)
imgDir = "images/"
catDir = "static/catImages/"
otherDir = "static/otherImages/"
filename = f"downloadedCat{randomInt}.jpg"
dest = imgDir+filename
destNew = imgDir+"2"+filename
destCat = catDir+filename
destOther = otherDir+filename
# handle the POST request
if request.method == 'POST':
#url = request.form.get('imageLink')
# Get the JSON payload from the request body
data = request.get_json()
# Decode the URL
decoded_url = urllib.parse.unquote(data['imageLink'])
try:
download_url(decoded_url, dest, show_progress=False)
except:
obj["error"] = True
obj["message"] = "Download error: This picture does not work. Try a different one. "+decoded_url
return obj
time.sleep(1)
try:
im = Image.open(dest)
im.to_thumb(256,256)
im.save(destNew, "JPEG")
except:
obj["error"] = True
obj["message"] = "Error saving image: This picture does not work. Try a different one."
os.remove(dest)
os.remove(destNew)
return obj
os.remove(dest)
try:
is_cat,_,probs = learn.predict(destNew)
except:
obj["error"] = True
obj["message"] = "Predict error: This picture does not work. Try a different one."
os.remove(dest)
os.remove(destNew)
return obj
obj["isCat"] = is_cat
if is_cat == 'True':
os.rename(destNew, destCat)
obj["yourImage"] = "catImages/" + filename
else:
os.rename(destNew, destOther)
obj["yourImage"] = "otherImages/" + filename
catfiles = [f for f in listdir(catDir) if isfile(join(catDir, f))]
catfiles = ["catImages/" + f for f in catfiles]
obj["catImages"] = catfiles
otherfiles = [f for f in listdir(otherDir) if isfile(join(otherDir, f))]
otherfiles = ["otherImages/" + f for f in otherfiles]
obj["otherImages"] = otherfiles
return obj
# otherwise handle the GET request
# obj["error"] = True
# obj["message"] = 'isCat requires POST method. You have made GET request'
catfiles = [f for f in listdir(catDir) if isfile(join(catDir, f))]
catfiles = ["catImages/" + f for f in catfiles]
obj["catImages"] = catfiles
otherfiles = [f for f in listdir(otherDir) if isfile(join(otherDir, f))]
otherfiles = ["otherImages/" + f for f in otherfiles]
obj["otherImages"] = otherfiles
return obj