Hi,
I want to implement the object detection in a Tkinter GUI on my Raspberry. For that I wrote following code. The GUI alone works just fine, but I have a problem to implement the code for the classification/detection. In the method “stream()” I tried to get back the “img” (with “yield”) from the for-loop for every iteration and display it on my GUI with “display()”, before a new Iteration starts… Without the yield-command the for-loop/classification starts, but of course doesn’t stop. With the yield-command, the hole function “stream()” doesn’t run at all.
Can anyone tell me what I do wrong or can help how I can solve that problem?
Thanks in advance!
Jonas
> #!/usr/bin/env python
>
> import cv2
> import os
> import tkinter as tk
> from tkinter import ttk
> from PIL import Image
> from PIL import ImageTk
> from edge_impulse_linux.image import ImageImpulseRunner
>
> class Application(tk.Frame):
> def __init__(self, master=None):
> super().__init__(master)
> self.master = master
>
> # Set up GUI window
> master.title("traffic sign detection")
> #master.attributes("-fullscreen", True)
> master.config(background="grey")
> self.subframe()
>
> def get_modelfile(self):
> model = "/home/pi/sign_detection/modelfile.eim"
> dir_path = os.path.dirname(os.path.realpath(__file__))
> self.modelfile = os.path.join(dir_path, model)
> print('MODEL: ' + self.modelfile)
>
> # Load runner
> self.load_runner()
>
> def subframe(self):
> #Set up image frame
> self.imageFrame = tk.Frame(self.master, width=480, height=480)
> self.imageFrame.grid(row=0, column=0, padx=10, pady=2)
> self.create_widgets()
>
> def create_widgets(self):
> # Exit-Button widget
> self.exit = tk.Button(self.master, text="EXIT", bg="red", padx=50, pady=50, command=self.master.destroy)
> self.exit.grid(row=0, column=1, padx=10, pady=2)
>
> # Display widget
> self.stream_label = tk.Label(self.imageFrame, text="traffic sign detection")
> self.stream_label.grid(row=0, column=0)
>
> # Load model
> self.get_modelfile()
>
> def load_runner(self):
> with ImageImpulseRunner(self.modelfile) as runner:
> self.runner = runner
> model_info = runner.init()
> print('Loaded runner for "' + model_info['project']['owner'] + ' / ' + model_info['project']['name'] + '"')
> labels = model_info['model_parameters']['labels']
> self.display()
>
> def stream(self):
> for res, img in self.runner.classifier(0):
> if "bounding_boxes" in res["result"].keys():
> print('Found %d bounding boxes (%d ms.)' % (len(res["result"]["bounding_boxes"]), res['timing']['dsp'] + res['timing']['classification']))
> for bb in res["result"]["bounding_boxes"]:
> val_bb = int(bb['value']*100)
> label_bb = bb['label']
> print('\t%s (%.2f): x=%d y=%d w=%d h=%d' % (bb['label'], bb['value'], bb['x'], bb['y'], bb['width'], bb['height']))
> img = cv2.rectangle(img, (bb['x'], bb['y']), (bb['x'] + bb['width'], bb['y'] + bb['height']), (255, 0, 0), 1)
> cv2.putText(img, '%s %s%%' % (label_bb, str(val_bb)), (bb['x'], bb['y']-10), cv2.FONT_HERSHEY_SIMPLEX, 0.6, (255,0,0), 2)
> img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
> yield img
>
> def display(self):
> self.stream()
> classifier = self.stream()
> img = next(classifier)
> self.stream_resized = Image.fromarray(img).resize((320, 320))
> self.streamtk= ImageTk.PhotoImage(image=self.stream_resized)
> self.stream_label.streamtk = self.streamtk
> self.stream_label.configure(image=self.streamtk)
> self.master.after(10, self.display)
>
> def main():
> # Opens main window
> window = tk.Tk()
> GUI = Application(master=window)
> window.mainloop()
>
> if __name__ == "__main__":
> main()