alliedvision / VimbaPython

Old Allied Vision Vimba Python API. The successor to this API is VmbPy
BSD 2-Clause "Simplified" License
93 stars 40 forks source link

Increase Frames Per Second of Vimba SDK with Tkinter #141

Open smitshah-19 opened 1 year ago

smitshah-19 commented 1 year ago

I have to use Allied Vision Camera that uses Vimba SDK to make a GUI that captures the photo. I have created a small GUI, but the FPS is very less. I want to increase FPS to 30. Is that possible, if so can someone please explain me how to increase FPS?

The related code is shown below:


from datetime import datetime
from tkinter import filedialog
from tkinter import *
import tkinter as tk
import os
import cv2
from pymba import Vimba
import cv2
import time
from vimba import *
import os
from datetime import datetime

root = tk.Tk()
root.title("Pycam")
root.geometry("1340x700")
root.configure(background="sky blue")

from bs4 import BeautifulSoup as bs

# Reading the data inside the xml
# file to a variable under the name
# data
with open('vimba.xml', 'r') as file:
    # Read each line in the file, readlines() returns a list of lines
    content = file.readlines()
    # Combine the lines in the list into a string
    content = "".join(content)
    bs_content = bs(content, "lxml")

exposure = float(bs_content.find("feature", {'name': 'ExposureTimeAbs'}).get_text())
gain = float(bs_content.find("feature", {'name': 'Gain'}).get_text())
whitebalance = bs_content.find("feature", {'name': 'BalanceWhiteAuto'}).get_text()
frameps = round(float(bs_content.find("feature", {'name': 'AcquisitionFrameRateAbs'}).get_text()), 1)

is_running = True
i = 1
count = 0

def createwidgets():
    global cameralabel
    global destPath
    global exposure
    global gain
    global white_balance

    feedlable = Label(root, bg="steelblue", fg="white", text="WEBcam Part", font=("Comic Sans MS", 20))
    feedlable.grid(row=1, column=1, padx=10, pady=10, columnspan=2)

    cameralabel = Label(root, bg="steelblue", borderwidth=3, relief="groove")
    cameralabel.grid(row=2, column=1, padx=10, pady=10, columnspan=2)

    saveEntry = Entry(root, width=55, textvariable=destPath)
    saveEntry.grid(row=3, column=1, padx=10, pady=10)

    browseBtn = Button(root, text="Browse", width=10, command=destBrowse)
    browseBtn.grid(row=3, column=2, padx=10, pady=10)

    # startCam = Button(root, text="Start Capture", command=open_camera, bg="Lightblue", font=("Comic Sans MS", 15), width=20)
    # startCam.grid(row=4, column=1, padx=10, pady=10)

    captureBtn = Button(root, text="Capture", command=capture_images, bg="Lightblue", font=("Comic Sans MS", 15), width=20)
    captureBtn.grid(row=4, column=1, padx=10, pady=10)

    #cameraBtn = Button(root, text="Stop Camera", command=close, bg="Lightblue", font=("Comic Sans MS", 15), width=20)
    #cameraBtn.grid(row=4, column=3, padx=10, pady=10)

def do_something(frame):
    save_path = destPath.get()
    os.chdir(save_path)
    lst = []
    global count
    count += 1
    frame = frame.as_numpy_ndarray()
    frame = cv2.cvtColor(frame, cv2.COLOR_BAYER_RG2RGB)
    frame = cv2.resize(frame, (1280, 720))
    cv2.imshow('Live feed', frame)
    filename = 'IMG_' +str(count) + '.jpg'
    font = cv2.FONT_HERSHEY_PLAIN
    cv2.putText(frame, str(datetime.now()), (20, 40),
                font, 2, (255, 255, 255), 2, cv2.LINE_AA)
    # cv2.imwrite(filename, frame)
    lst.append(frame)
    return len(lst)

def capture_images():
    with Vimba.get_instance() as vimba:
        with vimba.get_all_cameras()[0] as cam:
            # save_path = destPath.get()
            # os.chdir(save_path)
            while is_running:
                start = time.time()
                cam.set_pixel_format(PixelFormat.BayerRG8)
                cam.ExposureTimeAbs.set(exposure)
                cam.BalanceWhiteAuto.set(whitebalance)
                cam.Gain.set(gain)
                frame = cam.get_frame()
                result = do_something(frame)
                key = cv2.waitKey(1)
                end = time.time()
                seconds = end - start
                fps = int(result/seconds)
                print('FPS:', fps)
                if key == ord('q'):
                    break
            cv2.destroyAllWindows()

def destBrowse():
    # Presenting user with a pop-up for directory selection. initialdir argument is optional
    # Retrieving the user-input destination directory and storing it in destinationDirectory
    # Setting the initialdir argument is optional. SET IT TO YOUR DIRECTORY PATH
    destDirectory = filedialog.askdirectory()
    destPath.set(destDirectory)

    # Displaying the directory in the directory textbox
    destPath.set(destDirectory)

destPath = StringVar()

createwidgets()
root.mainloop()
nordeh commented 1 year ago

Looking for continuous grabbing we'd suggest asynchronous_grab.py with the frame observer, much more performant compared to cam.get_frame(). https://github.com/alliedvision/VimbaPython/blob/master/Examples/asynchronous_grab.py