Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Show webcam sequence TkInter

I did a program with python, importing the OpenCV's libraries. Now, I'm doing the GUI in Tkinter. I'm trying to show the webcam in the GUI but I couldn't. I put the code in the Function because I would like with a push button see my webcam.

My code is:

def webcam():
   img= cv.QueryFrame(cap)
   cam= PhotoImage(img)
   label1 = Label(root, image=cam)
   label1.image = cam
   label1.pack()
   label1.place(x=0, y=400)

Also, I don't know how update constantly without a while cycle, because I have another push button to quit the program.

like image 751
Margarita Gonzalez Avatar asked May 03 '13 20:05

Margarita Gonzalez


People also ask

How to show webcam in tkinter?

Using the OpenCV module, we have to show the webcam in a tkinter window. Once the installation has been done, we can start creating the structure and GUI of the application. The basic functionality of our application would be to open the web camera (if possible) using OpenCV.

Can you use tkinter and OpenCV together?

Using Tkinter Library, we can create an interactive application that uses OpenCV as the essential part of the application. To create the application, you are required to install OpenCV in your local machine and make sure that Python Pillow package is pre-installed.

How do you use Customtkinter?

To use CustomTkinter, just place the /customtkinter folder from this repository next to your program, and then you can do import customtkinter .


3 Answers

A simple version of camera capture using OpenCv and 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()

You will need to download and install PIL...

UPDATE:

... and OpenCV for this to work.

To Install PIL, run the following command in your Terminal/Command Prompt:

pip install Pillow or python -m pip install Pillow

To Install OpenCV, run the following command in your Terminal/Command Prompt:

pip install opencv-python or python -m pip install opencv-python

like image 163
Kieleth Avatar answered Oct 04 '22 19:10

Kieleth


Kieleth's updated code for py3.7

import PIL
from PIL import Image,ImageTk
import pytesseract
import cv2
from tkinter import *
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()
root.bind('<Escape>', lambda e: root.quit())
lmain = Label(root)
lmain.pack()

def show_frame():
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    img = PIL.Image.fromarray(cv2image)
    imgtk = ImageTk.PhotoImage(image=img)
    lmain.imgtk = imgtk
    lmain.configure(image=imgtk)
    lmain.after(10, show_frame)

show_frame()
root.mainloop()
like image 33
Stanley Avatar answered Oct 04 '22 20:10

Stanley


Try this code:

from collections import deque
import cv2
from PIL import Image, ImageTk
import time
import Tkinter as tk

def quit_(root):
    root.destroy()

def update_image(image_label, cam):
    (readsuccessful, f) = cam.read()
    gray_im = cv2.cvtColor(f, cv2.COLOR_BGR2GRAY)
    a = Image.fromarray(gray_im)
    b = ImageTk.PhotoImage(image=a)
    image_label.configure(image=b)
    image_label._image_cache = b  # avoid garbage collection
    root.update()


def update_fps(fps_label):
    frame_times = fps_label._frame_times
    frame_times.rotate()
    frame_times[0] = time.time()
    sum_of_deltas = frame_times[0] - frame_times[-1]
    count_of_deltas = len(frame_times) - 1
    try:
        fps = int(float(count_of_deltas) / sum_of_deltas)
    except ZeroDivisionError:
        fps = 0
    fps_label.configure(text='FPS: {}'.format(fps))


def update_all(root, image_label, cam, fps_label):
    update_image(image_label, cam)
    update_fps(fps_label)
    root.after(20, func=lambda: update_all(root, image_label, cam, fps_label))


if __name__ == '__main__':
    root = tk.Tk() 
    image_label = tk.Label(master=root)# label for the video frame
    image_label.pack()
    cam = cv2.VideoCapture(1) 
    fps_label = tk.Label(master=root)# label for fps
    fps_label._frame_times = deque([0]*5)  # arbitrary 5 frame average FPS
    fps_label.pack()
    # quit button
    quit_button = tk.Button(master=root, text='Quit',command=lambda: quit_(root))
    quit_button.pack()
    # setup the update callback
    root.after(0, func=lambda: update_all(root, image_label, cam, fps_label))
    root.mainloop()
like image 21
Rohan Williams Avatar answered Oct 04 '22 21:10

Rohan Williams