Update app.py
Browse files
app.py
CHANGED
@@ -6,12 +6,13 @@ import numpy as np
|
|
6 |
app = Flask(__name__)
|
7 |
CORS(app)
|
8 |
|
9 |
-
# Initialize QRCodeDetector
|
10 |
qr_detector = cv2.QRCodeDetector()
|
11 |
-
|
12 |
-
# Simple in-memory state for previous frame ROI (for change detection)
|
13 |
prev_timer_roi = None
|
14 |
-
CHANGE_THRESHOLD = 500 #
|
|
|
|
|
|
|
|
|
15 |
|
16 |
def is_timer_running(current_roi):
|
17 |
global prev_timer_roi
|
@@ -23,30 +24,30 @@ def is_timer_running(current_roi):
|
|
23 |
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
|
24 |
_, thresh = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY)
|
25 |
changed_pixels = cv2.countNonZero(thresh)
|
26 |
-
|
27 |
prev_timer_roi = current_roi
|
28 |
return changed_pixels > CHANGE_THRESHOLD
|
29 |
|
30 |
@app.route('/scan', methods=['POST'])
|
31 |
-
def
|
32 |
-
|
33 |
-
if not file:
|
34 |
return jsonify({'error': 'No image uploaded'}), 400
|
35 |
|
|
|
36 |
img_bytes = file.read()
|
37 |
npimg = np.frombuffer(img_bytes, np.uint8)
|
38 |
frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
|
39 |
|
40 |
-
#
|
41 |
-
timer_roi = frame[130:160, 360:430]
|
42 |
|
43 |
if is_timer_running(timer_roi):
|
44 |
data, points, _ = qr_detector.detectAndDecode(frame)
|
45 |
if data:
|
46 |
-
return jsonify({'qr_data': data})
|
47 |
-
|
|
|
48 |
else:
|
49 |
return jsonify({'message': 'Timer not running'}), 204
|
50 |
|
51 |
if __name__ == '__main__':
|
52 |
-
app.run(host='0.0.0.0', port=7860)
|
|
|
6 |
app = Flask(__name__)
|
7 |
CORS(app)
|
8 |
|
|
|
9 |
qr_detector = cv2.QRCodeDetector()
|
|
|
|
|
10 |
prev_timer_roi = None
|
11 |
+
CHANGE_THRESHOLD = 500 # Tune this based on test results
|
12 |
+
|
13 |
+
@app.route('/')
|
14 |
+
def home():
|
15 |
+
return jsonify({"message": "Timer-aware QR Scanner API is running"}), 200
|
16 |
|
17 |
def is_timer_running(current_roi):
|
18 |
global prev_timer_roi
|
|
|
24 |
gray = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
|
25 |
_, thresh = cv2.threshold(gray, 25, 255, cv2.THRESH_BINARY)
|
26 |
changed_pixels = cv2.countNonZero(thresh)
|
|
|
27 |
prev_timer_roi = current_roi
|
28 |
return changed_pixels > CHANGE_THRESHOLD
|
29 |
|
30 |
@app.route('/scan', methods=['POST'])
|
31 |
+
def scan_qr_if_timer_active():
|
32 |
+
if 'image' not in request.files:
|
|
|
33 |
return jsonify({'error': 'No image uploaded'}), 400
|
34 |
|
35 |
+
file = request.files['image']
|
36 |
img_bytes = file.read()
|
37 |
npimg = np.frombuffer(img_bytes, np.uint8)
|
38 |
frame = cv2.imdecode(npimg, cv2.IMREAD_COLOR)
|
39 |
|
40 |
+
# Adjust this ROI for timer location (y1:y2, x1:x2)
|
41 |
+
timer_roi = frame[130:160, 360:430]
|
42 |
|
43 |
if is_timer_running(timer_roi):
|
44 |
data, points, _ = qr_detector.detectAndDecode(frame)
|
45 |
if data:
|
46 |
+
return jsonify({'qr_data': data}), 200
|
47 |
+
else:
|
48 |
+
return jsonify({'message': 'QR code not detected'}), 204
|
49 |
else:
|
50 |
return jsonify({'message': 'Timer not running'}), 204
|
51 |
|
52 |
if __name__ == '__main__':
|
53 |
+
app.run(host='0.0.0.0', port=7860)
|