Cocolicious
commited on
Commit
•
ab41864
1
Parent(s):
f4469e8
Upload 17 files
Browse files- .gitattributes +1 -0
- .gitignore +3 -0
- .vscode/launch.json +26 -0
- README.md +7 -0
- __pycache__/app.cpython-310-DESKTOP-DIP1FRF.pyc +0 -0
- __pycache__/app.cpython-310.pyc +0 -0
- app.py +127 -0
- canvas.png +0 -0
- canvas2.png +0 -0
- cat.png +0 -0
- cats_model/fingerprint.pb +3 -0
- cats_model/keras_metadata.pb +3 -0
- cats_model/saved_model.pb +3 -0
- cats_model/variables/variables.data-00000-of-00001 +3 -0
- cats_model/variables/variables.index +0 -0
- knn_clf.pkl +3 -0
- randomforest_regression.pkl +3 -0
- requirements.txt +26 -0
.gitattributes
CHANGED
@@ -32,3 +32,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
32 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
33 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
34 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
35 |
+
cats_model/variables/variables.data-00000-of-00001 filter=lfs diff=lfs merge=lfs -text
|
.gitignore
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
__pycache__/*
|
2 |
+
kia-fs23/*
|
3 |
+
.DS_Store
|
.vscode/launch.json
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
// Use IntelliSense to learn about possible attributes.
|
3 |
+
// Hover to view descriptions of existing attributes.
|
4 |
+
// For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387
|
5 |
+
"version": "0.2.0",
|
6 |
+
"configurations": [
|
7 |
+
|
8 |
+
{
|
9 |
+
"name": "Python: Flask",
|
10 |
+
"type": "python",
|
11 |
+
"request": "launch",
|
12 |
+
"module": "flask",
|
13 |
+
"env": {
|
14 |
+
"FLASK_APP": "app.py",
|
15 |
+
"FLASK_DEBUG": "1"
|
16 |
+
},
|
17 |
+
"args": [
|
18 |
+
"run",
|
19 |
+
//"--no-debugger",
|
20 |
+
//"--no-reload"
|
21 |
+
],
|
22 |
+
"jinja": true,
|
23 |
+
"justMyCode": true
|
24 |
+
}
|
25 |
+
]
|
26 |
+
}
|
README.md
ADDED
@@ -0,0 +1,7 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# KI-Anwendungen-n-to-n-backend
|
2 |
+
|
3 |
+
Flask install: https://flask.palletsprojects.com/en/2.2.x/installation/
|
4 |
+
Flask quickstart: https://flask.palletsprojects.com/en/2.2.x/quickstart/
|
5 |
+
Check that the correct envierment is active.
|
6 |
+
|
7 |
+
Pythonanywhere (to deploy the backend): https://www.pythonanywhere.com/
|
__pycache__/app.cpython-310-DESKTOP-DIP1FRF.pyc
ADDED
Binary file (3.68 kB). View file
|
|
__pycache__/app.cpython-310.pyc
ADDED
Binary file (3.18 kB). View file
|
|
app.py
ADDED
@@ -0,0 +1,127 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from flask import Flask, request, jsonify
|
2 |
+
from flask_cors import CORS
|
3 |
+
from tensorflow import keras
|
4 |
+
from keras import layers
|
5 |
+
import numpy as np
|
6 |
+
from PIL import Image
|
7 |
+
from io import BytesIO
|
8 |
+
import base64
|
9 |
+
import os
|
10 |
+
|
11 |
+
app = Flask(__name__)
|
12 |
+
CORS(app)
|
13 |
+
app.config['MAX_CONTENT_LENGTH'] = 16 * 1000 * 1000
|
14 |
+
|
15 |
+
cats_and_dogs_model = None
|
16 |
+
|
17 |
+
# Load and train the cats and dogs model
|
18 |
+
def load_cats_and_dogs_model():
|
19 |
+
# Define the model architecture
|
20 |
+
model = keras.Sequential([
|
21 |
+
layers.Conv2D(32, (3, 3), activation='relu', input_shape=(28, 28, 3)),
|
22 |
+
layers.MaxPooling2D((2, 2)),
|
23 |
+
layers.Conv2D(64, (3, 3), activation='relu'),
|
24 |
+
layers.MaxPooling2D((2, 2)),
|
25 |
+
layers.Conv2D(64, (3, 3), activation='relu'),
|
26 |
+
layers.Flatten(),
|
27 |
+
layers.Dense(64, activation='relu'),
|
28 |
+
layers.Dense(1, activation='sigmoid')
|
29 |
+
])
|
30 |
+
|
31 |
+
# Compile the model
|
32 |
+
model.compile(optimizer='adam',
|
33 |
+
loss='binary_crossentropy',
|
34 |
+
metrics=['accuracy'])
|
35 |
+
|
36 |
+
# Load or generate your training data (X_train and y_train)
|
37 |
+
dataset_directory = 'C:\\Users\\chant\\OneDrive - ZHAW\\ZHAW\\Semester 6\\KI Anwendungen\\Tutorial\\Project2\\dataset'
|
38 |
+
train_directory = os.path.join(dataset_directory, 'train')
|
39 |
+
test_directory = os.path.join(dataset_directory, 'test')
|
40 |
+
train_cats_directory = os.path.join(train_directory, 'cats')
|
41 |
+
train_dogs_directory = os.path.join(train_directory, 'dogs')
|
42 |
+
test_cats_directory = os.path.join(test_directory, 'cats')
|
43 |
+
test_dogs_directory = os.path.join(test_directory, 'dogs')
|
44 |
+
|
45 |
+
train_images = []
|
46 |
+
train_labels = []
|
47 |
+
test_images = []
|
48 |
+
test_labels = []
|
49 |
+
|
50 |
+
for filename in os.listdir(train_cats_directory):
|
51 |
+
if filename.endswith('.jpg'):
|
52 |
+
img = Image.open(os.path.join(train_cats_directory, filename))
|
53 |
+
img = img.resize((28, 28))
|
54 |
+
img = img.convert('RGB')
|
55 |
+
img = np.array(img)
|
56 |
+
train_images.append(img)
|
57 |
+
train_labels.append(0) # Assign label 0 for cats
|
58 |
+
|
59 |
+
for filename in os.listdir(train_dogs_directory):
|
60 |
+
if filename.endswith('.jpg'):
|
61 |
+
img = Image.open(os.path.join(train_dogs_directory, filename))
|
62 |
+
img = img.resize((28, 28))
|
63 |
+
img = img.convert('RGB')
|
64 |
+
img = np.array(img)
|
65 |
+
train_images.append(img)
|
66 |
+
train_labels.append(1) # Assign label 1 for dogs
|
67 |
+
|
68 |
+
for filename in os.listdir(test_cats_directory):
|
69 |
+
if filename.endswith('.jpg'):
|
70 |
+
img = Image.open(os.path.join(test_cats_directory, filename))
|
71 |
+
img = img.resize((28, 28))
|
72 |
+
img = img.convert('RGB')
|
73 |
+
img = np.array(img)
|
74 |
+
test_images.append(img)
|
75 |
+
test_labels.append(0) # Assign label 0 for cats
|
76 |
+
|
77 |
+
for filename in os.listdir(test_dogs_directory):
|
78 |
+
if filename.endswith('.jpg'):
|
79 |
+
img = Image.open(os.path.join(test_dogs_directory, filename))
|
80 |
+
img = img.resize((28, 28))
|
81 |
+
img = img.convert('RGB')
|
82 |
+
img = np.array(img)
|
83 |
+
test_images.append(img)
|
84 |
+
test_labels.append(1) # Assign label 1 for dogs
|
85 |
+
|
86 |
+
X_train = np.array(train_images)
|
87 |
+
y_train = np.array(train_labels)
|
88 |
+
X_test = np.array(test_images)
|
89 |
+
y_test = np.array(test_labels)
|
90 |
+
|
91 |
+
# Preprocess the data
|
92 |
+
X_train = X_train.astype('float32') / 255
|
93 |
+
X_test = X_test.astype('float32') / 255
|
94 |
+
|
95 |
+
# Train the model
|
96 |
+
model.fit(X_train, y_train, epochs=10, batch_size=32)
|
97 |
+
|
98 |
+
# Evaluate the model
|
99 |
+
test_loss, test_acc = model.evaluate(X_test, y_test, verbose=2)
|
100 |
+
print('Test accuracy:', test_acc)
|
101 |
+
|
102 |
+
# Set the trained model as the global variable
|
103 |
+
global cats_and_dogs_model
|
104 |
+
cats_and_dogs_model = model
|
105 |
+
|
106 |
+
# Define the route for image classification
|
107 |
+
@app.route('/api/prediction/classify', methods=['POST'])
|
108 |
+
def classify_image():
|
109 |
+
data = request.get_json()
|
110 |
+
image_data = data['image']
|
111 |
+
image = Image.open(BytesIO(base64.b64decode(image_data)))
|
112 |
+
image = image.resize((28, 28))
|
113 |
+
image = image.convert('RGB')
|
114 |
+
image = np.array(image)
|
115 |
+
image = image.astype('float32') / 255
|
116 |
+
image = np.expand_dims(image, axis=0)
|
117 |
+
result = cats_and_dogs_model.predict(image)[0][0]
|
118 |
+
class_name = 'cat' if result < 0.5 else 'dog'
|
119 |
+
response = {'class_name': class_name, 'confidence': float(result)}
|
120 |
+
return jsonify(response)
|
121 |
+
|
122 |
+
# Add this if statement to start the Flask app
|
123 |
+
if __name__ == "__main__" or __name__ == "app" or __name__ == "flask_app":
|
124 |
+
print(("* Loading models and starting the server..."
|
125 |
+
"please wait until the server has fully started"))
|
126 |
+
load_cats_and_dogs_model()
|
127 |
+
app.run()
|
canvas.png
ADDED
canvas2.png
ADDED
cat.png
ADDED
cats_model/fingerprint.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:4ced7eec16e65eeba652f6d677790346d3f9a43cf2c72a8a4addcbd0dad1f53b
|
3 |
+
size 56
|
cats_model/keras_metadata.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:e4d174102dab739f97718105b0ae47d5d071a7d2b9c78e02b594b83347eea027
|
3 |
+
size 542338
|
cats_model/saved_model.pb
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:887f94167a26c6c76a6f3a90275240df8e759882a7ba43b9b1115f62376cac71
|
3 |
+
size 3516089
|
cats_model/variables/variables.data-00000-of-00001
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:27874b2153417411c21894ba1ab599f9d9307e23b80d598a827d9708951fddb0
|
3 |
+
size 9190536
|
cats_model/variables/variables.index
ADDED
Binary file (14.9 kB). View file
|
|
knn_clf.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:68b1016e93945e36975c7f6e3db3384c1a84f0be4bc349beb982e23b7ac2fd00
|
3 |
+
size 62800749
|
randomforest_regression.pkl
ADDED
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
1 |
+
version https://git-lfs.github.com/spec/v1
|
2 |
+
oid sha256:31ac826a662e888ecbfe8ab1fe905e35ffc85c0b65364524eed16c0bad3a645c
|
3 |
+
size 8179557
|
requirements.txt
ADDED
@@ -0,0 +1,26 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
black==23.1.0
|
2 |
+
click==8.1.3
|
3 |
+
colorama==0.4.6
|
4 |
+
Flask==2.2.3
|
5 |
+
Flask-Cors==3.0.10
|
6 |
+
itsdangerous==2.1.2
|
7 |
+
Jinja2==3.1.2
|
8 |
+
joblib==1.2.0
|
9 |
+
MarkupSafe==2.1.2
|
10 |
+
mypy-extensions==1.0.0
|
11 |
+
numpy==1.24.2
|
12 |
+
packaging==23.0
|
13 |
+
pandas==1.5.3
|
14 |
+
pathspec==0.11.0
|
15 |
+
platformdirs==3.1.0
|
16 |
+
Pygments==2.14.0
|
17 |
+
pypandoc==1.11
|
18 |
+
python-dateutil==2.8.2
|
19 |
+
pytz==2022.7.1
|
20 |
+
scikit-learn==1.2.1
|
21 |
+
scipy==1.10.1
|
22 |
+
six==1.16.0
|
23 |
+
superprompt==1.2.0
|
24 |
+
threadpoolctl==3.1.0
|
25 |
+
tomli==2.0.1
|
26 |
+
Werkzeug==2.2.3
|