Scanner / app.py
mike23415's picture
Create app.py
146e5ff verified
raw
history blame
1.6 kB
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)