Open CV is a Python library that is used to work with Computer Vision and other artificial artifacts. Open CV has inbuilt functions and methods which provide access to work with Computer Vision in Artificial Intelligence and Machine Learning. Some of the examples of Open CV are: face detection, object detection, X-ray and other industrial uses.
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. You can install these packages by typing the following commands in the notebook.
pip install open-cv pip install Pillow
Once the installation is complete, 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. So, to display each and every captured frame, we can use Python Pillow (PIL) package which converts the frame into an Image. The Image now can be used in a Label widget that iteratively displays every captured frame in the window.
# Import required Libraries from tkinter import * from PIL import Image, ImageTk import cv2 # Create an instance of TKinter Window or frame win= Tk() # Set the size of the window win.geometry("700x350")# Create a Label to capture the Video frames label =Label(win) label.grid(row=0, column=0) cap= cv2.VideoCapture(0) # Define function to show frame def show_frames(): # Get the latest frame and convert into Image cv2image= cv2.cvtColor(cap.read(),cv2.COLOR_BGR2RGB) img = Image.fromarray(cv2image) # Convert image to PhotoImage imgtk = ImageTk.PhotoImage(image = img) label.imgtk = imgtk label.configure(image=imgtk) # Repeat after an interval to capture continiously label.after(20, show_frames) show_frames() win.mainloop()
When we execute the above code, it will display a window that opens the user camera to capture the frames.