0

I want to build a GUI to scan QR code with Tkinter python to record video feed with some buttons and listbox. I have completed in combine OpenCV with Tkinter. However, I can not scan any QR code. I figured my problem is from the line barcodes = pyzbar.decode(frame)but I do not know what its error because the program still runs smoothly. Here is my code :

from imutils.video import VideoStream
from pyzbar import pyzbar
from PIL import Image, ImageTk
from firebase_admin import credentials, firestore

import Tkinter as tki
import firebase_admin
import threading
import argparse
import datetime
import imutils
import time
import cv2
import RPi.GPIO as GPIO
import customer
from customer import Customerlist

    window = tki.Tk()  #Makes main window

    #Graphics window
    imageFrame = tki.Frame(window, width=600, height=500)
    imageFrame.grid(row=0, column=0, padx=10, pady=2)

    #Capture video frames
    lmain = tki.Label(imageFrame)
    lmain.grid(row=0, column=0)
    #gst = "nvcamerasrc ! video/x-raw(memory:NVMM), width=(int)640, height=(int)480, format=(string)I420, framerate=(fraction)24/1 ! nvvidconv flip-method=6 ! video/x-raw, format=(string)I420 ! videoconvert ! video/x-raw, format=(string)BGR ! appsink"
    cap = cv2.VideoCapture(0)

    def show_frame():
        ret, frame = cap.read()
        frame = cv2.flip(frame, 1)
        frame = imutils.resize(frame, width=400)
        cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
        img = Image.fromarray(cv2image)
        imgtk = ImageTk.PhotoImage(image=img)
        lmain.imgtk = imgtk
        lmain.configure(image=imgtk)
        lmain.after(10, show_frame) 



    while True : 
                    show_frame()
                    barcodes = pyzbar.decode(frame) 
                    window.mainloop()  #Starts GUI

                    for barcode in barcodes:
                        (x, y, w, h) = barcode.rect
                        cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 0, 255), 2)
                        # the barcode data is a bytes object so if we want to draw it
                        # on our output image we need to convert it to a string first
                        barcodeData = barcode.data.decode("utf-8")
                        barcodeType = barcode.type  

                        # draw the barcode data and barcode type on the image
                        text = "{} ({})".format(barcodeData, barcodeType)
                        cv2.putText(frame, text, (x, y - 10),
                            cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)  
                cv2.imshow("Thesis", frame)
                key = cv2.waitKey(1) & 0xFF      

csv.close()
cap.release()
cv2.destroyAllWindows()

Can anyone help me? I am much appriciate

  • Hello and welcome to stack overflow. Please try to trim your code and create a [Minimal, Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) of the issue – zteffi Oct 15 '19 at 14:53

1 Answers1

0

window.mainloop() is a blocking call - it starts an infinite loop. So the code below it, that processes the frame, will never execute.
A solution would be to put the barcode processing code in a separate function that is called from show_frame().

example of similar issue

J.D.
  • 4,511
  • 2
  • 7
  • 20