r/pythonhelp • • Jan 04 '23

How to display cv2 (yolov3) output video stream inside a tkinter window ??

Hello! I hope you are well.

I am working on a object detection desktop application and I am trying to stream the output video (after object detection ie: with boxes and labels) inside a tkinter window. I have tried to do it with a canvas and update the canvas image after every n milliseconds and even use a label image with updating but it doesn't seem to work. It has been a couple days that I am trying with it. The furthest I was able to accomplish was showing the first frame only. I think that I have a problem with the loop. I don't know which one to run first, the mainloop or the updating the canvas loop. Logically, I should run the mainloop first and the updating loop should keep running in the background nothing after the mainloop runs until the tkinter window is closed as I saw in a course (still a tkinter beginner)

Here is the code that enabled me to show the first frame alone (I have hiddent my ip webcam https adress):

import cv2
import numpy as np
from tkinter import *
import PIL
from PIL import ImageTk


# load the model
net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
# classes list
classes = []
with open('coco.names', 'r') as f:
    classes = f.read().splitlines()


window = Tk()
window.geometry("1000x800")
window.title('opencv inside tkinter !!')

cap = cv2.VideoCapture(0)
adress = "https://ip_webcam_adress:port/video"
cap.open(adress)

# tuple of lines, cols and canals
_, img = cap.read()
height, width, no_channels = img.shape
print('width: ' + str(width) + ' height: ' + str(height))
# Cette fonction effectue :Soustraction moyenne  Mise à l'échelle  Permutation de canaux BR
blob = cv2.dnn.blobFromImage(img, 1 / 255, (416, 416), (0, 0, 0), swapRB=True, crop=False)


# nn input
net.setInput(blob)

# get output layers names
output_layers_names = net.getUnconnectedOutLayersNames()
layerOutputs = net.forward(output_layers_names)

boxes = []
confidences = []
class_ids = []

for output in layerOutputs:
    for detection in output:
        scores = detection[5:]
        class_id = np.argmax(scores)
        confidence = scores[class_id]
        if confidence > 0.5:
            # center to all cadre
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)

            x = int(center_x - w / 2)
            y = int(center_y - h / 2)

            boxes.append([x, y, w, h])
            confidences.append((float(confidence)))
            class_ids.append(class_id)

# print(len(boxes))
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
print("HI  ", indexes)
# add text to the image
font = cv2.FONT_HERSHEY_PLAIN
colors = np.random.uniform(0, 255, size=(len(boxes), 3))
# for i in indexes.flatten()
for i in indexes:
    x, y, w, h = boxes[i]
    label = str(classes[class_ids[i]])
    confidence = str(round(confidences[i], 2))
    color = colors[i]
    print('label is: ' + label)
    cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
    cv2.putText(img, label + " " + confidence, (x, y + 20), font, 2, (255, 255, 255), 2)

    # show
new_size = (int(600), int(337.5))
img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)
new_img = ImageTk.PhotoImage(image=PIL.Image.fromarray(img))

canvas = Canvas(window, width=600, height=337)
canvas.create_image(0, 0, anchor='nw', image=new_img)
canvas.pack()

window.mainloop()

Thank you in advance for your help. This project is a huge thing for me at school and I would really appreciate your guidance :)

0 Upvotes

3 comments sorted by

1

u/[deleted] Feb 16 '23

[removed] — view removed comment

1

u/heyits_ashraf Feb 17 '23

Yes, I was able to do it! I will write here the snippets of the code that matter to you. For my code, I had 2 classes: Gui and Brain. Gui is the gui and Brain has the yolo and its use.

So, for the gui, I defined the following:

self.canvas = Canvas(strm_frame, width=700, height=393, bg='black')
self.canvas.pack()
self.brain = Brain(self)

The Brain class takes in its constructor an arg Tk windows or wtv u want.

def __init__(self, window):
    self.cap.open(self.address)
    self.window = window
    self.window.canvas.configure(bg='black')
    self.start()

For this method start(), it contains a lot of things related to cv2 stream:

def start(self):    
    _, img = self.cap.read()
    ...
    new_size = (int(700), int(393))
    img = cv2.resize(img, new_size, interpolation=cv2.INTER_LINEAR)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    new_img = ImageTk.PhotoImage(image=PIL.Image.fromarray(img))

    
    self.window.canvas.delete("all")
    self.window.canvas.img = new_img
    self.window.canvas.create_image(0, 0, anchor=NW, image=self.window.canvas.img)
    self.window.canvas.after(10, self.start)

I didn't include much of the stream related code assuming you can already get it. For the values 700 and 393, I just wanted to keep the 16:9 ratio and also, that's what worked for the gui of my app. The second part with self.window.canvas is where the 'update' of the image happens. I set the interval to 10 ms so that the method start would execute every 10 ms. You can try with other interval values.