mike23415 commited on
Commit
fa9f5bc
·
verified ·
1 Parent(s): 74a51ff

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +14 -13
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 # You might need to tune this
 
 
 
 
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 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)
 
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)