Update handler.py
Browse files- handler.py +61 -55
handler.py
CHANGED
|
@@ -4,58 +4,64 @@ from torchvision import transforms
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
model
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 4 |
from PIL import Image
|
| 5 |
import io
|
| 6 |
|
| 7 |
+
# Import your Faster R-CNN model definition
|
| 8 |
+
from model import get_model
|
| 9 |
+
|
| 10 |
+
class EndpointHandler:
|
| 11 |
+
def __init__(self, path: str = ""):
|
| 12 |
+
"""
|
| 13 |
+
Initialize the handler. Load the Faster R-CNN model.
|
| 14 |
+
"""
|
| 15 |
+
self.device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
| 16 |
+
self.model_weights_path = os.path.join(path, "model.pt") # Adjust for your file name
|
| 17 |
+
|
| 18 |
+
# Load the model
|
| 19 |
+
self.model = get_model(num_classes=4) # Modify for your num_classes
|
| 20 |
+
print(f"Loading weights from: {self.model_weights_path}")
|
| 21 |
+
checkpoint = torch.load(self.model_weights_path, map_location=self.device)
|
| 22 |
+
self.model.load_state_dict(checkpoint["model_state_dict"])
|
| 23 |
+
self.model.to(self.device)
|
| 24 |
+
self.model.eval()
|
| 25 |
+
|
| 26 |
+
# Define image preprocessing
|
| 27 |
+
self.transform = transforms.Compose([
|
| 28 |
+
transforms.Resize((640, 640)), # Adjust size to match your training setup
|
| 29 |
+
transforms.ToTensor(),
|
| 30 |
+
])
|
| 31 |
+
|
| 32 |
+
def __call__(self, data):
|
| 33 |
+
"""
|
| 34 |
+
Process the incoming request and return object detection predictions.
|
| 35 |
+
"""
|
| 36 |
+
try:
|
| 37 |
+
# Expect input data to include a Base64-encoded image
|
| 38 |
+
if "image" not in data:
|
| 39 |
+
return [{"error": "No 'image' provided in request."}]
|
| 40 |
+
|
| 41 |
+
# Convert Base64-encoded image to bytes
|
| 42 |
+
image_bytes = data["image"].encode("latin1")
|
| 43 |
+
image = Image.open(io.BytesIO(image_bytes)).convert("RGB")
|
| 44 |
+
|
| 45 |
+
# Preprocess the image
|
| 46 |
+
input_tensor = self.transform(image).unsqueeze(0).to(self.device)
|
| 47 |
+
|
| 48 |
+
# Run inference
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
outputs = self.model(input_tensor)
|
| 51 |
+
|
| 52 |
+
# Extract results
|
| 53 |
+
boxes = outputs[0]["boxes"].cpu().tolist()
|
| 54 |
+
labels = outputs[0]["labels"].cpu().tolist()
|
| 55 |
+
scores = outputs[0]["scores"].cpu().tolist()
|
| 56 |
+
|
| 57 |
+
# Confidence threshold
|
| 58 |
+
threshold = 0.5
|
| 59 |
+
predictions = [
|
| 60 |
+
{"box": box, "label": label, "score": score}
|
| 61 |
+
for box, label, score in zip(boxes, labels, scores)
|
| 62 |
+
if score > threshold
|
| 63 |
+
]
|
| 64 |
+
|
| 65 |
+
return [{"predictions": predictions}]
|
| 66 |
+
except Exception as e:
|
| 67 |
+
return [{"error": str(e)}]
|