mike23415 commited on
Commit
146e5ff
·
verified ·
1 Parent(s): 900631c

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +52 -0
app.py ADDED
@@ -0,0 +1,52 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from flask import Flask, request, jsonify
2
+ from flask_cors import CORS
3
+ import cv2
4
+ import numpy as np
5
+
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 # You might need to tune this
15
+
16
+ def is_timer_running(current_roi):
17
+ global prev_timer_roi
18
+ if prev_timer_roi is None:
19
+ prev_timer_roi = current_roi
20
+ return False
21
+
22
+ diff = cv2.absdiff(prev_timer_roi, 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 scan_qr_when_timer_active():
32
+ file = request.files.get('image')
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
+ # You may need to fine-tune this ROI for your use case
41
+ timer_roi = frame[130:160, 360:430] # adjust based on your timer position
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
+ return jsonify({'message': 'QR not detected'}), 204
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)