Object Detection using PySimpleGui

Posted by


PySimpleGUI is a simple yet powerful Python library that allows you to easily create graphical user interfaces (GUI) for your Python programs. In this tutorial, we will demonstrate how you can use PySimpleGUI to create a GUI application that can detect objects in images using the OpenCV computer vision library.

To get started, make sure you have PySimpleGUI and OpenCV installed in your Python environment. You can install them using the following commands:

pip install PySimpleGUI
pip install opencv-python

Next, create a new Python script and import the necessary libraries:

import PySimpleGUI as sg
import cv2
import numpy as np

Next, define the layout of your GUI application. We will create a simple layout with a FileBrowse button to select an image file, a Detect Objects button to start the object detection process, and an Image element to display the selected image:

layout = [
    [sg.Text('Select an image file:'), sg.InputText(key='file_path'), sg.FileBrowse()],
    [sg.Button('Detect Objects')],
    [sg.Image(key='image')]
]

window = sg.Window('Object Detection', layout)

Now, let’s define a function that will perform object detection using the OpenCV library. We will use the YOLOv3 object detection model, which is pre-trained on the COCO dataset and provides good performance for detecting a wide range of objects.

def detect_objects(image_path):
    net = cv2.dnn.readNet('yolov3.weights', 'yolov3.cfg')
    classes = []

    with open('coco.names', 'r') as f:
        classes = [line.strip() for line in f.readlines()]

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

    img = cv2.imread(image_path)
    img = cv2.resize(img, (416, 416))
    blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)

    net.setInput(blob)
    outs = net.forward(output_layers)

    for out in outs:
        for detection in out:
            scores = detection[5:]
            class_id = np.argmax(scores)
            confidence = scores[class_id]

            if confidence > 0.5:
                center_x = int(detection[0] * img.shape[1])
                center_y = int(detection[1] * img.shape[0])
                w = int(detection[2] * img.shape[1])
                h = int(detection[3] * img.shape[0])

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

                cv2.rectangle(img, (x, y), (x + w, y + h), (0, 255, 0), 2)
                cv2.putText(img, classes[class_id], (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)

    cv2.imwrite('output.jpg', img)
    return 'output.jpg'

Now, let’s update our GUI application to call the detect_objects function when the Detect Objects button is clicked and display the detected objects on the image:

while True:
    event, values = window.read()

    if event == sg.WIN_CLOSED:
        break

    if event == 'Detect Objects':
        image_path = values['file_path']
        output_image_path = detect_objects(image_path)

        window['image'].update(output_image_path)

window.close()

In this tutorial, we have shown you how to create a GUI application using PySimpleGUI for detecting objects in images using the OpenCV library. You can further customize the layout and functionality of the application to suit your specific requirements. Give it a try and start creating your own object detection applications with PySimpleGUI!

0 0 votes
Article Rating

Leave a Reply

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x