i am trying to use the following code to create an interactive GUI which changes the color-space(HSV, RBG, grayscale etc) on the event of button click. Display an OpenCV video in tkinter using multiprocessing Being new to python i am having problems with multiprocessing and my attempts on making the GUI which would change it's color space on button clicks hangs the entire system. Any help on it's implementation will be highly appreciated. Thankyou. Below is my code:
import cv2
from PIL import Image,ImageTk
import Tkinter as tk
import numpy as np
from multiprocessing import Process , Queue
def quit_it(root,process):
root.destroy()
process.terminate()
def black_andwhite(root,process):
process.terminate
p=Process(target=capture_image, args=(5,queue, ))
p.start()
root.after(0, func=lambda: update_all(root, image_label, queue))
def update_image(image_label, queue):
frame = queue.get()
a = Image.fromarray(frame)
b = ImageTk.PhotoImage(image=a)
image_label.configure(image=b)
image_label._image_cache = b
root.update()
def update_all(root, image_label, queue):
update_image(image_label, queue)
root.after(0, func=lambda: update_all(root, image_label, queue))
def capture_image(var,queue):
vidFile = cv2.VideoCapture(0)
while True:
try:
flag, frame=vidFile.read()
if flag==0:
break
if(var==5):
frame1=cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
else:
frame1=cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
queue.put(frame1)
cv2.waitKey(20)
except:
continue
if __name__ == '__main__':
queue=Queue();
root=tk.Tk()
root.geometry("1500x1200+2+2")
image_label=tk.Label(master=root)
image_label.pack()
p=Process(target=capture_image, args=(7,queue, ))
p.start()
quit_button=tk.Button(master=root, text='Quit', command=lambda:quit_it(root,p))
quit_button.pack()
bandw_button=tk.Button(master=root, text='black_and_white',command=lambda:black_andwhite(root,p))
bandw_button.pack()
root.after(5, func=lambda: update_all(root, image_label, queue,))
root.mainloop()
p.terminate()