forked from Vidhaankhare16/CCS-HACK-TU
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
73 lines (64 loc) · 2.25 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
import uvicorn
from fastapi import FastAPI
import joblib
import tensorflow as tf
from tensorflow import keras
from fastapi import File, UploadFile
from fastapi.middleware.cors import CORSMiddleware
from PIL import Image
import io
app = FastAPI()
#joblib_in = open()
model = tf.keras.models.load_model('model.keras')
@app.get('/')
def index():
return {'message': 'Crack Identifier ML API'}
app.add_middleware(
CORSMiddleware,
allow_origins=["*"], # Change this to your frontend URL in production
allow_credentials=True,
allow_methods=["*"],
allow_headers=["*"],
)
@app.get('/image/predict') #post banao
def predict_crack_type():
def predict_crack(image_path):
img = tf.keras.preprocessing.image.load_img(
image_path, target_size=(300, 300)
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) / 255.0
prediction = model.predict(img_array)
return "Crack Detected" if prediction[0][0] > 0.5 else "No Crack Found"
text = predict_crack('/home/harsh/Desktop/hacktu6/Broken_railroad_tracks_1.jpg')
#data = data.model_dump()
#Image = Image
#prediction = model.predict([Image])
#return {
# 'prediction': prediction[0]
#}
return {'message': text}
@app.post("/upload/")
async def upload_image(file: UploadFile = File(...)):
# Read and process image
image = Image.open(io.BytesIO(await file.read()))
image = image.convert("L") # Example: Convert to grayscale
def predict_crack(image_path):
img = tf.keras.preprocessing.image.load_img(
image_path, target_size=(300, 300)
)
img_array = tf.keras.preprocessing.image.img_to_array(img)
img_array = tf.expand_dims(img_array, 0) / 255.0
prediction = model.predict(img_array)
return "Crack Detected" if prediction[0][0] > 0.5 else "No Crack Found"
text = predict_crack(image)
#data = data.model_dump()
#Image = Image
#prediction = model.predict([Image])
#return {
# 'prediction': prediction[0]
#}
return {'message': text}
return {"filename": file.filename, "message": "Image processed successfully"}
if __name__ == '__main__':
uvicorn.run(app, host='127.0.0.1', port=8080)