0

When I run the below code. The camera opens and we can read the barcode. What I need is that the camera window remains at the side of my Tkinter GUI application rather than poping up. Here's the code

from imutils.video import VideoStream
from pyzbar import pyzbar
import argparse
import datetime
from datetime import datetime
import imutils
import time
import cv2
import winsound

frequency = 600  # Set Frequency To 2500 Hertz
duration = 800  # Set Duration To 1000 ms == 1 second

ap = argparse.ArgumentParser()
ap.add_argument("-o", "--output", type=str, default="barcodesData.csv",
                help="path to output CSV file ")
args = vars(ap.parse_args())

print("Starting webcam")

vs = VideoStream(src=0).start()
time.sleep(2.0)
csvWrite = open(args["output"], "w")
found = set()
while True:
    frameData = vs.read()
    frameData = imutils.resize(frameData, width=600)
    barcodes = pyzbar.decode(frameData)
    for barcode in barcodes:
        (x, y, width, height) = barcode.rect
        cv2.rectangle(frameData, (x, y), (x + width, y + height), (0, 0, 255), 2)
        barcodeData = barcode.data.decode("utf-8")
        barcodeType = barcode.type
        textData = "{} ({})".format(barcodeData, barcodeType)
        cv2.putText(frameData, textData, (x, y - 10),
                    cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        if barcodeData not in found:
            csvWrite.write("{},{}\n".format(datetime.today().strftime('%Y-%m-%d'),
                                            barcodeData))
            csvWrite.flush()
            found.add(barcodeData)
            winsound.Beep(frequency, duration)
    cv2.imshow("Barcode Scanner", frameData)
    key = cv2.waitKey(1) & 0xFF
    if key == ord("e"):
        break

# close the output CSV file do a bit of cleanup
print("\nWait while we calculate cost...")
csvWrite.close()
cv2.destroyAllWindows()
vs.stop()

time.sleep(1.0)

TO be specific. I'm making a billing software where I can read the barcodes of the products and make a bill. The camera separate screen is annoying so if the camera is on any side of the canvas all the time. It would be more quick.

Tahil Bansal
  • 135
  • 6
  • 1
    So, you've explained what you want, and then provided a lot of code, but you didn't explain what you're having trouble with. It appears that you haven't implemented anything in TKinter yet. Is there a specific part of it you're stuck on? If so you should go into detail. If you're just asking us to tell or show you how to do this all in TKinter from scratch, that's a bit too broad of a question I think. – Random Davis Feb 10 '21 at 20:42
  • You need to convert `OpenCV` image to `Pillow` (or `PIL`) image and show the image using tkinter `Label` instead of `cv2.imshow()`. Search SO and you can find few examples. – acw1668 Feb 11 '21 at 04:20

1 Answers1

0

I encoded the IDs of each product/item in my database in a QR code. When that particular item is being sought for, I used CV2 to detect and decode the QR code.

Here is the code:

def encode_qr():

    import qrcode

    import random as r

    item_code = r.randint(00000000,99999999)

    data = item_code
    qrfile = "qr_image_name_for_specified_item.png"
    # generate qrcode
    qrimage = qrcode.make(data)
    # save image
    fl = qrimage.save(qrfile)
    print("Done generating qrcode")


def decode_qr():

    import cv2
    filename="qr_image_name_for_specified_item.png" 
    # alternatively webcam cv2.VideoCapture(0)
    # read image 
    image = cv2.imread(filename)

    # initialize qrcode detector

   

    detector = cv2.QRCodeDetector()
    # detect and decode
    info, v_array, binary_qrcode=detector.detectAndDecode(image)
    # if null?
    if v_array is None:
        print("No qrcode detected or probably some technical issues occurred")
    else:
        print("QRCODE data")
        print(info)
        # below i am working with the import sqlite3 as sql3
        sqldb = sql3.connect("your_database.db")
        cur = sqldb.cursor()
        cur.execute("select * from table where ID=?, (info,))
        rows = cur.fetchall()
        for r in rows:
            print(r) # this will loop all the item details with the itemcode
           print(r[1]) # a specific detail with the item_code
ChrisGPT was on strike
  • 127,765
  • 105
  • 273
  • 257
  • 2
    Please read [answer]. SHOUTING at us is not appropriate. Calling yourself "Godzone" repeatedly is not appropriate. Please format your code properly and _clearly explain_ how it answers the question. Brand new code is a lot less useful than explaining how the code in the question can be modified. And yelling about how great you are is a good way to get suspended. – ChrisGPT was on strike Dec 17 '21 at 23:21
  • This code does not help to the user asking the question – Gonzalo Odiard Dec 18 '21 at 19:41