File size: 3,031 Bytes
ea2c6ef 0209ec8 ea2c6ef | 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 | #!/usr/bin/env python3
from flask import Flask, request, jsonify
import cv2
import numpy as np
import base64
import os
app = Flask(__name__)
class SimpleCaptchaSolver:
def preprocess(self, img):
return cv2.resize(img, (64, 64))
def extract_features(self, img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
hist = cv2.calcHist([gray], [0], None, [32], [0, 256])
hist = cv2.normalize(hist, hist).flatten()
edges = cv2.Canny(gray, 50, 150)
edge_ratio = np.sum(edges > 0) / edges.size
return np.append(hist, edge_ratio)
def compare(self, feat1, feat2):
dot = np.dot(feat1, feat2)
norm1 = np.linalg.norm(feat1)
norm2 = np.linalg.norm(feat2)
if norm1 == 0 or norm2 == 0:
return 0.0
return dot / (norm1 * norm2)
def solve(self, target, options):
target = self.preprocess(target)
options = [self.preprocess(opt) for opt in options]
target_feat = self.extract_features(target)
scores = []
for opt in options:
opt_feat = self.extract_features(opt)
score = self.compare(target_feat, opt_feat)
scores.append(float(score))
best_idx = int(np.argmax(scores))
return {
'answer': best_idx + 1,
'confidence': scores[best_idx],
'scores': scores
}
solver = SimpleCaptchaSolver()
def b64_to_cv2(b64_str):
img_data = base64.b64decode(b64_str.split(',')[-1])
img_array = np.frombuffer(img_data, np.uint8)
return cv2.imdecode(img_array, cv2.IMREAD_COLOR)
@app.route('/', methods=['GET'])
def home():
return jsonify({
'service': 'CAPTCHA Solver API',
'version': '2.0',
'status': 'online',
'endpoints': {
'/health': 'GET - Health check',
'/solve': 'POST - Solve CAPTCHA'
}
})
@app.route('/health', methods=['GET'])
def health():
return jsonify({'status': 'ok', 'solver': 'SimpleCaptchaSolver v2.0'})
@app.route('/solve', methods=['POST'])
def solve():
try:
data = request.json
if not data or 'target' not in data or 'options' not in data:
return jsonify({'status': 0, 'error': 'Missing fields'}), 400
target = b64_to_cv2(data['target'])
options = [b64_to_cv2(opt) for opt in data['options']]
result = solver.solve(target, options)
return jsonify({'status': 1, 'result': result})
except Exception as e:
return jsonify({'status': 0, 'error': str(e)}), 500
if __name__ == '__main__':
# Hugging Face Spaces α PORT env variable ααα― αααΊααΎααΊαα±αΈαα«ααααΊα·αααΊα
# ααααΊααΎααΊαα¬αΈαααΊ 7860 ααα― default αα―αΆαΈαα«αααΊα
port = int(os.environ.get('PORT', 7860))
print("="*60)
print(f"π CAPTCHA Solver API")
print(f"π Port: {port}")
print("="*60)
app.run(host='0.0.0.0', port=port, debug=False)
|