File size: 14,819 Bytes
56bb634
b2cfca7
 
 
56bb634
b2cfca7
 
 
 
 
56bb634
b2cfca7
 
 
 
 
56bb634
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
b2cfca7
 
 
56bb634
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a896d
b2cfca7
 
 
 
c4a896d
b2cfca7
 
 
 
c4a896d
b2cfca7
 
 
 
56bb634
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a896d
b2cfca7
 
 
c4a896d
b2cfca7
 
 
 
 
 
 
 
 
c4a896d
b2cfca7
 
 
 
56bb634
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
b2cfca7
 
 
 
c4a896d
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c4a896d
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
 
b2cfca7
 
 
 
 
 
 
 
 
 
56bb634
 
b2cfca7
 
 
56bb634
 
b2cfca7
 
56bb634
b2cfca7
 
 
 
 
56bb634
b2cfca7
 
 
 
56bb634
 
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
 
b2cfca7
 
 
 
 
 
 
 
 
56bb634
 
b2cfca7
 
56bb634
b2cfca7
 
 
 
56bb634
 
 
b2cfca7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
56bb634
 
b2cfca7
 
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
import gradio as gr
import cv2
import numpy as np
from PIL import Image
import torch
from transformers import DetrImageProcessor, DetrForObjectDetection
from collections import defaultdict
import time
import psutil
import os

# Load DETR model (optimized for CPU)
print("Loading DETR model...")
processor = DetrImageProcessor.from_pretrained("facebook/detr-resnet-50")
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-50")
model.eval()

# COCO class labels
COCO_CLASSES = [
    'N/A', 'person', 'bicycle', 'car', 'motorcycle', 'airplane', 'bus',
    'train', 'truck', 'boat', 'traffic light', 'fire hydrant', 'N/A',
    'stop sign', 'parking meter', 'bench', 'bird', 'cat', 'dog', 'horse',
    'sheep', 'cow', 'elephant', 'bear', 'zebra', 'giraffe', 'N/A', 'backpack',
    'umbrella', 'N/A', 'N/A', 'handbag', 'tie', 'suitcase', 'frisbee', 'skis',
    'snowboard', 'sports ball', 'kite', 'baseball bat', 'baseball glove',
    'skateboard', 'surfboard', 'tennis racket', 'bottle', 'N/A', 'wine glass',
    'cup', 'fork', 'knife', 'spoon', 'bowl', 'banana', 'apple', 'sandwich',
    'orange', 'broccoli', 'carrot', 'hot dog', 'pizza', 'donut', 'cake',
    'chair', 'couch', 'potted plant', 'bed', 'N/A', 'dining table', 'N/A',
    'N/A', 'toilet', 'N/A', 'tv', 'laptop', 'mouse', 'remote', 'keyboard',
    'cell phone', 'microwave', 'oven', 'toaster', 'sink', 'refrigerator', 'N/A',
    'book', 'clock', 'vase', 'scissors', 'teddy bear', 'hair drier', 'toothbrush'
]

def get_available_memory():
    """Get available system memory in GB"""
    return psutil.virtual_memory().available / (1024 ** 3)

def auto_adjust_confidence(image_size, num_objects_hint=None):
    """Dynamically adjust confidence based on image complexity"""
    pixels = image_size[0] * image_size[1]
    
    # Base confidence on image size
    if pixels < 500000:  # Small image
        base_confidence = 0.6
    elif pixels < 2000000:  # Medium image
        base_confidence = 0.65
    else:  # Large image
        base_confidence = 0.7
    
    return base_confidence

def auto_calculate_frame_interval(total_frames, video_duration, available_memory_gb):
    """Dynamically calculate optimal frame interval based on video properties and system resources"""
    
    # Base calculations
    fps = total_frames / video_duration if video_duration > 0 else 30
    
    # Memory-based adjustment
    if available_memory_gb < 2:
        memory_factor = 3
    elif available_memory_gb < 4:
        memory_factor = 2
    else:
        memory_factor = 1
    
    # Duration-based adjustment
    if video_duration < 10:
        duration_factor = 1
    elif video_duration < 30:
        duration_factor = 2
    elif video_duration < 60:
        duration_factor = 3
    else:
        duration_factor = 4
    
    # Calculate optimal frames to process
    target_frames = min(150, max(30, total_frames // (memory_factor * duration_factor)))
    
    # Calculate interval
    interval = max(1, total_frames // target_frames)
    
    return interval, target_frames

def detect_objects(image, confidence_threshold=None):
    """Detect objects in a single image with dynamic confidence"""
    # Convert to RGB if needed
    if isinstance(image, np.ndarray):
        image = Image.fromarray(image)
    
    # Auto-adjust confidence if not provided
    if confidence_threshold is None:
        confidence_threshold = auto_adjust_confidence(image.size)
    
    # Prepare image
    inputs = processor(images=image, return_tensors="pt")
    
    # Inference
    with torch.no_grad():
        outputs = model(**inputs)
    
    # Post-process
    target_sizes = torch.tensor([image.size[::-1]])
    results = processor.post_process_object_detection(
        outputs, target_sizes=target_sizes, threshold=confidence_threshold
    )[0]
    
    return results, image, confidence_threshold

def draw_boxes(image, results):
    """Draw bounding boxes on image"""
    img_array = np.array(image)
    
    detections = []
    object_counts = defaultdict(int)
    
    for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
        box = [round(i, 2) for i in box.tolist()]
        label_name = COCO_CLASSES[label.item()]
        
        if label_name != 'N/A':
            # Draw rectangle
            x1, y1, x2, y2 = map(int, box)
            cv2.rectangle(img_array, (x1, y1), (x2, y2), (0, 255, 0), 2)
            
            # Add label
            label_text = f"{label_name}: {score:.2f}"
            cv2.putText(img_array, label_text, (x1, y1-10),
                       cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
            
            detections.append(f"{label_name} ({score:.2%})")
            object_counts[label_name] += 1
    
    return img_array, detections, object_counts

def process_static_image(image):
    """Process static image mode with auto-detection"""
    if image is None:
        return None, "Please upload an image"
    
    start_time = time.time()
    
    # Detect objects with auto-adjusted confidence
    results, pil_image, used_confidence = detect_objects(image, confidence_threshold=None)
    
    # Draw boxes
    annotated_image, detections, object_counts = draw_boxes(pil_image, results)
    
    processing_time = time.time() - start_time
    
    # Create detailed summary
    if detections:
        summary = f"### 🎯 Detection Results\n\n"
        summary += f"**Found {len(detections)} objects in {processing_time:.2f} seconds**\n\n"
        summary += f"*Auto-adjusted confidence threshold: {used_confidence:.2f}*\n\n"
        summary += "#### Detected Objects:\n"
        
        # Group by object type
        for obj_name, count in sorted(object_counts.items(), key=lambda x: x[1], reverse=True):
            summary += f"- **{obj_name}**: {count} instance(s)\n"
        
        summary += f"\n#### All Detections:\n"
        for i, d in enumerate(detections, 1):
            summary += f"{i}. {d}\n"
    else:
        summary = f"### ⚠️ No objects detected\n\n"
        summary += f"*Confidence threshold used: {used_confidence:.2f}*\n\n"
        summary += "Try uploading a different image with more visible objects."
    
    return annotated_image, summary

def process_video(video_path, progress=gr.Progress()):
    """Process video mode with full auto-adjustment"""
    if video_path is None:
        return None, "Please upload a video"
    
    progress(0, desc="Analyzing video...")
    
    cap = cv2.VideoCapture(video_path)
    
    # Get video properties
    fps = int(cap.get(cv2.CAP_PROP_FPS))
    total_frames = int(cap.get(cv2.CAP_PROP_FRAME_COUNT))
    width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
    height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
    duration = total_frames / fps if fps > 0 else 0
    
    # Get available memory
    available_memory = get_available_memory()
    
    # Auto-calculate optimal frame interval
    frame_interval, estimated_frames = auto_calculate_frame_interval(
        total_frames, duration, available_memory
    )
    
    progress(0.1, desc=f"Processing video (sampling every {frame_interval} frames)...")
    
    # Auto-adjust confidence based on video properties
    frame_size = width * height
    confidence_threshold = auto_adjust_confidence((width, height))
    
    # Output video writer
    output_path = "output_video.mp4"
    fourcc = cv2.VideoWriter_fourcc(*'mp4v')
    out = cv2.VideoWriter(output_path, fourcc, fps, (width, height))
    
    frame_count = 0
    processed_count = 0
    object_tracker = defaultdict(int)
    start_time = time.time()
    
    while cap.isOpened():
        ret, frame = cap.read()
        if not ret:
            break
        
        # Update progress
        if frame_count % 30 == 0:
            progress_pct = (frame_count / total_frames) * 0.8 + 0.1
            progress(progress_pct, desc=f"Processing frame {frame_count}/{total_frames}")
        
        # Process every nth frame
        if frame_count % frame_interval == 0:
            # Convert BGR to RGB
            rgb_frame = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
            
            # Detect objects
            results, _, _ = detect_objects(rgb_frame, confidence_threshold)
            
            # Draw boxes
            for score, label, box in zip(results["scores"], results["labels"], results["boxes"]):
                box = [round(i, 2) for i in box.tolist()]
                label_name = COCO_CLASSES[label.item()]
                
                if label_name != 'N/A':
                    x1, y1, x2, y2 = map(int, box)
                    cv2.rectangle(frame, (x1, y1), (x2, y2), (0, 255, 0), 2)
                    
                    label_text = f"{label_name}: {score:.2f}"
                    cv2.putText(frame, label_text, (x1, y1-10),
                               cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
                    
                    object_tracker[label_name] += 1
            
            processed_count += 1
        
        out.write(frame)
        frame_count += 1
    
    cap.release()
    out.release()
    
    processing_time = time.time() - start_time
    
    progress(1.0, desc="Complete!")
    
    # Create detailed summary
    summary = f"### 🎬 Video Processing Complete\n\n"
    summary += f"**Processing Time**: {processing_time:.2f} seconds\n\n"
    summary += "#### Video Information:\n"
    summary += f"- Duration: {duration:.2f} seconds\n"
    summary += f"- Total frames: {total_frames}\n"
    summary += f"- FPS: {fps}\n"
    summary += f"- Resolution: {width}x{height}\n\n"
    
    summary += "#### Auto-Optimization Settings:\n"
    summary += f"- Confidence threshold: {confidence_threshold:.2f} *(auto-adjusted)*\n"
    summary += f"- Frame interval: Every {frame_interval} frame(s) *(auto-calculated)*\n"
    summary += f"- Frames processed: {processed_count}/{total_frames}\n"
    summary += f"- Available memory: {available_memory:.2f} GB\n\n"
    
    if object_tracker:
        summary += "### πŸ“Š Detected Objects Across Video:\n\n"
        for obj, count in sorted(object_tracker.items(), key=lambda x: x[1], reverse=True):
            summary += f"- **{obj}**: {count} detection(s)\n"
    else:
        summary += "⚠️ No objects detected in the video.\n"
        summary += "This might be due to low lighting, fast motion, or absence of recognizable objects."
    
    return output_path, summary

# Create Gradio interface
with gr.Blocks(title="AI Object Recognition System", theme=gr.themes.Soft()) as demo:
    gr.Markdown("""
    # πŸ€– AI Object Recognition System
    ### Intelligent Auto-Adjusting Detection & Tracking
    
    This system **automatically optimizes** detection parameters based on:
    - Image/video size and complexity
    - Available system resources
    - Video duration and frame rate
    
    **No manual tuning required!**
    """)
    
    with gr.Tabs():
        # Static Image Tab
        with gr.Tab("πŸ“Έ Static Mode - Image Detection"):
            gr.Markdown("""
            ### Automatic Image Analysis
            Upload any image and the system will:
            - Auto-adjust confidence thresholds
            - Detect all visible objects
            - Provide detailed statistics
            """)
            
            with gr.Row():
                with gr.Column():
                    static_input = gr.Image(type="numpy", label="Upload Image")
                    static_btn = gr.Button("πŸ” Auto-Detect Objects", variant="primary", size="lg")
                    gr.Markdown("*The system will automatically optimize detection settings*")
                
                with gr.Column():
                    static_output = gr.Image(label="Detected Objects")
                    static_summary = gr.Markdown(label="Detection Results")
            
            static_btn.click(
                fn=process_static_image,
                inputs=[static_input],
                outputs=[static_output, static_summary]
            )
            
            gr.Examples(
                examples=[],
                inputs=static_input,
                label="Try these examples (upload your own images)"
            )
        
        # Dynamic Video Tab
        with gr.Tab("πŸŽ₯ Dynamic Mode - Video Detection"):
            gr.Markdown("""
            ### Automatic Video Analysis
            Upload a video and the system will:
            - Auto-calculate optimal frame sampling
            - Adjust confidence based on video quality
            - Optimize for available CPU resources
            - Track objects across frames
            
            **Supports videos of any length!** The system automatically scales processing.
            """)
            
            with gr.Row():
                with gr.Column():
                    video_input = gr.Video(label="Upload Video")
                    video_btn = gr.Button("🎬 Auto-Process Video", variant="primary", size="lg")
                    gr.Markdown("""
                    *The system will automatically:*
                    - Analyze video properties
                    - Calculate optimal frame sampling
                    - Adjust detection thresholds
                    - Monitor system resources
                    """)
                
                with gr.Column():
                    video_output = gr.Video(label="Processed Video with Detections")
                    video_summary = gr.Markdown(label="Processing Results")
            
            video_btn.click(
                fn=process_video,
                inputs=[video_input],
                outputs=[video_output, video_summary]
            )
    
    gr.Markdown("""
    ---
    ## 🧠 How Auto-Adjustment Works
    
    ### Image Mode:
    - **Small images** (< 500K pixels): Lower confidence threshold for more detections
    - **Large images** (> 2M pixels): Higher threshold to reduce false positives
    
    ### Video Mode:
    - **Short videos** (< 10s): Process more frames for detail
    - **Long videos** (> 60s): Smart sampling to maintain performance
    - **Memory-aware**: Adjusts based on available RAM
    - **Quality-adaptive**: Balances speed vs accuracy automatically
    
    ### πŸ“Š Technical Details:
    - **Model**: DETR ResNet-50 (Detection Transformer)
    - **Dataset**: COCO (80+ object categories)
    - **Optimization**: CPU-friendly with intelligent resource management
    - **Supported Objects**: People, vehicles, animals, furniture, electronics, food, and more
    
    ### πŸ’‘ Tips:
    - The system works best with clear, well-lit images/videos
    - All adjustments happen automatically - just upload and click!
    - Processing time varies based on video length and system resources
    """)

if __name__ == "__main__":
    demo.launch()