Skip to main content
  1. My Blog Posts and Stories/

Object detection for N00bs

··701 words·4 mins

Surprise surprise #

Didn’t know that I was doing Machine learning did ya ;D.

So I am taking a Do Your Own Module(DYOM) and it was about machine learning. (Because its pass-fail)

There is an assignment for the module that seemed really cool which asks us to deploy a web service where the user can upload their images and the bounding boxes get labelled on the images.

Assignment deliverables #

The TLDR for the assignment:

  1. Use any pre-trained model (Or train your own if you want to)
  2. Make it into a web app or a tool

Seems fairly simple right? I hope so?

The example slides provided us with 2 pre-trained models:

  1. Inception Resnet v2
  2. Mobile Net

Inception resnet was suppose to be the slower but more accurate model and mobile net is the faster but less accurate model. After some quick googling that was the TLDR that I got

Image Recognition AI trends
AI Trends for image recognition

Details Details #

For a deeper dive into the different YOLO versions, you can take a look at this Yolo v4 vs Yolo v5 Well well well look at that. As a gamer, I loved those sweet sweet FPS numbers. Time to bring on that YOLO 5

YOLO v5 is the one that I used. It had an amazing example code that I have come to love. I have extracted it and placed it below

Image Detection #

import torch

# Model
model = torch.hub.load('ultralytics/yolov5', 'yolov5s')

# Image
img = ''

# Inference
results = model(img)
results.print()  # or .show(), .save()

At for more news #

I love that the code is amazingly short for image detection. Best sh*t ever! God bless YOLO v5 to save my hw. So I added that few lines of code into my flask application and it is done right??

Not exactly…….

The truth is I have no idea how the user is supposed to upload the file onto flask…

So the next challenge is to find out how to upload files onto the server using Flask. According to the flask documentation, I am suppose to do this

import os
from flask import Flask, flash, request, redirect, url_for
from werkzeug.utils import secure_filename

UPLOAD_FOLDER = '/path/to/the/uploads'
ALLOWED_EXTENSIONS = {'txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'}

app = Flask(__name__)

def allowed_file(filename):
    return '.' in filename and \
           filename.rsplit('.', 1)[1].lower() in ALLOWED_EXTENSIONS

@app.route('/', methods=['GET', 'POST'])
def upload_file():
    if request.method == 'POST':
        # check if the post request has the file part
        if 'file' not in request.files:
            flash('No file part')
            return redirect(request.url)
        file = request.files['file']
        # if user does not select file, browser also
        # submit an empty part without filename
        if file.filename == '':
            flash('No selected file')
            return redirect(request.url)
        if file and allowed_file(file.filename):
            filename = secure_filename(file.filename)
            return redirect(
    return '''
    <!doctype html>
    <title>Upload new File</title>
    <h1>Upload new File</h1>
    <form method=post enctype=multipart/form-data>
      <input type=file name=file>
      <input type=submit value=Upload>

It should be just copy paste and it should be done right?? Welp if only life was that simple…….. Life has ways of coming up with surprises. The issue is it takes up space in the computer, if there are too many users, the server will take up too much space. Then what if there are 2 files with the same name?? There could be race conditions. Oh man there are so many issues with this implementation.

Fixing the issue. #

Welp you could guess where all of this is going, so I’ve decided to implement a all in memory version of this function. Should be EZPZ after all that suffering in operating systems.

Time to alter my code. Apparently, by using BytesIO I can simulate a file object but the actual file is in RAM.

By replacing all the downloading by saving it in Bytesio everything will be in memory and all those issues will be avoided XD. The downside is that if too many people use it at once and there is not enough ram, then the server will crash x.x. Welp knowing my webpage I don’t think I have to worry about that yet.

You can visit the website here (Not up anymore).

For more information you can view the source code below Github repo