-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
53 lines (41 loc) · 1.54 KB
/
main.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
from flask import Flask, render_template, redirect, url_for, request
import cv2
import tensorflow as tf
import numpy as np
import os
CATEGORIES = ['Dog','Cat']
app = Flask(__name__)
app.secret_key = "qazwsxedcrfvtgbyhnujmik,olbndphaphpanpnn23p"
model = tf.keras.models.load_model('model')
def prepare(path):
IMG_SIZE = 100
IMG_ARRAY = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
new_array = cv2.resize(IMG_ARRAY, (IMG_SIZE, IMG_SIZE))
return new_array.reshape(-1, IMG_SIZE, IMG_SIZE, 1)[0]
def empty_static_folder(folder_path):
for filename in os.listdir(folder_path):
file_path = os.path.join(folder_path, filename)
try:
if os.path.isfile(file_path):
os.unlink(file_path)
elif os.path.isdir(file_path):
empty_static_folder(file_path)
os.rmdir(file_path)
except Exception as e:
print(f"Error deleting {file_path}: {e}")
@app.route('/', methods= ['GET','POST'])
def index():
empty_static_folder('static')
if request.method == 'POST':
image = request.files['file']
path = 'static/'+str(image.filename)
image.save(path)
prediction_input = np.array([prepare(path)])
prediction = model.predict(prediction_input)
predicted_class = int(prediction[0][0])
data = CATEGORIES[predicted_class]
return render_template('image.html', prediction=data, path=path)
else:
return render_template('image.html')
if __name__ == '__main__':
app.run(debug=True)