Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -5,22 +5,22 @@ import requests
|
|
| 5 |
from flask import Flask, render_template, request, jsonify
|
| 6 |
from ultralytics import YOLO
|
| 7 |
from datetime import datetime
|
|
|
|
| 8 |
|
| 9 |
app = Flask(__name__)
|
| 10 |
|
| 11 |
# --- CONFIGURATION ---
|
| 12 |
-
# PASTE YOUR MICROMIND API URL HERE (The one ending in /prediction/...)
|
| 13 |
MICROMIND_API_URL = "https://training.aimicromind.com/api/v1/prediction/606f1092-e94c-42f8-ba6f-09671904a15e"
|
| 14 |
|
| 15 |
-
# GLOBAL SETTINGS
|
| 16 |
current_settings = {
|
| 17 |
-
"target_class_id": 0,
|
| 18 |
"target_label": "person",
|
| 19 |
"confidence": 0.5,
|
| 20 |
"status": "ARMED"
|
| 21 |
}
|
| 22 |
|
| 23 |
-
# Load AI Model
|
| 24 |
print("--- Loading AI Model... ---")
|
| 25 |
try:
|
| 26 |
model = YOLO('yolov8n.pt')
|
|
@@ -28,6 +28,15 @@ try:
|
|
| 28 |
except Exception as e:
|
| 29 |
print(f"⚠️ Model download/load warning: {e}")
|
| 30 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 31 |
@app.route('/')
|
| 32 |
def index():
|
| 33 |
"""Serves the webpage to the user."""
|
|
@@ -46,8 +55,8 @@ def process_frame():
|
|
| 46 |
npimg = np.frombuffer(file, np.uint8)
|
| 47 |
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
|
| 48 |
|
| 49 |
-
# 2. Run YOLO AI
|
| 50 |
-
results = model(img, verbose=False)
|
| 51 |
|
| 52 |
detected = False
|
| 53 |
detections_list = []
|
|
@@ -59,7 +68,7 @@ def process_frame():
|
|
| 59 |
conf = float(box.conf[0])
|
| 60 |
|
| 61 |
# Check against current settings
|
| 62 |
-
if cls_id == current_settings['target_class_id']
|
| 63 |
detected = True
|
| 64 |
detections_list.append({
|
| 65 |
"label": current_settings['target_label'],
|
|
@@ -67,7 +76,7 @@ def process_frame():
|
|
| 67 |
"bbox": box.xyxy[0].tolist()
|
| 68 |
})
|
| 69 |
|
| 70 |
-
# 4. Alert MicroMind (
|
| 71 |
if detected:
|
| 72 |
print(f"🚨 THREAT: {current_settings['target_label']} detected!")
|
| 73 |
|
|
@@ -75,11 +84,11 @@ def process_frame():
|
|
| 75 |
f"Source: Cloud Web Cam\n"
|
| 76 |
f"Confidence: {detections_list[0]['confidence']}")
|
| 77 |
|
| 78 |
-
|
| 79 |
-
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
|
| 83 |
|
| 84 |
return jsonify({
|
| 85 |
"status": "ARMED",
|
|
@@ -121,6 +130,5 @@ def update_settings():
|
|
| 121 |
return jsonify({"status": "success", "current_state": current_settings})
|
| 122 |
|
| 123 |
if __name__ == '__main__':
|
| 124 |
-
# Hugging Face Spaces sets PORT env var to 7860
|
| 125 |
port = int(os.environ.get("PORT", 7860))
|
| 126 |
app.run(host='0.0.0.0', port=port)
|
|
|
|
| 5 |
from flask import Flask, render_template, request, jsonify
|
| 6 |
from ultralytics import YOLO
|
| 7 |
from datetime import datetime
|
| 8 |
+
import threading
|
| 9 |
|
| 10 |
app = Flask(__name__)
|
| 11 |
|
| 12 |
# --- CONFIGURATION ---
|
|
|
|
| 13 |
MICROMIND_API_URL = "https://training.aimicromind.com/api/v1/prediction/606f1092-e94c-42f8-ba6f-09671904a15e"
|
| 14 |
|
| 15 |
+
# GLOBAL SETTINGS
|
| 16 |
current_settings = {
|
| 17 |
+
"target_class_id": 0,
|
| 18 |
"target_label": "person",
|
| 19 |
"confidence": 0.5,
|
| 20 |
"status": "ARMED"
|
| 21 |
}
|
| 22 |
|
| 23 |
+
# Load AI Model
|
| 24 |
print("--- Loading AI Model... ---")
|
| 25 |
try:
|
| 26 |
model = YOLO('yolov8n.pt')
|
|
|
|
| 28 |
except Exception as e:
|
| 29 |
print(f"⚠️ Model download/load warning: {e}")
|
| 30 |
|
| 31 |
+
# Alert sender function
|
| 32 |
+
def send_alert(alert_msg):
|
| 33 |
+
"""Sends alert in background - fully non-blocking."""
|
| 34 |
+
try:
|
| 35 |
+
requests.post(MICROMIND_API_URL, json={"question": alert_msg}, timeout=5)
|
| 36 |
+
print(f"✅ Alert sent to MicroMind")
|
| 37 |
+
except Exception as e:
|
| 38 |
+
print(f"⚠️ Alert sending failed: {e}")
|
| 39 |
+
|
| 40 |
@app.route('/')
|
| 41 |
def index():
|
| 42 |
"""Serves the webpage to the user."""
|
|
|
|
| 55 |
npimg = np.frombuffer(file, np.uint8)
|
| 56 |
img = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
|
| 57 |
|
| 58 |
+
# 2. Run YOLO AI with optimized settings
|
| 59 |
+
results = model(img, verbose=False, conf=current_settings['confidence'])
|
| 60 |
|
| 61 |
detected = False
|
| 62 |
detections_list = []
|
|
|
|
| 68 |
conf = float(box.conf[0])
|
| 69 |
|
| 70 |
# Check against current settings
|
| 71 |
+
if cls_id == current_settings['target_class_id']:
|
| 72 |
detected = True
|
| 73 |
detections_list.append({
|
| 74 |
"label": current_settings['target_label'],
|
|
|
|
| 76 |
"bbox": box.xyxy[0].tolist()
|
| 77 |
})
|
| 78 |
|
| 79 |
+
# 4. Alert MicroMind (Fully Non-blocking)
|
| 80 |
if detected:
|
| 81 |
print(f"🚨 THREAT: {current_settings['target_label']} detected!")
|
| 82 |
|
|
|
|
| 84 |
f"Source: Cloud Web Cam\n"
|
| 85 |
f"Confidence: {detections_list[0]['confidence']}")
|
| 86 |
|
| 87 |
+
# Send alert in separate thread - fully fire-and-forget
|
| 88 |
+
threading.Thread(
|
| 89 |
+
target=lambda: send_alert(alert_msg),
|
| 90 |
+
daemon=True
|
| 91 |
+
).start()
|
| 92 |
|
| 93 |
return jsonify({
|
| 94 |
"status": "ARMED",
|
|
|
|
| 130 |
return jsonify({"status": "success", "current_state": current_settings})
|
| 131 |
|
| 132 |
if __name__ == '__main__':
|
|
|
|
| 133 |
port = int(os.environ.get("PORT", 7860))
|
| 134 |
app.run(host='0.0.0.0', port=port)
|