Spaces:
Sleeping
Sleeping
Upload 3 files
Browse files- Dockerfile +36 -0
- app.py +126 -0
- requirements.txt +9 -0
Dockerfile
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Use standard python image
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Install system dependencies
|
| 5 |
+
RUN apt-get update && apt-get install -y \
|
| 6 |
+
libgl1 \
|
| 7 |
+
libglib2.0-0 \
|
| 8 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 9 |
+
|
| 10 |
+
# Create user with ID 1000
|
| 11 |
+
RUN useradd -m -u 1000 user
|
| 12 |
+
|
| 13 |
+
# Set working directory
|
| 14 |
+
WORKDIR /app
|
| 15 |
+
|
| 16 |
+
# Copy requirements
|
| 17 |
+
COPY requirements.txt .
|
| 18 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 19 |
+
|
| 20 |
+
# Download model during build to avoid runtime permission errors
|
| 21 |
+
RUN pip install ultralytics && \
|
| 22 |
+
python -c "from ultralytics import YOLO; YOLO('yolov8n.pt')"
|
| 23 |
+
|
| 24 |
+
# Copy app code and CHANGE OWNER to user
|
| 25 |
+
COPY --chown=user . .
|
| 26 |
+
|
| 27 |
+
# Switch to user
|
| 28 |
+
USER user
|
| 29 |
+
ENV PATH="/home/user/.local/bin:$PATH" \
|
| 30 |
+
PORT=7860
|
| 31 |
+
|
| 32 |
+
# Expose port
|
| 33 |
+
EXPOSE 7860
|
| 34 |
+
|
| 35 |
+
# Run Flask
|
| 36 |
+
CMD ["python", "app.py"]
|
app.py
ADDED
|
@@ -0,0 +1,126 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import cv2
|
| 3 |
+
import numpy as np
|
| 4 |
+
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://intern.aimicromind.com/api/v1/prediction/616027a7-173c-4e52-b627-a4346da1c5c1"
|
| 14 |
+
|
| 15 |
+
# GLOBAL SETTINGS (Controlled by MicroMind Agent later)
|
| 16 |
+
current_settings = {
|
| 17 |
+
"target_class_id": 0, # 0 = person
|
| 18 |
+
"target_label": "person",
|
| 19 |
+
"confidence": 0.5,
|
| 20 |
+
"status": "ARMED"
|
| 21 |
+
}
|
| 22 |
+
|
| 23 |
+
# Load AI Model (Runs once when server starts)
|
| 24 |
+
print("--- Loading AI Model... ---")
|
| 25 |
+
try:
|
| 26 |
+
model = YOLO('yolov8n.pt')
|
| 27 |
+
print("✅ Model Loaded.")
|
| 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."""
|
| 34 |
+
return render_template('index.html')
|
| 35 |
+
|
| 36 |
+
@app.route('/process_frame', methods=['POST'])
|
| 37 |
+
def process_frame():
|
| 38 |
+
"""Receives an image frame from the browser, runs AI, and returns results."""
|
| 39 |
+
|
| 40 |
+
if current_settings['status'] == 'DISARMED':
|
| 41 |
+
return jsonify({"status": "DISARMED", "detections": []})
|
| 42 |
+
|
| 43 |
+
try:
|
| 44 |
+
# 1. Get Image from Request
|
| 45 |
+
file = request.files['image'].read()
|
| 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 = []
|
| 54 |
+
|
| 55 |
+
# 3. Analyze Results
|
| 56 |
+
for result in results:
|
| 57 |
+
for box in result.boxes:
|
| 58 |
+
cls_id = int(box.cls[0])
|
| 59 |
+
conf = float(box.conf[0])
|
| 60 |
+
|
| 61 |
+
# Check against current settings
|
| 62 |
+
if cls_id == current_settings['target_class_id'] and conf > current_settings['confidence']:
|
| 63 |
+
detected = True
|
| 64 |
+
detections_list.append({
|
| 65 |
+
"label": current_settings['target_label'],
|
| 66 |
+
"confidence": round(conf, 2),
|
| 67 |
+
"bbox": box.xyxy[0].tolist()
|
| 68 |
+
})
|
| 69 |
+
|
| 70 |
+
# 4. Alert MicroMind (Fire and Forget)
|
| 71 |
+
if detected:
|
| 72 |
+
print(f"🚨 THREAT: {current_settings['target_label']} detected!")
|
| 73 |
+
|
| 74 |
+
alert_msg = (f"ALERT: {current_settings['target_label']} detected.\n"
|
| 75 |
+
f"Source: Cloud Web Cam\n"
|
| 76 |
+
f"Confidence: {detections_list[0]['confidence']}")
|
| 77 |
+
|
| 78 |
+
try:
|
| 79 |
+
# Send alert to Agent (Timeout is short to keep video smooth)
|
| 80 |
+
requests.post(MICROMIND_API_URL, json={"question": alert_msg}, timeout=0.5)
|
| 81 |
+
except:
|
| 82 |
+
pass
|
| 83 |
+
|
| 84 |
+
return jsonify({
|
| 85 |
+
"status": "ARMED",
|
| 86 |
+
"target": current_settings['target_label'],
|
| 87 |
+
"detections": detections_list
|
| 88 |
+
})
|
| 89 |
+
|
| 90 |
+
except Exception as e:
|
| 91 |
+
print(f"Error: {e}")
|
| 92 |
+
return jsonify({"error": str(e)}), 500
|
| 93 |
+
|
| 94 |
+
@app.route('/update_settings', methods=['POST'])
|
| 95 |
+
def update_settings():
|
| 96 |
+
"""Endpoint for MicroMind Agent to change settings."""
|
| 97 |
+
data = request.json
|
| 98 |
+
|
| 99 |
+
# Simple mapping for demo
|
| 100 |
+
label_map = {
|
| 101 |
+
'person': 0, 'bicycle': 1, 'car': 2, 'motorcycle': 3, 'airplane': 4, 'bus': 5, 'train': 6, 'truck': 7, 'boat': 8,
|
| 102 |
+
'bird': 14, 'cat': 15, 'dog': 16, 'backpack': 24, 'umbrella': 25, 'handbag': 26, 'tie': 27, 'suitcase': 28,
|
| 103 |
+
'bottle': 39, 'cup': 41, 'fork': 42, 'knife': 43, 'spoon': 44, 'bowl': 45, 'banana': 46, 'apple': 47, 'sandwich': 48,
|
| 104 |
+
'chair': 56, 'couch': 57, 'bed': 59, 'dining table': 60, 'laptop': 63, 'mouse': 64, 'remote': 65, 'keyboard': 66,
|
| 105 |
+
'cell phone': 67, 'microwave': 68, 'oven': 69, 'toaster': 70, 'sink': 71, 'refrigerator': 72, 'book': 73,
|
| 106 |
+
'clock': 74, 'vase': 75, 'scissors': 76, 'teddy bear': 77, 'toothbrush': 79
|
| 107 |
+
}
|
| 108 |
+
|
| 109 |
+
if 'label' in data:
|
| 110 |
+
new_label = data['label'].lower()
|
| 111 |
+
if new_label in label_map:
|
| 112 |
+
current_settings['target_label'] = new_label
|
| 113 |
+
current_settings['target_class_id'] = label_map[new_label]
|
| 114 |
+
|
| 115 |
+
if 'confidence' in data:
|
| 116 |
+
current_settings['confidence'] = float(data['confidence'])
|
| 117 |
+
|
| 118 |
+
if 'status' in data:
|
| 119 |
+
current_settings['status'] = data['status'].upper()
|
| 120 |
+
|
| 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)
|
requirements.txt
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
fastapi
|
| 2 |
+
uvicorn
|
| 3 |
+
gradio>=4.0.0
|
| 4 |
+
ultralytics
|
| 5 |
+
opencv-python
|
| 6 |
+
requests
|
| 7 |
+
pydantic
|
| 8 |
+
python-multipart
|
| 9 |
+
huggingface-hub==0.22.2
|