0

I am trying to make a object detection program which detects the object infront of the user and outputs the object using tts. What i'm trying to do is display the processed frame in a tkinter label. I have tried using PIL but i always get src_empty error when updating the frames.

def run_computer_vision():
    net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg")
    net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
    net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA)

    classes = []
    with open("yolov3.txt", "r") as f:
        classes = [line.strip() for line in f.readlines()]

    # Reading layers
    layer_names = net.getLayerNames()
    output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]

    cap = cv2.VideoCapture(0)
    # loading the image
    while True:
        ret, frame = cap.read()
        if not ret:
            break

        height, width, channels = frame.shape
        blob = cv2.dnn.blobFromImage(
            frame, 0.00392, (416, 416), (0, 0, 0), True, False)
        net.setInput(blob)
        outs = net.forward(output_layers)

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

        for out in outs:
            for detection in out:
                scores = detection[5:]
                class_id = np.argmax(scores)
                confidence = scores[class_id]
                if confidence > 0:
                    cx = int(detection[0] * width)
                    cy = int(detection[1] * height)
                    w = int(detection[2] * width)
                    h = int(detection[3] * height)
                    x = int(cx - w / 2)
                    y = int(cy - h / 2)
                    boxes.append([x, y, w, h])
                    confidences.append(float(confidence))
                    class_ids.append(class_id)

        # apply non-max suppression
        indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)

        # loop over the boxes
        for i in range(len(boxes)):
            if i in indexes:
                x, y, w, h = boxes[i]
                label = classes[class_ids[i]]
                cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)
                cv2.putText(frame, label, (x, y + 30), cv2.FONT_HERSHEY_SIMPLEX, 1, (255, 255, 255), 2)
                direction = get_direction(x, y, w, h, width, height)
                engine.say(label + "On your " + direction)
                engine.runAndWait()
                time.sleep(1)

        cv2.imshow("Detection", frame)
        # Check for key presses
        key = cv2.waitKey(1)
        if key == ord('q'):
            break
        elif key == ord('s'):
            cv2.imwrite("image.jpg", frame)

    # Release the camera and close the window
    cap.release()
    cv2.destroyAllWindows()
    Camera()

I would like to display the frames of cv2.imshow("Detection",frame) in a tkinter label

Raze Quit
  • 1
  • 1
  • Did you search SO? There are few questions on converting opencv image to tkinter image, like [this](https://stackoverflow.com/questions/28670461/read-an-image-with-opencv-and-display-it-with-tkinter) or [this](https://stackoverflow.com/questions/70858278/opencv-pythongui-displaying-opencv-image-into-tkinter). – acw1668 Mar 14 '23 at 05:09
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Blue Robin Mar 14 '23 at 16:17
  • It's not a good idea to use Tkinter and OpenCV High-level GUI API at the same time. Do a preliminary research on GUI and the event loop. – relent95 Mar 20 '23 at 01:32

0 Answers0