Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/opencv/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Tkinter显示其他窗口_Python_Opencv_User Interface_Tkinter - Fatal编程技术网

Python Tkinter显示其他窗口

Python Tkinter显示其他窗口,python,opencv,user-interface,tkinter,Python,Opencv,User Interface,Tkinter,是否可以显示OpenCV使用Tkinter创建的窗口?我想用Tkinter打开它,这样我可以提供更多的GUI功能。这以前做过吗?我检查了谷歌和它本身,但没有发现任何东西 因此,正如kobejohn所建议的,我附加了摄像头捕获和显示的代码 import cv2 import urllib import numpy as np import subprocess stream=urllib.urlopen('IP Address') bytes='' while True: bytes+

是否可以显示OpenCV使用Tkinter创建的窗口?我想用Tkinter打开它,这样我可以提供更多的GUI功能。这以前做过吗?我检查了谷歌和它本身,但没有发现任何东西

因此,正如kobejohn所建议的,我附加了摄像头捕获和显示的代码

import cv2
import urllib 
import numpy as np
import subprocess

stream=urllib.urlopen('IP Address')
bytes=''
while True:
    bytes+=stream.read(1024)
    a = bytes.find('\xff\xd8')
    b = bytes.find('\xff\xd9')
    if a!=-1 and b!=-1:
        jpg = bytes[a:b+2]
        bytes= bytes[b+2:]
        i = cv2.imdecode(np.fromstring(jpg, dtype=np.uint8),cv2.CV_LOAD_IMAGE_COLOR)
        cv2.imshow('i',i)
        if cv2.waitKey(1) ==27:
            exit(0)

此代码基于注释中的讨论。它不会将opencv窗口放入tkinter。它只是将opencv图像放入tkinter

Prakar,我没有可用的IP摄像头,你能试试这个吗?我已经确认它与这个答案底部的USB代码一起工作

基本上,我只是将您的jpg阅读代码插入到的简化版本中,以获得下面的代码。它使用两步转换:字节-->opencv图像-->tkinter图像。可能有一种更有效的方法可以直接从字节转换为tkinter映像,但如果性能出现问题,您可以解决这个问题


IP摄像机
USB摄像机 *编辑-我已经确认下面的代码可以使用opencv从USB摄像头拍摄视频并将其发送到tkinter窗口。因此,希望上面的代码将适用于您的ip摄像头

import cv2
import PIL.Image
import PIL.ImageTk
import Tkinter as tk


def update_image(image_label, cv_capture):
    cv_image = cv_capture.read()[1]
    cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
    pil_image = PIL.Image.fromarray(cv_image)
    tk_image = PIL.ImageTk.PhotoImage(image=pil_image)
    image_label.configure(image=tk_image)
    image_label._image_cache = tk_image  # avoid garbage collection
    root.update()


def update_all(root, image_label, cv_capture):
    if root.quit_flag:
        root.destroy()  # this avoids the update event being in limbo
    else:
        update_image(image_label, cv_capture)
        root.after(10, func=lambda: update_all(root, image_label, cv_capture))


if __name__ == '__main__':
    cv_capture = cv2.VideoCapture()
    cv_capture.open(0)  # have to use whatever your camera id actually is
    root = tk.Tk()
    setattr(root, 'quit_flag', False)
    def set_quit_flag():
        root.quit_flag = True
    root.protocol('WM_DELETE_WINDOW', set_quit_flag)  # avoid errors on exit
    image_label = tk.Label(master=root)  # the video will go here
    image_label.pack()
    root.after(0, func=lambda: update_all(root, image_label, cv_capture))
    root.mainloop()

此代码基于注释中的讨论。它不会将opencv窗口放入tkinter。它只是将opencv图像放入tkinter

Prakar,我没有可用的IP摄像头,你能试试这个吗?我已经确认它与这个答案底部的USB代码一起工作

基本上,我只是将您的jpg阅读代码插入到的简化版本中,以获得下面的代码。它使用两步转换:字节-->opencv图像-->tkinter图像。可能有一种更有效的方法可以直接从字节转换为tkinter映像,但如果性能出现问题,您可以解决这个问题


IP摄像机
USB摄像机 *编辑-我已经确认下面的代码可以使用opencv从USB摄像头拍摄视频并将其发送到tkinter窗口。因此,希望上面的代码将适用于您的ip摄像头

import cv2
import PIL.Image
import PIL.ImageTk
import Tkinter as tk


def update_image(image_label, cv_capture):
    cv_image = cv_capture.read()[1]
    cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
    pil_image = PIL.Image.fromarray(cv_image)
    tk_image = PIL.ImageTk.PhotoImage(image=pil_image)
    image_label.configure(image=tk_image)
    image_label._image_cache = tk_image  # avoid garbage collection
    root.update()


def update_all(root, image_label, cv_capture):
    if root.quit_flag:
        root.destroy()  # this avoids the update event being in limbo
    else:
        update_image(image_label, cv_capture)
        root.after(10, func=lambda: update_all(root, image_label, cv_capture))


if __name__ == '__main__':
    cv_capture = cv2.VideoCapture()
    cv_capture.open(0)  # have to use whatever your camera id actually is
    root = tk.Tk()
    setattr(root, 'quit_flag', False)
    def set_quit_flag():
        root.quit_flag = True
    root.protocol('WM_DELETE_WINDOW', set_quit_flag)  # avoid errors on exit
    image_label = tk.Label(master=root)  # the video will go here
    image_label.pack()
    root.after(0, func=lambda: update_all(root, image_label, cv_capture))
    root.mainloop()

此代码基于注释中的讨论。它不会将opencv窗口放入tkinter。它只是将opencv图像放入tkinter

Prakar,我没有可用的IP摄像头,你能试试这个吗?我已经确认它与这个答案底部的USB代码一起工作

基本上,我只是将您的jpg阅读代码插入到的简化版本中,以获得下面的代码。它使用两步转换:字节-->opencv图像-->tkinter图像。可能有一种更有效的方法可以直接从字节转换为tkinter映像,但如果性能出现问题,您可以解决这个问题


IP摄像机
USB摄像机 *编辑-我已经确认下面的代码可以使用opencv从USB摄像头拍摄视频并将其发送到tkinter窗口。因此,希望上面的代码将适用于您的ip摄像头

import cv2
import PIL.Image
import PIL.ImageTk
import Tkinter as tk


def update_image(image_label, cv_capture):
    cv_image = cv_capture.read()[1]
    cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
    pil_image = PIL.Image.fromarray(cv_image)
    tk_image = PIL.ImageTk.PhotoImage(image=pil_image)
    image_label.configure(image=tk_image)
    image_label._image_cache = tk_image  # avoid garbage collection
    root.update()


def update_all(root, image_label, cv_capture):
    if root.quit_flag:
        root.destroy()  # this avoids the update event being in limbo
    else:
        update_image(image_label, cv_capture)
        root.after(10, func=lambda: update_all(root, image_label, cv_capture))


if __name__ == '__main__':
    cv_capture = cv2.VideoCapture()
    cv_capture.open(0)  # have to use whatever your camera id actually is
    root = tk.Tk()
    setattr(root, 'quit_flag', False)
    def set_quit_flag():
        root.quit_flag = True
    root.protocol('WM_DELETE_WINDOW', set_quit_flag)  # avoid errors on exit
    image_label = tk.Label(master=root)  # the video will go here
    image_label.pack()
    root.after(0, func=lambda: update_all(root, image_label, cv_capture))
    root.mainloop()

此代码基于注释中的讨论。它不会将opencv窗口放入tkinter。它只是将opencv图像放入tkinter

Prakar,我没有可用的IP摄像头,你能试试这个吗?我已经确认它与这个答案底部的USB代码一起工作

基本上,我只是将您的jpg阅读代码插入到的简化版本中,以获得下面的代码。它使用两步转换:字节-->opencv图像-->tkinter图像。可能有一种更有效的方法可以直接从字节转换为tkinter映像,但如果性能出现问题,您可以解决这个问题


IP摄像机
USB摄像机 *编辑-我已经确认下面的代码可以使用opencv从USB摄像头拍摄视频并将其发送到tkinter窗口。因此,希望上面的代码将适用于您的ip摄像头

import cv2
import PIL.Image
import PIL.ImageTk
import Tkinter as tk


def update_image(image_label, cv_capture):
    cv_image = cv_capture.read()[1]
    cv_image = cv2.cvtColor(cv_image, cv2.COLOR_BGR2RGB)
    pil_image = PIL.Image.fromarray(cv_image)
    tk_image = PIL.ImageTk.PhotoImage(image=pil_image)
    image_label.configure(image=tk_image)
    image_label._image_cache = tk_image  # avoid garbage collection
    root.update()


def update_all(root, image_label, cv_capture):
    if root.quit_flag:
        root.destroy()  # this avoids the update event being in limbo
    else:
        update_image(image_label, cv_capture)
        root.after(10, func=lambda: update_all(root, image_label, cv_capture))


if __name__ == '__main__':
    cv_capture = cv2.VideoCapture()
    cv_capture.open(0)  # have to use whatever your camera id actually is
    root = tk.Tk()
    setattr(root, 'quit_flag', False)
    def set_quit_flag():
        root.quit_flag = True
    root.protocol('WM_DELETE_WINDOW', set_quit_flag)  # avoid errors on exit
    image_label = tk.Label(master=root)  # the video will go here
    image_label.pack()
    root.after(0, func=lambda: update_all(root, image_label, cv_capture))
    root.mainloop()


直接回答,我不知道怎么做,除非我有充分的理由避免其他解决方案,否则我不会尝试。你能把图像数据直接转换成文本吗?我想你的开发时间会少得多。@kobejohn问题是我不是在处理视频,我是在处理mjpeg流。我不知道如何使用tkinter访问它。如果我考虑我正在获取的图像并转换它们,然后在Tkinter显示它们,那我该怎么办呢?如果我不使用tkinter,我可以使用其他东西来构建一个GUI来支持它吗?类似于wxwidgets、pygtk或qt?您可以使用qt作为GUI后端构建OpenCV(当您从源代码编译OpenCV时,有一个with_qt选项)。然后,您将能够使用Qt@remi在窗户上。你知道如何在windows上做到这一点吗?@PrakharMohanSrivastava你能发布一段非常简单的代码来在opencv中显示视频吗?然后有人可以告诉你如何将视频/图像转换为tkinter格式。直接回答,我不知道如何转换,除非我有充分的理由避免使用其他解决方案,否则我不会尝试。你能把图像数据直接转换成文本吗?我想你的开发时间会少得多。@kobejohn问题是我不是在处理视频,我是在处理mjpeg流。我不知道如何使用tkinter访问它。如果我考虑我正在获取的图像并转换它们,然后在Tkinter显示它们,那我该怎么办呢?如果我不使用tkinter,我可以使用其他东西来构建一个GUI来支持它吗?类似于wxwidgets、pygtk或qt?您可以使用qt作为GUI后端构建OpenCV(当您从源代码编译OpenCV时,有一个with_qt选项)。然后,您将能够使用Qt@remi在窗户上。你知道如何在windows上做到这一点吗?@PrakharMohanSrivastava你能发布一段非常简单的代码来在opencv中显示视频吗?然后有人可以告诉你如何将视频/图像转换为tkinter格式。直接回答,我不知道如何转换,除非我有充分的理由避免使用其他解决方案,否则我不会尝试。你能把图像数据转换成a吗