File size: 1,599 Bytes
146e5ff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
from flask import Flask, request, jsonify
from flask_cors import CORS
import cv2
import numpy as np

app = Flask(__name__)
CORS(app)

# Initialize QRCodeDetector
qr_detector = cv2.QRCodeDetector()

# Simple in-memory state for previous frame ROI (for change detection)
prev_timer_roi = None
CHANGE_THRESHOLD = 500  # You might need to tune this

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_when_timer_active():
    file = request.files.get('image')
    if not file:
        return jsonify({'error': 'No image uploaded'}), 400

    img_bytes = file.read()
    npimg = np.frombuffer(img_bytes, np.uint8)
    frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR)

    # You may need to fine-tune this ROI for your use case
    timer_roi = frame[130:160, 360:430]  # adjust based on your timer position

    if is_timer_running(timer_roi):
        data, points, _ = qr_detector.detectAndDecode(frame)
        if data:
            return jsonify({'qr_data': data})
        return jsonify({'message': 'QR not detected'}), 204
    else:
        return jsonify({'message': 'Timer not running'}), 204

if __name__ == '__main__':
    app.run(host='0.0.0.0', port=7860)