Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,74 +1,31 @@
|
|
| 1 |
-
import
|
| 2 |
-
import
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
import tensorflow as tf
|
| 5 |
-
import
|
| 6 |
-
|
| 7 |
-
# Ensure deterministic execution and avoid random_device
|
| 8 |
-
os.environ["PYTHONHASHSEED"] = "0"
|
| 9 |
-
random.seed(0)
|
| 10 |
-
np.random.seed(0)
|
| 11 |
-
tf.random.set_seed(0)
|
| 12 |
-
torch.manual_seed(0)
|
| 13 |
-
torch.use_deterministic_algorithms(True)
|
| 14 |
-
torch.set_deterministic_debug_mode("warn")
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
from segment_anything import sam_model_registry, SamPredictor
|
| 18 |
|
| 19 |
app = FastAPI()
|
| 20 |
|
| 21 |
-
#
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
# Load the classification model
|
| 25 |
-
model = tf.keras.models.load_model("1.keras")
|
| 26 |
CLASS_NAMES = ['Fungi', 'Healthy', 'Nematode', 'Pest', 'Phytopthora', 'Virus']
|
| 27 |
|
| 28 |
-
# Load the SAM model
|
| 29 |
-
DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
|
| 30 |
-
SAM_CHECKPOINT_PATH = "C:/Users/lenovo/Downloads/models/sam_model/sam_vit_b.pth"
|
| 31 |
-
|
| 32 |
-
sam = sam_model_registry["vit_b"](checkpoint=SAM_CHECKPOINT_PATH).to(DEVICE)
|
| 33 |
-
predictor = SamPredictor(sam)
|
| 34 |
-
|
| 35 |
@app.post("/predict")
|
| 36 |
-
async def predict(
|
| 37 |
-
file: UploadFile = File(...),
|
| 38 |
-
x_api_key: str = Header(None)
|
| 39 |
-
):
|
| 40 |
-
# Check API key
|
| 41 |
-
if x_api_key != API_KEY:
|
| 42 |
-
raise HTTPException(status_code=401, detail="Invalid API Key")
|
| 43 |
-
|
| 44 |
try:
|
| 45 |
-
# Load and prepare the input image
|
| 46 |
contents = await file.read()
|
| 47 |
-
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 48 |
-
image_np = np.array(image)
|
| 49 |
-
|
| 50 |
-
# Convert to BGR for SAM
|
| 51 |
-
image_bgr = cv2.cvtColor(image_np, cv2.COLOR_RGB2BGR)
|
| 52 |
|
| 53 |
-
#
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
input_point = np.array([[W // 2, H // 2]])
|
| 57 |
-
input_label = np.array([1])
|
| 58 |
-
masks, scores, _ = predictor.predict(point_coords=input_point, point_labels=input_label, multimask_output=False)
|
| 59 |
-
|
| 60 |
-
# Apply the mask
|
| 61 |
-
mask = masks[0]
|
| 62 |
-
segmented = image_np * np.expand_dims(mask, axis=-1)
|
| 63 |
|
| 64 |
-
#
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
arr = np.array(segmented_pil).astype("float32")
|
| 68 |
-
arr = np.expand_dims(arr, axis=0)
|
| 69 |
|
| 70 |
# Predict
|
| 71 |
-
prediction = model.predict(
|
| 72 |
predicted_class = int(np.argmax(prediction[0]))
|
| 73 |
predicted_label = CLASS_NAMES[predicted_class]
|
| 74 |
|
|
|
|
| 1 |
+
from fastapi import FastAPI, UploadFile, File
|
| 2 |
+
from fastapi.responses import JSONResponse
|
| 3 |
+
from PIL import Image
|
| 4 |
import numpy as np
|
| 5 |
import tensorflow as tf
|
| 6 |
+
import io
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
|
| 8 |
app = FastAPI()
|
| 9 |
|
| 10 |
+
# Load the model
|
| 11 |
+
model = tf.keras.models.load_model("2.keras")
|
|
|
|
|
|
|
|
|
|
| 12 |
CLASS_NAMES = ['Fungi', 'Healthy', 'Nematode', 'Pest', 'Phytopthora', 'Virus']
|
| 13 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
@app.post("/predict")
|
| 15 |
+
async def predict(file: UploadFile = File(...)):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 16 |
try:
|
|
|
|
| 17 |
contents = await file.read()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 18 |
|
| 19 |
+
# Load and process image
|
| 20 |
+
image = Image.open(io.BytesIO(contents)).convert("RGB")
|
| 21 |
+
image = image.resize((224, 224))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 22 |
|
| 23 |
+
# Don't normalize — match tf.data image_dataset_from_directory
|
| 24 |
+
img_array = np.array(image).astype("float32")
|
| 25 |
+
img_array = np.expand_dims(img_array, axis=0) # (1, 224, 224, 3)
|
|
|
|
|
|
|
| 26 |
|
| 27 |
# Predict
|
| 28 |
+
prediction = model.predict(img_array)
|
| 29 |
predicted_class = int(np.argmax(prediction[0]))
|
| 30 |
predicted_label = CLASS_NAMES[predicted_class]
|
| 31 |
|