webstream and barcode recognition running in parallel

This commit is contained in:
paul-loedige 2023-01-16 02:25:29 +00:00
parent feeb93650c
commit 3eca8a3b19

View File

@ -1,14 +1,27 @@
from flask import Flask, render_template, Response from flask import Flask, render_template, Response
import cv2 import cv2
import imutils
from imutils.video import VideoStream
from pyzbar import pyzbar
import time
app = Flask(__name__) app = Flask(__name__)
vs = VideoStream(src=0).start()
time.sleep(2)
frame = None
def stream(camera_index): def stream(camera_index):
cam = cv2.VideoCapture(camera_index) global frame
while True: while True:
_, frame = cam.read() raw_frame = vs.read()
frame = cv2.resize(frame,(400, 240)) stream_frame = imutils.resize(raw_frame, width=400)
ret, jpg = cv2.imencode('.jpg', frame) decode_frame = imutils.resize(raw_frame, width=800)
barcodes = pyzbar.decode(decode_frame)
for barcode in barcodes:
barcode_data = barcode.data.decode("utf-8")
print(barcode_data)
ret, jpg = cv2.imencode('.jpg', stream_frame)
jpg2bytes = jpg.tobytes() jpg2bytes = jpg.tobytes()
yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + jpg2bytes + b'\r\n\r\n' yield b'--frame\r\nContent-Type: image/jpeg\r\n\r\n' + jpg2bytes + b'\r\n\r\n'
@ -21,4 +34,4 @@ def stream_fedd():
return Response(stream(0), mimetype='multipart/x-mixed-replace; boundary=frame') return Response(stream(0), mimetype='multipart/x-mixed-replace; boundary=frame')
if __name__ == '__main__': if __name__ == '__main__':
app.run(debug=True, port=5000, host='0.0.0.0') app.run(debug=False, port=5000, host='0.0.0.0')