简体   繁体   中英

How to display an image and a terminal in OpenCV

How can use OpenCV to display my webcam and a terminal at the same time? I need the terminal to run some commands to make the webcam appear and others different actions with the object detection.

Here's a very simple example of running a webcam while at the same time being able to use terminal input() by doing all the opencv frame capturing, and imshow ing from a separate thread:

from threading import Thread
import cv2

shouldExit = False
color = True

def cam():
    cap = cv2.VideoCapture(0, cv2.CAP_DSHOW)
    while not shouldExit:
        ret, frame = cap.read()
        if not color:
            frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        cv2.imshow("window title", frame)
        cv2.waitKey(1)
    cap.release()
    cv2.destroyAllWindows()

t = Thread(target=cam)
t.start()

while True:
    print('select an action: q-quit, t-toggle color')
    choice = input(">").strip().lower()
    if choice == 'q':
        shouldExit = True
        t.join()
        break
    elif choice == 't':
        color = not color
    else:
        print('invalid input')

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM