Spaces:
Running
Running
Use only image urls
Browse files- main.py +11 -8
- utils/image_preclassification.py +4 -7
- utils/palmoil_classification.py +3 -2
- utils/preprocessing.py +2 -0
main.py
CHANGED
|
@@ -11,6 +11,7 @@ import io
|
|
| 11 |
import numpy as np
|
| 12 |
from io import BytesIO
|
| 13 |
from pydantic import BaseModel
|
|
|
|
| 14 |
|
| 15 |
|
| 16 |
# Logging
|
|
@@ -22,7 +23,6 @@ description = """
|
|
| 22 |
"""
|
| 23 |
|
| 24 |
class ImageRequest(BaseModel):
|
| 25 |
-
image: str
|
| 26 |
imageURL:str
|
| 27 |
lang: int
|
| 28 |
|
|
@@ -49,23 +49,23 @@ async def home():
|
|
| 49 |
# red palm oil classification endpoint
|
| 50 |
@app.post("/predict")
|
| 51 |
async def predict(image_request: ImageRequest):
|
| 52 |
-
logging.info("Loading image")
|
| 53 |
|
| 54 |
-
# Decode base64 image string
|
| 55 |
-
decoded_image = base64.b64decode(image_request.image)
|
| 56 |
|
| 57 |
-
# Create a BytesIO object to read the image data
|
| 58 |
-
image_bytes = BytesIO(decoded_image)
|
| 59 |
|
| 60 |
try:
|
| 61 |
# Pre-classify image
|
| 62 |
-
is_palm_oil = pre_classification(image_request.imageURL)
|
| 63 |
|
| 64 |
logging.info("Pre-classification successful")
|
| 65 |
|
| 66 |
if is_palm_oil:
|
| 67 |
model = AfroPalmModel()
|
| 68 |
-
prediction,confidence = model.predict(
|
| 69 |
logging.debug(f"Prediction: {prediction}, Confidence: {confidence*100:.2f}%")
|
| 70 |
|
| 71 |
# Generate audio
|
|
@@ -79,6 +79,9 @@ async def predict(image_request: ImageRequest):
|
|
| 79 |
|
| 80 |
# model = AfroPalmModel()
|
| 81 |
# prediction,confidence = model.predict(image_bytes)
|
|
|
|
|
|
|
|
|
|
| 82 |
|
| 83 |
return {
|
| 84 |
"status": "success",
|
|
|
|
| 11 |
import numpy as np
|
| 12 |
from io import BytesIO
|
| 13 |
from pydantic import BaseModel
|
| 14 |
+
import os
|
| 15 |
|
| 16 |
|
| 17 |
# Logging
|
|
|
|
| 23 |
"""
|
| 24 |
|
| 25 |
class ImageRequest(BaseModel):
|
|
|
|
| 26 |
imageURL:str
|
| 27 |
lang: int
|
| 28 |
|
|
|
|
| 49 |
# red palm oil classification endpoint
|
| 50 |
@app.post("/predict")
|
| 51 |
async def predict(image_request: ImageRequest):
|
| 52 |
+
# logging.info("Loading image")
|
| 53 |
|
| 54 |
+
# # Decode base64 image string
|
| 55 |
+
# decoded_image = base64.b64decode(image_request.image)
|
| 56 |
|
| 57 |
+
# # Create a BytesIO object to read the image data
|
| 58 |
+
# image_bytes = BytesIO(decoded_image)
|
| 59 |
|
| 60 |
try:
|
| 61 |
# Pre-classify image
|
| 62 |
+
is_palm_oil, image_path = pre_classification(image_request.imageURL)
|
| 63 |
|
| 64 |
logging.info("Pre-classification successful")
|
| 65 |
|
| 66 |
if is_palm_oil:
|
| 67 |
model = AfroPalmModel()
|
| 68 |
+
prediction,confidence = model.predict(image_path)
|
| 69 |
logging.debug(f"Prediction: {prediction}, Confidence: {confidence*100:.2f}%")
|
| 70 |
|
| 71 |
# Generate audio
|
|
|
|
| 79 |
|
| 80 |
# model = AfroPalmModel()
|
| 81 |
# prediction,confidence = model.predict(image_bytes)
|
| 82 |
+
if os.path.isfile(image_path):
|
| 83 |
+
# Remove the file after processing
|
| 84 |
+
os.remove(image_path)
|
| 85 |
|
| 86 |
return {
|
| 87 |
"status": "success",
|
utils/image_preclassification.py
CHANGED
|
@@ -49,20 +49,17 @@ def pre_classification(imageURL):
|
|
| 49 |
class_name = result["class"]
|
| 50 |
confidence = result["confidence"]
|
| 51 |
|
| 52 |
-
|
| 53 |
-
if os.path.isfile(image_path):
|
| 54 |
-
# Remove the file after processing
|
| 55 |
-
os.remove(image_path)
|
| 56 |
|
| 57 |
if class_name == "palmoil":
|
| 58 |
-
return True
|
| 59 |
|
| 60 |
else:
|
| 61 |
-
return False
|
| 62 |
|
| 63 |
|
| 64 |
else:
|
| 65 |
-
return False
|
| 66 |
|
| 67 |
else:
|
| 68 |
logging.error("Error loading the image for pre-classification")
|
|
|
|
| 49 |
class_name = result["class"]
|
| 50 |
confidence = result["confidence"]
|
| 51 |
|
| 52 |
+
|
|
|
|
|
|
|
|
|
|
| 53 |
|
| 54 |
if class_name == "palmoil":
|
| 55 |
+
return True , image_path
|
| 56 |
|
| 57 |
else:
|
| 58 |
+
return False, image_path
|
| 59 |
|
| 60 |
|
| 61 |
else:
|
| 62 |
+
return False, image_path
|
| 63 |
|
| 64 |
else:
|
| 65 |
logging.error("Error loading the image for pre-classification")
|
utils/palmoil_classification.py
CHANGED
|
@@ -36,7 +36,7 @@ class AfroPalmModel:
|
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
-
def predict(self,
|
| 40 |
"""
|
| 41 |
Make a prediction on the image
|
| 42 |
:param image: image to make prediction on
|
|
@@ -45,7 +45,7 @@ class AfroPalmModel:
|
|
| 45 |
|
| 46 |
logging.info("Making prediction")
|
| 47 |
|
| 48 |
-
img = preprocess_image(
|
| 49 |
|
| 50 |
logging.debug(f'Image preprocessed with shape {np.array(img).shape}')
|
| 51 |
|
|
@@ -56,4 +56,5 @@ class AfroPalmModel:
|
|
| 56 |
|
| 57 |
logging.info("Classification successful")
|
| 58 |
|
|
|
|
| 59 |
return predictions.index(max(predictions)), max(predictions)
|
|
|
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
+
def predict(self, image_path):
|
| 40 |
"""
|
| 41 |
Make a prediction on the image
|
| 42 |
:param image: image to make prediction on
|
|
|
|
| 45 |
|
| 46 |
logging.info("Making prediction")
|
| 47 |
|
| 48 |
+
img = preprocess_image(image_path)
|
| 49 |
|
| 50 |
logging.debug(f'Image preprocessed with shape {np.array(img).shape}')
|
| 51 |
|
|
|
|
| 56 |
|
| 57 |
logging.info("Classification successful")
|
| 58 |
|
| 59 |
+
|
| 60 |
return predictions.index(max(predictions)), max(predictions)
|
utils/preprocessing.py
CHANGED
|
@@ -27,8 +27,10 @@ def preprocess_image(image):
|
|
| 27 |
if img.shape[-1] == 1: # Grayscale image
|
| 28 |
img = np.stack([img, img, img], axis=-1)
|
| 29 |
elif img.shape[-1] != 3: # Not RGB or Grayscale
|
|
|
|
| 30 |
raise ValueError("Input image must be in RGB or Grayscale format")
|
| 31 |
else:
|
|
|
|
| 32 |
raise ValueError("Unsupported image type")
|
| 33 |
|
| 34 |
# Define the transformations
|
|
|
|
| 27 |
if img.shape[-1] == 1: # Grayscale image
|
| 28 |
img = np.stack([img, img, img], axis=-1)
|
| 29 |
elif img.shape[-1] != 3: # Not RGB or Grayscale
|
| 30 |
+
logging.error("Input image must be in RGB or Grayscale format")
|
| 31 |
raise ValueError("Input image must be in RGB or Grayscale format")
|
| 32 |
else:
|
| 33 |
+
logging.error("Unsupported image type")
|
| 34 |
raise ValueError("Unsupported image type")
|
| 35 |
|
| 36 |
# Define the transformations
|