File size: 5,956 Bytes
01213c6
 
 
 
5801f57
01213c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5801f57
 
 
 
01213c6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from flask import Flask, render_template, request, jsonify, send_from_directory
import base64
import cv2
import numpy as np
from ultralytics import YOLO, RTDETR
import io
from PIL import Image
import time
import torch
import logging
from datetime import datetime

# Configure logging for manusia detection
logging.basicConfig(
    filename='manusia_detection.log',
    level=logging.INFO,
    format='%(asctime)s - %(message)s'
)

print(f"PyTorch CUDA available: {torch.cuda.is_available()}")
print(f"CUDA device count: {torch.cuda.device_count()}")
if torch.cuda.is_available():
    print(f"CUDA device name: {torch.cuda.get_device_name(0)}")

app = Flask(__name__)
app.config['MAX_CONTENT_LENGTH'] = 1 * 1024 * 1024  # Limit uploads to 5MB

@app.route('/audio/<path:filename>')
def serve_audio(filename):
    return send_from_directory('audio', filename)

# Load YOLO model
# model = YOLO('model/best_26102025.pt')

# Load RT-DETR model
model = RTDETR('model/best_rtdetr.pt')

#use GPU
# model.to('cuda')
# print(f"YOLO model loaded on device: {model.device}")

# Model configuration
CONFIDENCE_THRESHOLD = 0.3 # Lower confidence threshold for better detection
IMAGE_SIZE = 480  # Smaller inference size can improve performance

# Human detection tracking
human_detection_state = {
    'first_detected_at': None,
    'is_alarm_active': False,
    'last_detection_time': 0,
    'detection_threshold': 0.05  # 1 seconds
}


@app.route('/')
def index():
    return render_template('screen_share.html')


@app.route('/detect', methods=['POST'])
def detect():
    start_time = time.time()
    try:
        # Get image data from request
        data = request.json
        image_data = data['image']

        # Remove the prefix from base64 data
        if 'data:image/jpeg;base64,' in image_data:
            image_data = image_data.replace('data:image/jpeg;base64,', '')
        elif 'data:image/png;base64,' in image_data:
            image_data = image_data.replace('data:image/png;base64,', '')

        # Decode base64 to image
        image_bytes = base64.b64decode(image_data)
        image = Image.open(io.BytesIO(image_bytes))

        # Convert to OpenCV format
        frame = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)

        # Run detection with optimized parameters
        results = model.predict(
            source=frame,
            conf=CONFIDENCE_THRESHOLD,
            verbose=False,
            imgsz=IMAGE_SIZE,  # Use smaller size for faster inference
            iou=0.5
        )

        # Process results
        detections = []
        human_detected = False

        for result in results:
            boxes = result.boxes.xyxy.cpu().numpy()
            scores = result.boxes.conf.cpu().numpy()
            classes = result.boxes.cls.cpu().numpy()

            for box, score, cls in zip(boxes, scores, classes):
                x1, y1, x2, y2 = map(int, box)
                class_name = model.names[int(cls)]

                # save to log if manusia detected
                if class_name.lower() == 'manusia':
                    detection_time = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
                    logging.info(f"Detected 'manusia' at {detection_time} with confidence {score:.4f}")

                # Check if this is a human/person detection
                if class_name.lower() == 'manusia':
                    human_detected = True

                detections.append({
                    'box': [x1, y1, x2, y2],
                    'class': class_name,
                    'confidence': float(score)
                })

        # Update human detection state
        current_time = time.time()
        alarm_status = check_human_detection(human_detected, current_time)

        processing_time = time.time() - start_time
        return jsonify({
            'success': True,
            'detections': detections,
            'processing_time_ms': round(processing_time * 1000, 2),
            'alarm': alarm_status
        })

    except Exception as e:
        print(f"Error processing image: {str(e)}")
        # Reset human detection on error
        reset_human_detection()
        return jsonify({
            'success': False,
            'error': str(e)
        }), 500


def check_human_detection(human_detected, current_time):
    """Track human detection and determine if alarm should be triggered"""
    global human_detection_state

    if human_detected:
        # If this is the first human detection or there was a gap in detection
        if human_detection_state['first_detected_at'] is None:
            human_detection_state['first_detected_at'] = current_time
            human_detection_state['is_alarm_active'] = False
            return {'active': False}

        # Check if human has been detected for the threshold duration
        elapsed_time = current_time - human_detection_state['first_detected_at']
        if elapsed_time >= human_detection_state['detection_threshold']:
            # Trigger the alarm if not already triggered
            human_detection_state['is_alarm_active'] = True
            return {'active': True, 'duration': elapsed_time}

        # Human detected but threshold not reached
        return {'active': False, 'progress': elapsed_time / human_detection_state['detection_threshold']}
    else:
        # No human detected, reset the tracking
        reset_human_detection()
        return {'active': False}


def reset_human_detection():
    """Reset human detection tracking"""
    global human_detection_state
    human_detection_state['first_detected_at'] = None
    human_detection_state['is_alarm_active'] = False


@app.route('/reset_alarm', methods=['POST'])
def reset_alarm():
    """Endpoint to manually reset the alarm"""
    reset_human_detection()
    return jsonify({'success': True})


if __name__ == '__main__':
    # Use threaded mode for better performance
    app.run(debug=True, host='0.0.0.0', port=7860, threaded=True)