import cv2
from qrdecode import Scanner, QRProcessor
from qrdecode.utils.image import ImageProcessor
from qrdecode.utils.biostrip import Biostrip, ColorLines
import matplotlib.pyplot as plt

cap = cv2.VideoCapture(0)

while True:
    # Capture frame-by-frame
    ret, frame = cap.read()
    if frame is not None:


        try:
            biostrip = Biostrip(frame,
                                threshold="gaussian",
                                output_size=300,
                                output_border=20)

            #cv2.imshow("Input", frame)
            #cv2.imshow("MultiQR", biostrip.image)

            assert len(biostrip.scanner.codes) == 2
            code1 = biostrip.scanner.codes[0]
            code2 = biostrip.scanner.codes[1]
            assert isinstance(code1, ImageProcessor)
            assert isinstance(code2, ImageProcessor)

            plt.figure()
            plt.subplot(1, 4, 1)
            plt.imshow(biostrip.image[:, :, ::-1])

            plt.title("Original Image")
            plt.subplot(1, 4, 2)
            plt.title("Position Trimmed Image1")
            plt.imshow(code1.image[:, :, ::-1])

            plt.subplot(1, 4, 3)
            plt.title("Position Trimmed Image2")
            plt.imshow(code2.image[:, :, ::-1])

            processor1 = QRProcessor(code1,
                                     code_border=biostrip.output_border,
                                     code_offset=biostrip.output_offset,
                                     code_size=biostrip.output_size
                                     )
            processor2 = QRProcessor(code2,
                                     code_border=biostrip.output_border,
                                     code_offset=biostrip.output_offset,
                                     code_size=biostrip.output_size
                                     )

            #processor1.sample(correct_image=True)
            #processor2.sample(correct_image=True)

            lines = ColorLines(biostrip.slice)
            lines.mean()
            lines.solve()
            plt.show()

        except Exception as e:
            print(e)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

# When everything done, release the capture
cap.release()
cv2.destroyAllWindows()
