Streamlit with opencv and yolo

Hi there. i am making a streamlit app in which user will upload an image and yolo object detection algorithm will detect objects in that uploaded image but i am unable to do it.

Can anyone tell me how to do this ?
The code is given below.

import streamlit as st
import cv2
from PIL import Image,ImageEnhance
import numpy as np
import pandas as pd
import glob
import random
import os


@st.cache(persist=True)
def load_image(img):
    im = Image.open(img)
    return im


@st.cache(persist=True)
def yolo_objectdetection():

    # Load Yolo
    net = cv2.dnn.readNet("yolov3_training_last.weights", "yolov3_testing.cfg")

    # Name custom object
    classes = ["Land"]

    # Images path
    global images_path
    images_path = glob.glob(r"...\uploads")


    layer_names = net.getLayerNames()
    output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
    colors = np.random.uniform(0, 255, size=(len(classes), 3))

    # Insert here the path of your images
    random.shuffle(images_path)
    # loop through all the images
    for img_path in images_path:
        # Loading image
        img = cv2.imread(img_path)
        img = cv2.resize(img, None, fx=3.5, fy=3.5)
        height, width, channels = img.shape

        # Detecting objects
        blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)

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

        # Showing informations on the screen
        class_ids = []
        confidences = []
        boxes = []
        for out in outs:
            for detection in out:
                scores = detection[5:]
                class_id = np.argmax(scores)
                confidence = scores[class_id]
                if confidence > 0.3:
                    # Object detected
                    print(class_id)
                    center_x = int(detection[0] * width)
                    center_y = int(detection[1] * height)
                    w = int(detection[2] * width)
                    h = int(detection[3] * height)

                    # Rectangle coordinates
                    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)

        indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
        print(indexes)
        font = cv2.FONT_HERSHEY_PLAIN
        for i in range(len(boxes)):
            if i in indexes:
                x, y, w, h = boxes[i]
                label = str(classes[class_ids[i]])
                color = colors[class_ids[i]]
                cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
                cv2.putText(img, label, (x, y + 30), font, 3, color, 2)

        cv2.imshow('Image',img)
        key = cv2.waitKey(0)

    cv2.destroyAllWindows()


def main():
    """
    Land Detection App
    """
    st.title('Land Detection App')
    st.text('Build with Streamlit,Yolov3 and OpenCV')

    menu = ['Detection','About']
    choice = st.sidebar.selectbox('Menu',menu)

    if choice == 'Detection':
        st.subheader(' Land Detection')
        image_file = st.file_uploader("Upload Image",type=['jpg', 'png', 'jpeg'])

        if image_file is not None:
            our_image = Image.open(image_file)
            st.text('Original Image')
            # st.write(type(our_image))
            st.image(our_image)

        enhance_type = st.sidebar.radio('Enhance Type', ['Original', 'Gray-Scale', 'Contrast', 'Brightness', 'Blurring'])

        if enhance_type == 'Gray-Scale':
            new_img = np.array(our_image.convert('RGB'))
            img = cv2.cvtColor(new_img, 1)
            gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
            # st.write(new_img)
            st.image(gray)

        if enhance_type == 'Contrast':
            c_rate = st.sidebar.slider('Contrast', 0.5, 3.5)
            enhancer = ImageEnhance.Contrast(our_image)
            img_output = enhancer.enhance(c_rate)
            st.image(img_output)

        if enhance_type == 'Brightness':
            c_rate = st.sidebar.slider('Brightness', 0.5, 3.5)
            enhancer = ImageEnhance.Brightness(our_image)
            img_output = enhancer.enhance(c_rate)
            st.image(img_output)

        if enhance_type == 'Blurring':
            new_img = np.array(our_image.convert('RGB'))
            blur_rate = st.sidebar.slider('Blurring', 0.5, 3.5)
            img = cv2.cvtColor(new_img, 1)
            blur_img = cv2.GaussianBlur(img, (11, 11), blur_rate)
            st.image(blur_img)
        else:
            pass

        # Land Detection
        task = ['Land']
        feature_choice = st.sidebar.selectbox("Find Features", task)
        if st.button("Process"):
            if feature_choice == 'Land':
                result_img = yolo_objectdetection()
                result_img = []
                st.image(result_img)

                st.success("Found {} Land".format(len(result_img)))

            else:
                st.markdown('## Land not Detected')
                st.markdown('## Kindly upload correct image ')





    elif choice == 'About':
        st.subheader('About')
        st.info('We are an AI company which provides great blueprints and provide value to our users.')



if __name__ == '__main__':
    main()

Hi @rehan_Siddique, welcome to the Streamlit community!

We have an example similar to yours on GitHub, perhaps you can find the bits you need there:

hi, i checked that github but it did not worked for me.
can you kindly help me with me my code.

hi guys anyone can tell how to make multi frame live streaming using streamlit. and how to customize the frame size?