Scanner / app.py
mike23415's picture
Update app.py
fa9f5bc verified
from flask import Flask, request, jsonify
from flask_cors import CORS
import cv2
import numpy as np
app = Flask(__name__)
CORS(app)
qr_detector = cv2.QRCodeDetector()
prev_timer_roi = None
CHANGE_THRESHOLD = 500 # Tune this based on test results
@app.route('/')
def home():
return jsonify({"message": "Timer-aware QR Scanner API is running"}), 200
def is_timer_running(current_roi):
global prev_timer_roi
if prev_timer_roi is None:
prev_timer_roi = current_roi
return False
diff = cv2.absdiff(prev_timer_roi, current_roi)
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
_, thresh = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY)
changed_pixels = cv2.countNonZero(thresh)
prev_timer_roi = current_roi
return changed_pixels > CHANGE_THRESHOLD
@app.route('/scan', methods=['POST'])
def scan_qr_if_timer_active():
if 'image' not in request.files:
return jsonify({'error': 'No image uploaded'}), 400
file = request.files['image']
img_bytes = file.read()
npimg = np.frombuffer(img_bytes, np.uint8)
frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
# Adjust this ROI for timer location (y1:y2, x1:x2)
timer_roi = frame[130:160, 360:430]
if is_timer_running(timer_roi):
data, points, _ = qr_detector.detectAndDecode(frame)
if data:
return jsonify({'qr_data': data}), 200
else:
return jsonify({'message': 'QR code not detected'}), 204
else:
return jsonify({'message': 'Timer not running'}), 204
if __name__ == '__main__':
app.run(host='0.0.0.0', port=7860)