File size: 1,610 Bytes
146e5ff
 
 
 
 
 
 
 
 
 
fa9f5bc
 
 
 
 
146e5ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
fa9f5bc
 
146e5ff
 
fa9f5bc
146e5ff
 
 
 
fa9f5bc
 
146e5ff
 
 
 
fa9f5bc
 
 
146e5ff
 
 
 
fa9f5bc
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
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)