使用 OpenCv 和 Tkinter 进行相机捕获的简单版本:
import Tkinter as tk
import cv2
from PIL import Image, ImageTk
width, height = 800, 600
cap = cv2.VideoCapture(0)
cap.set(cv2.CAP_PROP_FRAME_WIDTH, width)
cap.set(cv2.CAP_PROP_FRAME_HEIGHT, height)
root = tk.Tk()
root.bind('<Escape>', lambda e: root.quit())
lmain = tk.Label(root)
lmain.pack()
def show_frame():
_, frame = cap.read()
frame = cv2.flip(frame, 1)
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)
show_frame()
root.mainloop()
您将需要下载并安装 PIL ...
更新:
... 和 OpenCV 让它工作。
要安装 PIL,请在终端/命令提示符中运行以下命令:
pip install Pillow
或者python -m pip install Pillow
要安装 OpenCV,请在终端/命令提示符中运行以下命令:
pip install opencv-python
或者python -m pip install opencv-python