0

I'm working on a project where I have to run an image classifier on the frames from a live video feed with opencv. Now, I also have to get input from the user, to run various other functions. However, getting inputs on the same loop I'm updating the opencv frames wont work, as the input fuctions pauses the whole program.

For example-

import cv2

cam=cv2.VideoCapture(0)

def background():
        ret, frame= cam.read()
        cv2.imshow("Screen", frame)




while True:
    background()
    text=input("->\t")
    if text=="quit":
        break
        
cam.release()
cv2.destroyAllWindows()

So what I've done is, I've made the background fuctions, threaded, by using the library threading, so that it could run independently.

Here's the code-

import threading
import cv2

cam=cv2.VideoCapture(0)

def background():
        while True:
            ret, frame= cam.read()
            cv2.imshow("Screen", frame)


threading1 = threading.Thread(target=background)
threading1.start()

while True:
    text=input("->\t")
    if text=="quit":
        break
        
cam.release()
cv2.destroyAllWindows()

But upon running, the OpenCv window crashes. Though the input fuctions is working :/

Here's the screenshot- enter image description here

Someone please help me fix this :D

Aadhi Arun
  • 61
  • 7

1 Answers1

1

Just adding waitkey, in the background function fixes it.

Aadhi Arun
  • 61
  • 7