Code Monkey home page Code Monkey logo

python_final_project's Introduction

Python (063PYTHL6) - Final Project

Introduction

In this presentation, i will be demonstrating a Computer Vision demo using YOLOv5 on the Microsoft COCO Dataset including close to 90 detectable objects.
The user can choose between detection on an image or a video.

Installation

Though the project will be deployed for the demo of my project, if you wish to replicate the code the following are the requirements list:

  • Python >= 3.8
  • GitCLI
  • Anaconda (optional)
  • streamlit -> pip3 install streamlit
  • git clone https://github.com/ultralytics/yolov5
  • cd yolov5
  • pip install -r requirements.txt
  • rm README.md
  • rm .gitattributes
  • mv ./* ..
  • rm -rfv yolov5

or

  • git clone https://github.com/hassan-baydoun/python_final_project.git
  • pip install -r requirements.txt

Run

Deployed:

Streamlit App

Or Locally:

Run with command streamlit run main.py Image and video examples can be found in data/images and data/videos

Code Samples

Private functions:

def _all_subdirs_of(b='.'):
    '''
        Returns all sub-directories in a specific Path
    '''
    result = []
    for d in os.listdir(b):
        bd = os.path.join(b, d)
        if os.path.isdir(bd): result.append(bd)
    return result

def _get_latest_folder():
    '''
        Returns the latest folder in a runs\detect
    '''
    return max(all_subdirs_of('runs/detect'), key=os.path.getmtime)

def _save_uploadedfile(uploadedfile):
    '''
        Saves uploaded videos to disk.
    '''
    with open(os.path.join("data/videos",uploadedfile.name),"wb") as f:
        f.write(uploadedfile.getbuffer())


def _format_func(option):
    '''
        Format function for select Key/Value implementation.
    '''
    return CHOICES[option]

Streamlit and detection call:

inferenceSource = str(st.sidebar.selectbox('Select Source to detect:', options=list(CHOICES.keys()), format_func=_format_func))

if inferenceSource == '0':
    uploaded_file = st.sidebar.file_uploader("Upload Image", type=['png','jpeg', 'jpg'])
    if uploaded_file is not None:
        is_valid = True
        with st.spinner(text='In progress'):
            st.sidebar.image(uploaded_file)
            picture = Image.open(uploaded_file)  
            picture = picture.save(f'data/images/{uploaded_file.name}') 
            opt.source = f'data/images/{uploaded_file.name}'
    else:
        is_valid = False
else:
    uploaded_file = st.sidebar.file_uploader("Upload Video", type=['mp4'])
    if uploaded_file is not None:
        is_valid = True
        with st.spinner(text='In progress'):
            st.sidebar.video(uploaded_file)
            _save_uploadedfile(uploaded_file)
            opt.source = f'data/videos/{uploaded_file.name}'
    else:
        is_valid = False

st.title('Welcome to my Final Python Project!')
st.subheader('Presented to: Prof. Georges Salloum by Hassan BAYDOUN (192604)')

inferenceButton = st.empty()

if is_valid:
    if inferenceButton.button('Launch the Detection!'):
        with st_stdout("info"):
            detect(opt)
        if inferenceSource != '0':
            st.warning('Video playback not available on deployed version due to resource restrictions. ')
            with st.spinner(text='Preparing Video'):
                for vid in os.listdir(_get_latest_folder()):
                    st.video(f'{_get_latest_folder()}/{vid}')
                st.balloons()
        else:
            with st.spinner(text='Preparing Images'):
                for img in os.listdir(_get_latest_folder()):
                    st.image(f'{_get_latest_folder()}/{img}')
                st.balloons()

Hassan Baydoun - 2021 โˆž

python_final_project's People

Contributors

hassan-baydoun avatar omarwafaie avatar

Forkers

omarwafaie

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.