Upload 6 files
Browse files- Dockerfile +31 -0
- README.md +142 -10
- app.py +398 -0
- best.onnx +3 -0
- data.yaml +25 -0
- requirements.txt +8 -0
Dockerfile
ADDED
|
@@ -0,0 +1,31 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
# Dockerfile for reCAPTCHA 3x3 Detection Space
|
| 2 |
+
FROM python:3.9-slim
|
| 3 |
+
|
| 4 |
+
# Set working directory
|
| 5 |
+
WORKDIR /app
|
| 6 |
+
|
| 7 |
+
# Install system dependencies
|
| 8 |
+
RUN apt-get update && apt-get install -y \
|
| 9 |
+
libgl1 \
|
| 10 |
+
libglib2.0-0 \
|
| 11 |
+
&& rm -rf /var/lib/apt/lists/*
|
| 12 |
+
|
| 13 |
+
# Copy requirements first (for caching)
|
| 14 |
+
COPY requirements.txt .
|
| 15 |
+
RUN pip install --no-cache-dir -r requirements.txt
|
| 16 |
+
|
| 17 |
+
# Copy application code and models
|
| 18 |
+
COPY app.py .
|
| 19 |
+
COPY best.onnx .
|
| 20 |
+
COPY data.yaml .
|
| 21 |
+
|
| 22 |
+
# Expose port
|
| 23 |
+
EXPOSE 7860
|
| 24 |
+
|
| 25 |
+
# Health check
|
| 26 |
+
HEALTHCHECK --interval=30s --timeout=10s --start-period=5s --retries=3 \
|
| 27 |
+
CMD python -c "import requests; requests.get('http://localhost:7860/health')"
|
| 28 |
+
|
| 29 |
+
# Run the application
|
| 30 |
+
CMD ["python", "app.py"]
|
| 31 |
+
|
README.md
CHANGED
|
@@ -1,10 +1,142 @@
|
|
| 1 |
-
---
|
| 2 |
-
title:
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
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| 10 |
+
|
| 11 |
+
# 🤖 reCAPTCHA 3x3 Detection API
|
| 12 |
+
|
| 13 |
+
Solves 3x3 grid reCAPTCHA challenges using YOLO object detection.
|
| 14 |
+
|
| 15 |
+
## 🎯 Features
|
| 16 |
+
|
| 17 |
+
- ⚡ Fast inference (2-3s per request)
|
| 18 |
+
- 🎯 High accuracy with YOLO detection
|
| 19 |
+
- 🔄 Auto-lowering confidence for better detection
|
| 20 |
+
- 📊 Built-in metrics and monitoring
|
| 21 |
+
- 🌐 CORS enabled for browser extensions
|
| 22 |
+
|
| 23 |
+
## 📡 API Endpoints
|
| 24 |
+
|
| 25 |
+
### POST /predict
|
| 26 |
+
|
| 27 |
+
Predict which tiles to click in a 3x3 grid.
|
| 28 |
+
|
| 29 |
+
**Request:**
|
| 30 |
+
```json
|
| 31 |
+
{
|
| 32 |
+
"image": "data:image/png;base64,iVBOR...",
|
| 33 |
+
"challenge_title": "crosswalks"
|
| 34 |
+
}
|
| 35 |
+
```
|
| 36 |
+
|
| 37 |
+
**Response:**
|
| 38 |
+
```json
|
| 39 |
+
{
|
| 40 |
+
"success": true,
|
| 41 |
+
"tiles_to_click": [2, 5, 8],
|
| 42 |
+
"num_detections": 12,
|
| 43 |
+
"confidence_used": 0.20,
|
| 44 |
+
"latency_s": 2.341
|
| 45 |
+
}
|
| 46 |
+
```
|
| 47 |
+
|
| 48 |
+
### GET /health
|
| 49 |
+
|
| 50 |
+
Check API health and statistics.
|
| 51 |
+
|
| 52 |
+
**Response:**
|
| 53 |
+
```json
|
| 54 |
+
{
|
| 55 |
+
"status": "healthy",
|
| 56 |
+
"model_loaded": true,
|
| 57 |
+
"requests_total": 42,
|
| 58 |
+
"requests_successful": 40,
|
| 59 |
+
"avg_latency_s": 2.5
|
| 60 |
+
}
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
## 🚀 Usage
|
| 64 |
+
|
| 65 |
+
### From Browser Extension
|
| 66 |
+
|
| 67 |
+
```javascript
|
| 68 |
+
const response = await fetch('https://YOUR-SPACE.hf.space/predict', {
|
| 69 |
+
method: 'POST',
|
| 70 |
+
headers: {'Content-Type': 'application/json'},
|
| 71 |
+
body: JSON.stringify({
|
| 72 |
+
image: screenshotBase64,
|
| 73 |
+
challenge_title: 'crosswalks'
|
| 74 |
+
})
|
| 75 |
+
});
|
| 76 |
+
|
| 77 |
+
const result = await response.json();
|
| 78 |
+
console.log('Tiles to click:', result.tiles_to_click);
|
| 79 |
+
```
|
| 80 |
+
|
| 81 |
+
### From Python
|
| 82 |
+
|
| 83 |
+
```python
|
| 84 |
+
import requests
|
| 85 |
+
import base64
|
| 86 |
+
|
| 87 |
+
with open('screenshot.png', 'rb') as f:
|
| 88 |
+
image_b64 = base64.b64encode(f.read()).decode()
|
| 89 |
+
|
| 90 |
+
response = requests.post('https://YOUR-SPACE.hf.space/predict', json={
|
| 91 |
+
'image': f'data:image/png;base64,{image_b64}',
|
| 92 |
+
'challenge_title': 'crosswalks'
|
| 93 |
+
})
|
| 94 |
+
|
| 95 |
+
print(response.json())
|
| 96 |
+
```
|
| 97 |
+
|
| 98 |
+
## 🔧 Model Details
|
| 99 |
+
|
| 100 |
+
- **Architecture:** YOLOv8 Detection
|
| 101 |
+
- **Input:** 640x640 RGB image
|
| 102 |
+
- **Output:** Bounding boxes with confidence scores
|
| 103 |
+
- **Classes:** Multiple object types (vehicles, crosswalks, traffic lights, etc.)
|
| 104 |
+
|
| 105 |
+
## 📊 Performance
|
| 106 |
+
|
| 107 |
+
- **Cold start:** ~15 seconds (first request)
|
| 108 |
+
- **Warm inference:** 2-3 seconds per request
|
| 109 |
+
- **Memory usage:** ~1.5GB
|
| 110 |
+
- **Concurrent requests:** 2-3 simultaneous
|
| 111 |
+
|
| 112 |
+
## 🎯 Tile Mapping
|
| 113 |
+
|
| 114 |
+
The API maps detected objects to a 3x3 grid:
|
| 115 |
+
|
| 116 |
+
```
|
| 117 |
+
[0] [1] [2]
|
| 118 |
+
[3] [4] [5]
|
| 119 |
+
[6] [7] [8]
|
| 120 |
+
```
|
| 121 |
+
|
| 122 |
+
Objects are mapped based on their center point within the grid.
|
| 123 |
+
|
| 124 |
+
## 🛡️ Rate Limits
|
| 125 |
+
|
| 126 |
+
- **Free tier:** 100 requests per hour
|
| 127 |
+
- **Timeout:** 30 seconds per request
|
| 128 |
+
- **Max image size:** 10MB
|
| 129 |
+
|
| 130 |
+
## 📝 License
|
| 131 |
+
|
| 132 |
+
MIT License - See LICENSE file for details
|
| 133 |
+
|
| 134 |
+
## 🔗 Related
|
| 135 |
+
|
| 136 |
+
- [4x4 Segmentation Space](https://huggingface.co/spaces/YOUR-USERNAME/recaptcha-solver-4x4)
|
| 137 |
+
- [Browser Extension](https://github.com/YOUR-REPO)
|
| 138 |
+
|
| 139 |
+
---
|
| 140 |
+
|
| 141 |
+
**Note:** This API is for educational and research purposes only. Use responsibly and respect website terms of service.
|
| 142 |
+
|
app.py
ADDED
|
@@ -0,0 +1,398 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
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|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
reCAPTCHA 3x3 Detection API - Hugging Face Space
|
| 3 |
+
Lightweight API for 3x3 grid challenge solving using YOLO detection
|
| 4 |
+
"""
|
| 5 |
+
|
| 6 |
+
from flask import Flask, request, jsonify
|
| 7 |
+
from flask_cors import CORS
|
| 8 |
+
import cv2
|
| 9 |
+
import numpy as np
|
| 10 |
+
import onnxruntime as ort
|
| 11 |
+
import yaml
|
| 12 |
+
import base64
|
| 13 |
+
import io
|
| 14 |
+
from PIL import Image
|
| 15 |
+
import time
|
| 16 |
+
import os
|
| 17 |
+
from functools import lru_cache
|
| 18 |
+
|
| 19 |
+
app = Flask(__name__)
|
| 20 |
+
CORS(app) # Enable CORS for browser extension
|
| 21 |
+
|
| 22 |
+
# Global variables
|
| 23 |
+
model_session = None
|
| 24 |
+
class_names = None
|
| 25 |
+
model_load_time = 0
|
| 26 |
+
request_count = 0
|
| 27 |
+
successful_count = 0
|
| 28 |
+
failed_count = 0
|
| 29 |
+
total_latency = 0.0
|
| 30 |
+
|
| 31 |
+
# Configuration
|
| 32 |
+
MODEL_FOLDER = "." # Models in root folder (no subfolder)
|
| 33 |
+
CONFIDENCE_THRESHOLD = 0.20
|
| 34 |
+
INPUT_SIZE = 640
|
| 35 |
+
|
| 36 |
+
print("="*60)
|
| 37 |
+
print("🚀 reCAPTCHA 3x3 Detection API")
|
| 38 |
+
print("="*60)
|
| 39 |
+
|
| 40 |
+
|
| 41 |
+
@lru_cache(maxsize=1)
|
| 42 |
+
def load_model():
|
| 43 |
+
"""Load ONNX model and class names (cached)"""
|
| 44 |
+
global model_session, class_names, model_load_time
|
| 45 |
+
|
| 46 |
+
start_time = time.time()
|
| 47 |
+
print(f"📦 Loading model from {MODEL_FOLDER}/...")
|
| 48 |
+
|
| 49 |
+
try:
|
| 50 |
+
# Load ONNX model
|
| 51 |
+
model_path = os.path.join(MODEL_FOLDER, "best.onnx")
|
| 52 |
+
model_session = ort.InferenceSession(
|
| 53 |
+
model_path,
|
| 54 |
+
providers=['CPUExecutionProvider']
|
| 55 |
+
)
|
| 56 |
+
print(f" ✓ Model loaded: {model_path}")
|
| 57 |
+
|
| 58 |
+
# Load class names
|
| 59 |
+
data_yaml_path = os.path.join(MODEL_FOLDER, "data.yaml")
|
| 60 |
+
with open(data_yaml_path, 'r') as f:
|
| 61 |
+
data = yaml.safe_load(f)
|
| 62 |
+
class_names = data['names']
|
| 63 |
+
print(f" ✓ Classes loaded: {len(class_names)} classes")
|
| 64 |
+
|
| 65 |
+
model_load_time = time.time() - start_time
|
| 66 |
+
print(f" ⏱️ Load time: {model_load_time:.2f}s")
|
| 67 |
+
|
| 68 |
+
return True
|
| 69 |
+
except Exception as e:
|
| 70 |
+
print(f" ✗ Error loading model: {e}")
|
| 71 |
+
return False
|
| 72 |
+
|
| 73 |
+
|
| 74 |
+
def base64_to_image(base64_string):
|
| 75 |
+
"""Convert base64 string to OpenCV image"""
|
| 76 |
+
try:
|
| 77 |
+
# Remove data URL prefix if present
|
| 78 |
+
if ',' in base64_string:
|
| 79 |
+
base64_string = base64_string.split(',')[1]
|
| 80 |
+
|
| 81 |
+
# Decode base64
|
| 82 |
+
image_data = base64.b64decode(base64_string)
|
| 83 |
+
|
| 84 |
+
# Convert to PIL Image
|
| 85 |
+
pil_image = Image.open(io.BytesIO(image_data))
|
| 86 |
+
|
| 87 |
+
# Convert to OpenCV format
|
| 88 |
+
opencv_image = cv2.cvtColor(np.array(pil_image), cv2.COLOR_RGB2BGR)
|
| 89 |
+
|
| 90 |
+
return opencv_image
|
| 91 |
+
except Exception as e:
|
| 92 |
+
print(f"Error converting base64 to image: {e}")
|
| 93 |
+
return None
|
| 94 |
+
|
| 95 |
+
|
| 96 |
+
def preprocess_image(img):
|
| 97 |
+
"""Preprocess image for YOLO model"""
|
| 98 |
+
orig_h, orig_w = img.shape[:2]
|
| 99 |
+
|
| 100 |
+
# Resize to 640x640
|
| 101 |
+
img_resized = cv2.resize(img, (INPUT_SIZE, INPUT_SIZE))
|
| 102 |
+
|
| 103 |
+
# Convert BGR to RGB
|
| 104 |
+
img_rgb = cv2.cvtColor(img_resized, cv2.COLOR_BGR2RGB)
|
| 105 |
+
|
| 106 |
+
# Normalize and transpose
|
| 107 |
+
img_normalized = img_rgb.astype(np.float32) / 255.0
|
| 108 |
+
img_transposed = np.transpose(img_normalized, (2, 0, 1))
|
| 109 |
+
|
| 110 |
+
# Add batch dimension
|
| 111 |
+
img_batch = np.expand_dims(img_transposed, axis=0)
|
| 112 |
+
|
| 113 |
+
return img_batch, orig_w, orig_h
|
| 114 |
+
|
| 115 |
+
|
| 116 |
+
def run_inference(img_batch):
|
| 117 |
+
"""Run ONNX inference"""
|
| 118 |
+
global model_session
|
| 119 |
+
|
| 120 |
+
input_name = model_session.get_inputs()[0].name
|
| 121 |
+
outputs = model_session.run(None, {input_name: img_batch})
|
| 122 |
+
|
| 123 |
+
return outputs
|
| 124 |
+
|
| 125 |
+
|
| 126 |
+
def parse_detections(outputs, orig_w, orig_h, conf_threshold=0.20):
|
| 127 |
+
"""Parse YOLO detection output"""
|
| 128 |
+
if not outputs or len(outputs) == 0:
|
| 129 |
+
return []
|
| 130 |
+
|
| 131 |
+
output = outputs[0]
|
| 132 |
+
|
| 133 |
+
# YOLOv8 detection format: [batch, num_classes + 4, num_anchors]
|
| 134 |
+
# Expected: [1, 18, 8400] → Need [8400, 18]
|
| 135 |
+
output = output[0] # Remove batch: [18, 8400] or [8400, 18]
|
| 136 |
+
|
| 137 |
+
# Ensure correct shape: [num_predictions, num_classes + 4]
|
| 138 |
+
if output.shape[0] < output.shape[1]: # If [18, 8400]
|
| 139 |
+
output = output.T # Transpose to [8400, 18]
|
| 140 |
+
|
| 141 |
+
num_classes = output.shape[1] - 4
|
| 142 |
+
|
| 143 |
+
results = []
|
| 144 |
+
for detection in output:
|
| 145 |
+
# Extract box coordinates
|
| 146 |
+
x_center, y_center, width, height = detection[:4]
|
| 147 |
+
|
| 148 |
+
# Get class scores
|
| 149 |
+
class_scores = detection[4:]
|
| 150 |
+
class_id = np.argmax(class_scores)
|
| 151 |
+
confidence = class_scores[class_id]
|
| 152 |
+
|
| 153 |
+
if confidence < conf_threshold:
|
| 154 |
+
continue
|
| 155 |
+
|
| 156 |
+
# Scale to original image
|
| 157 |
+
x_center = x_center * orig_w / INPUT_SIZE
|
| 158 |
+
y_center = y_center * orig_h / INPUT_SIZE
|
| 159 |
+
width = width * orig_w / INPUT_SIZE
|
| 160 |
+
height = height * orig_h / INPUT_SIZE
|
| 161 |
+
|
| 162 |
+
# Convert to x1, y1, x2, y2
|
| 163 |
+
x1 = int(x_center - width / 2)
|
| 164 |
+
y1 = int(y_center - height / 2)
|
| 165 |
+
x2 = int(x_center + width / 2)
|
| 166 |
+
y2 = int(y_center + height / 2)
|
| 167 |
+
|
| 168 |
+
# VALIDATION: Skip if class_id is out of range
|
| 169 |
+
if class_names and class_id >= len(class_names):
|
| 170 |
+
print(f" ⚠ Skipping detection with invalid class_id={class_id} (max={len(class_names)-1})")
|
| 171 |
+
continue
|
| 172 |
+
|
| 173 |
+
results.append({
|
| 174 |
+
'box': [x1, y1, x2, y2],
|
| 175 |
+
'center': [x_center, y_center],
|
| 176 |
+
'confidence': float(confidence),
|
| 177 |
+
'class_id': int(class_id),
|
| 178 |
+
'class_name': class_names[class_id] if class_names else str(class_id)
|
| 179 |
+
})
|
| 180 |
+
|
| 181 |
+
# Apply NMS
|
| 182 |
+
if len(results) > 0:
|
| 183 |
+
boxes = [r['box'] for r in results]
|
| 184 |
+
scores = [r['confidence'] for r in results]
|
| 185 |
+
|
| 186 |
+
indices = cv2.dnn.NMSBoxes(boxes, scores, conf_threshold, 0.45)
|
| 187 |
+
|
| 188 |
+
if len(indices) > 0:
|
| 189 |
+
results = [results[i] for i in indices.flatten()]
|
| 190 |
+
|
| 191 |
+
return results
|
| 192 |
+
|
| 193 |
+
|
| 194 |
+
def normalize_text(text):
|
| 195 |
+
"""Normalize challenge text"""
|
| 196 |
+
text = text.lower().strip()
|
| 197 |
+
|
| 198 |
+
# Remove articles "a " and "the " for better matching
|
| 199 |
+
text = text.replace('a ', '').replace('the ', '')
|
| 200 |
+
|
| 201 |
+
# Singular/plural mapping
|
| 202 |
+
mappings = {
|
| 203 |
+
'bicycle': 'bicycles',
|
| 204 |
+
'bus': 'buses',
|
| 205 |
+
'car': 'cars',
|
| 206 |
+
'fire hydrant': 'fire hydrant', # Keep singular! class name is "a fire hydrant"
|
| 207 |
+
'motorcycle': 'motorcycles',
|
| 208 |
+
'traffic light': 'traffic lights',
|
| 209 |
+
'crosswalk': 'crosswalks',
|
| 210 |
+
'vehicle': 'vehicles',
|
| 211 |
+
'bridge': 'bridges',
|
| 212 |
+
'boat': 'boats',
|
| 213 |
+
'taxi': 'taxis',
|
| 214 |
+
'stair': 'stairs',
|
| 215 |
+
'chimney': 'chimneys',
|
| 216 |
+
'parking meter': 'parking meters'
|
| 217 |
+
}
|
| 218 |
+
|
| 219 |
+
for singular, plural in mappings.items():
|
| 220 |
+
if singular in text:
|
| 221 |
+
return plural
|
| 222 |
+
|
| 223 |
+
return text
|
| 224 |
+
|
| 225 |
+
|
| 226 |
+
def get_tiles_to_click(detections, challenge_title, img_width, img_height, max_tiles=3):
|
| 227 |
+
"""Map detections to 3x3 tiles"""
|
| 228 |
+
if not detections or not challenge_title:
|
| 229 |
+
return []
|
| 230 |
+
|
| 231 |
+
# Normalize challenge title
|
| 232 |
+
normalized_title = normalize_text(challenge_title)
|
| 233 |
+
|
| 234 |
+
# Calculate tile dimensions
|
| 235 |
+
tile_width = img_width / 3
|
| 236 |
+
tile_height = img_height / 3
|
| 237 |
+
|
| 238 |
+
# Map detections to tiles
|
| 239 |
+
tile_scores = {}
|
| 240 |
+
|
| 241 |
+
for det in detections:
|
| 242 |
+
det_class = det['class_name'].lower()
|
| 243 |
+
# Also remove articles from detection class for consistent matching
|
| 244 |
+
det_class = det_class.replace('a ', '').replace('the ', '')
|
| 245 |
+
|
| 246 |
+
# Check if detection matches challenge
|
| 247 |
+
if normalized_title not in det_class and det_class not in normalized_title:
|
| 248 |
+
continue
|
| 249 |
+
|
| 250 |
+
# Get center point
|
| 251 |
+
center_x, center_y = det['center']
|
| 252 |
+
|
| 253 |
+
# Determine which tile
|
| 254 |
+
col = int(center_x // tile_width)
|
| 255 |
+
row = int(center_y // tile_height)
|
| 256 |
+
|
| 257 |
+
# Clamp to valid range
|
| 258 |
+
col = max(0, min(2, col))
|
| 259 |
+
row = max(0, min(2, row))
|
| 260 |
+
|
| 261 |
+
# Calculate tile ID (0-8, left to right, top to bottom)
|
| 262 |
+
tile_id = row * 3 + col
|
| 263 |
+
|
| 264 |
+
# Store best score for this tile
|
| 265 |
+
if tile_id not in tile_scores or det['confidence'] > tile_scores[tile_id]:
|
| 266 |
+
tile_scores[tile_id] = det['confidence']
|
| 267 |
+
|
| 268 |
+
# Sort by confidence and take top N
|
| 269 |
+
sorted_tiles = sorted(tile_scores.items(), key=lambda x: x[1], reverse=True)
|
| 270 |
+
tiles_to_click = [tile_id for tile_id, _ in sorted_tiles[:max_tiles]]
|
| 271 |
+
|
| 272 |
+
return sorted(tiles_to_click)
|
| 273 |
+
|
| 274 |
+
|
| 275 |
+
def auto_lower_confidence(img_batch, orig_w, orig_h, challenge_title, img_width, img_height):
|
| 276 |
+
"""Auto-lower confidence if < 3 tiles found"""
|
| 277 |
+
conf_thresholds = [0.20, 0.15, 0.10, 0.05]
|
| 278 |
+
|
| 279 |
+
for conf in conf_thresholds:
|
| 280 |
+
outputs = run_inference(img_batch)
|
| 281 |
+
detections = parse_detections(outputs, orig_w, orig_h, conf_threshold=conf)
|
| 282 |
+
tiles = get_tiles_to_click(detections, challenge_title, img_width, img_height, max_tiles=3)
|
| 283 |
+
|
| 284 |
+
if len(tiles) >= 3:
|
| 285 |
+
print(f" ✓ Got {len(tiles)} tiles at conf={conf}")
|
| 286 |
+
return tiles, len(detections), conf
|
| 287 |
+
|
| 288 |
+
# Return what we have
|
| 289 |
+
print(f" ⚠ Could only find {len(tiles)} tiles")
|
| 290 |
+
return tiles, len(detections), conf_thresholds[-1]
|
| 291 |
+
|
| 292 |
+
|
| 293 |
+
@app.route('/health', methods=['GET'])
|
| 294 |
+
def health():
|
| 295 |
+
"""Health check endpoint"""
|
| 296 |
+
return jsonify({
|
| 297 |
+
'status': 'healthy',
|
| 298 |
+
'model_loaded': model_session is not None,
|
| 299 |
+
'model_load_time_s': model_load_time,
|
| 300 |
+
'requests_total': request_count,
|
| 301 |
+
'requests_successful': successful_count,
|
| 302 |
+
'requests_failed': failed_count,
|
| 303 |
+
'avg_latency_s': total_latency / max(request_count, 1)
|
| 304 |
+
})
|
| 305 |
+
|
| 306 |
+
|
| 307 |
+
@app.route('/predict', methods=['POST'])
|
| 308 |
+
def predict():
|
| 309 |
+
"""Main prediction endpoint"""
|
| 310 |
+
global request_count, successful_count, failed_count, total_latency
|
| 311 |
+
|
| 312 |
+
start_time = time.time()
|
| 313 |
+
request_count += 1
|
| 314 |
+
|
| 315 |
+
try:
|
| 316 |
+
# Parse request
|
| 317 |
+
data = request.json
|
| 318 |
+
|
| 319 |
+
if not data or 'image' not in data:
|
| 320 |
+
failed_count += 1
|
| 321 |
+
return jsonify({'error': 'Missing image data'}), 400
|
| 322 |
+
|
| 323 |
+
challenge_title = data.get('challenge_title', '')
|
| 324 |
+
|
| 325 |
+
# Convert base64 to image
|
| 326 |
+
img = base64_to_image(data['image'])
|
| 327 |
+
if img is None:
|
| 328 |
+
failed_count += 1
|
| 329 |
+
return jsonify({'error': 'Invalid image data'}), 400
|
| 330 |
+
|
| 331 |
+
img_height, img_width = img.shape[:2]
|
| 332 |
+
|
| 333 |
+
# Preprocess
|
| 334 |
+
img_batch, orig_w, orig_h = preprocess_image(img)
|
| 335 |
+
|
| 336 |
+
# Predict with auto-lower confidence
|
| 337 |
+
tiles, num_detections, used_conf = auto_lower_confidence(
|
| 338 |
+
img_batch, orig_w, orig_h, challenge_title, img_width, img_height
|
| 339 |
+
)
|
| 340 |
+
|
| 341 |
+
# Calculate latency
|
| 342 |
+
latency = time.time() - start_time
|
| 343 |
+
total_latency += latency
|
| 344 |
+
successful_count += 1
|
| 345 |
+
|
| 346 |
+
return jsonify({
|
| 347 |
+
'success': True,
|
| 348 |
+
'tiles_to_click': tiles,
|
| 349 |
+
'num_detections': num_detections,
|
| 350 |
+
'confidence_used': used_conf,
|
| 351 |
+
'latency_s': round(latency, 3),
|
| 352 |
+
'challenge_title': challenge_title
|
| 353 |
+
})
|
| 354 |
+
|
| 355 |
+
except Exception as e:
|
| 356 |
+
failed_count += 1
|
| 357 |
+
latency = time.time() - start_time
|
| 358 |
+
total_latency += latency
|
| 359 |
+
|
| 360 |
+
print(f"Error in predict: {e}")
|
| 361 |
+
import traceback
|
| 362 |
+
traceback.print_exc()
|
| 363 |
+
|
| 364 |
+
return jsonify({
|
| 365 |
+
'success': False,
|
| 366 |
+
'error': str(e),
|
| 367 |
+
'latency_s': round(latency, 3)
|
| 368 |
+
}), 500
|
| 369 |
+
|
| 370 |
+
|
| 371 |
+
@app.route('/', methods=['GET'])
|
| 372 |
+
def index():
|
| 373 |
+
"""Root endpoint"""
|
| 374 |
+
return jsonify({
|
| 375 |
+
'name': 'reCAPTCHA 3x3 Detection API',
|
| 376 |
+
'version': '1.0.0',
|
| 377 |
+
'model': '3X3 YOLO Detection',
|
| 378 |
+
'endpoints': {
|
| 379 |
+
'POST /predict': 'Predict tiles to click',
|
| 380 |
+
'GET /health': 'Health check',
|
| 381 |
+
'GET /': 'This page'
|
| 382 |
+
}
|
| 383 |
+
})
|
| 384 |
+
|
| 385 |
+
|
| 386 |
+
if __name__ == '__main__':
|
| 387 |
+
print("\n🚀 Starting 3x3 Detection API...")
|
| 388 |
+
|
| 389 |
+
# Load model on startup
|
| 390 |
+
if load_model():
|
| 391 |
+
print("✅ Model loaded successfully!\n")
|
| 392 |
+
else:
|
| 393 |
+
print("❌ Failed to load model!\n")
|
| 394 |
+
exit(1)
|
| 395 |
+
|
| 396 |
+
# Run Flask app
|
| 397 |
+
app.run(host='0.0.0.0', port=7860, debug=False)
|
| 398 |
+
|
best.onnx
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:a6ab75da65bbc50b0b12f8fd0778fbcb076b28999fe5229a90532f3d16672f31
|
| 3 |
+
size 44752029
|
data.yaml
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
names:
|
| 2 |
+
- a fire hydrant
|
| 3 |
+
- bicycles
|
| 4 |
+
- bridge
|
| 5 |
+
- bus
|
| 6 |
+
- car
|
| 7 |
+
- chimney
|
| 8 |
+
- crosswalk
|
| 9 |
+
- ladder
|
| 10 |
+
- motorcycle
|
| 11 |
+
- other
|
| 12 |
+
- parking meters
|
| 13 |
+
- tractor
|
| 14 |
+
- traffic light
|
| 15 |
+
- tree
|
| 16 |
+
nc: 14
|
| 17 |
+
roboflow:
|
| 18 |
+
license: CC BY 4.0
|
| 19 |
+
project: rere-6ebeg
|
| 20 |
+
url: https://universe.roboflow.com/rereeee/rere-6ebeg/dataset/6
|
| 21 |
+
version: 6
|
| 22 |
+
workspace: rereeee
|
| 23 |
+
test: ../test/images
|
| 24 |
+
train: ../train/images
|
| 25 |
+
val: ../valid/images
|
requirements.txt
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
flask==3.0.0
|
| 2 |
+
flask-cors==4.0.0
|
| 3 |
+
opencv-python-headless==4.8.1.78
|
| 4 |
+
onnxruntime
|
| 5 |
+
pyyaml==6.0.1
|
| 6 |
+
Pillow==10.1.0
|
| 7 |
+
numpy==1.24.3
|
| 8 |
+
|