0

I use this code to capture and display the input from my webcam.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)

while(True):
    check, frame = cap.read()

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

cap.release()
cv2.destroyAllWindows()

I have a barcode scanner and want to check inside the while loop if a specific string gets scanned in.

input() interupts the stream from the webcam. I need something like cv2.waitKey() but for strings

while(True):
    check, frame = cap.read()

    cv2.imshow('frame',frame)
    if cv2.waitString(barcode) == '123456':
        # do something
    if cv2.waitString(barcode) == '098765':
        # do something else

I tried msvcrt, but to no avail. The stream continues but nothing gets printed.

    if msvcrt.kbhit():
        if msvcrt.getwche() == '280602017300':
            print("Barcode scanned!")

Or is there a way to skip input() until something was entered?

UPDATE

Making some progress with the help of this post. How to read keyboard-input?

I was able to update my code.

import threading
import queue
import time
import numpy as np
import cv2

def read_kbd_input(inputQueue):
    print('Ready for keyboard input:')
    while (True):
        input_str = input()
        inputQueue.put(input_str)

def main():
    inputQueue = queue.Queue()

    inputThread = threading.Thread(target=read_kbd_input, args=(inputQueue,), daemon=True)
    inputThread.start()

    cap = cv2.VideoCapture(0)

    font                   = cv2.FONT_HERSHEY_DUPLEX
    fontScale              = 1
    fontColor              = (255,255,255)
    lineType               = 2
    input_str = "test"

    while (True):
        check, frame = cap.read()

        if (inputQueue.qsize() > 0):
            input_str = inputQueue.get() 

            if (input_str == '280602017300'):
                print("do something")

        cv2.putText(frame, input_str, (10,30), font, fontScale, fontColor, lineType)

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

        time.sleep(0.01) 
    print("End.")

if (__name__ == '__main__'): 
    main()

Now the only problem left is that my webcam stream is supposed to run in fullscreen. So the console will allways be in the background and therefore wont get the inputs form the keyboard or my barcode scanner.

need it the other way around

  • I see this is an old post, hope you figured it out? I would think maybe use a GUI to show the capture which would also allow input, tkinter is included in Python standard library from v3 if I recall correctly. – T4roy Nov 10 '21 at 06:11
  • Well it's been a while. So I can't remember everything I tried. But in the end i moved the console window to the bottom left. That way it was not visible for the user. `import ctypes ctypes.windll.kernel32.SetConsoleTitleW("Tool_Inspector_Console") appname = 'Tool_Inspector_Console' xpos = 1920 ypos = 1080 width = 800 length = 600 def enumHandler(hwnd, lParam): if win32gui.IsWindowVisible(hwnd): if appname in win32gui.GetWindowText(hwnd): win32gui.MoveWindow(hwnd, xpos, ypos, width, length, True)` – Michael Vorndran Nov 11 '21 at 14:50
  • Inside the while loop I forced it to be always the front window. `console_hwnd = ctypes.windll.kernel32.GetConsoleWindow() foregroundwindow_hwnd = win32gui.GetForegroundWindow() if console_hwnd != foregroundwindow_hwnd: try: win32gui.SetForegroundWindow(console_hwnd) except Exception as e: print(e)` – Michael Vorndran Nov 11 '21 at 14:53

0 Answers0