wth-ishu commited on
Commit
311e1d5
·
verified ·
1 Parent(s): 0a92089

Upload 2 files

Browse files
Files changed (2) hide show
  1. app.py +96 -0
  2. requirements.txt +9 -9
app.py ADDED
@@ -0,0 +1,96 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ from ultralytics import YOLO
4
+ import numpy as np
5
+ import cv2
6
+ import io
7
+ import os
8
+ from dotenv import load_dotenv
9
+ from azure.storage.blob import BlobServiceClient
10
+
11
+ # Load environment variables from .env file
12
+ load_dotenv()
13
+
14
+ # Initialize Flask app
15
+ app = Flask(__name__)
16
+ CORS(app, resources={r"/*": {"origins": os.getenv("ORIGINS")}}) # Allow requests from specified origins
17
+
18
+ # Get Azure Blob Storage connection string from environment variable
19
+ connect_str = os.getenv("AZURE_STORAGE_CONNECTION_STRING")
20
+ blob_service_client = BlobServiceClient.from_connection_string(connect_str)
21
+ container_name = os.getenv("AZURE_BLOB_CONTAINER_NAME")
22
+ blob_name = os.getenv("AZURE_BLOB_MODEL_PATH") # Path to your model within the container
23
+
24
+ model = None
25
+
26
+ def load_model():
27
+ global model
28
+ try:
29
+ container_client = blob_service_client.get_container_client(container_name)
30
+ blob_client = container_client.get_blob_client(blob_name)
31
+ model_data = blob_client.download_blob().readall()
32
+ model = YOLO(io.BytesIO(model_data)) # Load YOLO model from bytes
33
+ print("YOLO model loaded successfully from Azure Blob Storage.")
34
+ except Exception as e:
35
+ print(f"Error loading model from Azure Blob Storage: {e}")
36
+ print(f"Container Name: {container_name}")
37
+ print(f"Blob Name: {blob_name}")
38
+ return jsonify({"error": f"Failed to load model: {e}"}), 500
39
+
40
+
41
+ with app.app_context():
42
+ # def before_first_request():
43
+ print("💮 Loading YOLO model...")
44
+ load_model()
45
+
46
+ @app.route("/")
47
+ def home():
48
+ return jsonify({"message": "Welcome to the YOLOv5 API!", "host": request.host, "url": request.url})
49
+
50
+ @app.route("/predict", methods=["POST"])
51
+ def predict():
52
+ """Receive an image from the frontend, run YOLO model, and return detections."""
53
+ if "file" not in request.files:
54
+ return jsonify({"error": "No file uploaded"}), 400
55
+
56
+ file = request.files["file"]
57
+ # Read image file as a byte stream and convert to numpy array using OpenCV
58
+ file_bytes = np.frombuffer(file.read(), np.uint8)
59
+ image_array = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
60
+
61
+ try:
62
+ results = model.predict(image_array)
63
+ predictions = {}
64
+
65
+ # Process detections
66
+ for result in results:
67
+ for box in result.boxes:
68
+ class_id = int(box.cls[0].item()) # Class ID
69
+ label = model.names[class_id] # Class name
70
+ confidence = round(box.conf[0].item(), 2) # Confidence score
71
+ x1, y1, x2, y2 = map(int, box.xyxy[0]) # Bounding box coordinates
72
+
73
+ prediction = {
74
+ "bbox": [x1, y1, x2, y2],
75
+ "class_id": class_id,
76
+ "confidence": confidence
77
+ }
78
+ # Ensure the label name doesn't contain spaces and is lowercase e.g. "root_piece"
79
+ label = label.replace(" ", "_").lower()
80
+ if label not in predictions:
81
+ predictions[label] = []
82
+ predictions[label].append(prediction)
83
+
84
+ except Exception as e:
85
+ return jsonify({"error": str(e)}), 500
86
+
87
+ return jsonify({"prediction": predictions})
88
+
89
+ if __name__ == "__main__":
90
+ if os.getenv("ENVIRONMENT") == "production":
91
+ # Use Gunicorn to run the app in production
92
+ from gunicorn.app.wsgiapp import run
93
+ run()
94
+ else:
95
+ # Use Flask's built-in server for local development
96
+ app.run(debug=True, host='0.0.0.0', port=5000)
requirements.txt CHANGED
@@ -1,9 +1,9 @@
1
- streamlit
2
- plotly
3
- ultralytics
4
- opencv-python
5
- numpy
6
- pillow
7
- pandas
8
- streamlit-lottie
9
- requests
 
1
+ Flask==3.1.0
2
+ Flask-Cors==5.0.0
3
+ numpy==2.1.1
4
+ opencv-python==4.11.0.86
5
+ ultralytics==8.3.70
6
+ dill==0.3.9
7
+ python-dotenv==1.0.1
8
+ gunicorn==23.0.0
9
+ azure-storage-blob==12.24.1