How to use trained yolov5 model with anvil?

What I’m trying to do:
Create an anvil web page that allow users to upload pictures to the server and then get the pictures back with labels of the detected objects
What I’ve tried and what’s not working:
I was able to train my model online using colab and do test with the test image, which saved into the detected folder in colab. However I can’t figure out how to upload the images from anvil app directly to detect.py as it a command, and how to retrieve the saved result from the detected folder. With unlink I was able to communicate with colab, but not invoke detect.py and retrieve the result.
Any help is appreciated…
Code Sample:

# this is a formatted code snippet.
# paste your code between ``` 

Clone link:
share a copy of your app

This would be a really good place to start. You’ve told everyone what you want your code to do, but you haven’t shown anyone what it is really doing.

3 Likes

I guess I should start with some fundamental questions: Can I run my trained object detection model in Anvil server? If yes, any reference/documentation/tips I can use? If not, any idea what server I can use and how to connect it with Anvil?
Thank you…

Hello @adhamfaisal

I’m certain that you can accomplish this with Anvil.

However, please show your uplink code snippets formatted nicely here on the forum. You seem to be pretty close to an answer but there are too many unknowns for other to help effectively.

Please do your best to be very clear so that people don’t have to do background research and guessing in order to help you. You should show us what you have tried when using uplink and where the documentation and related posts were unclear.

Please search the forum for related terms. Example, Jupyter, Colab, machine learning, image classifier, etc. I’m pretty sure there are examples, even official tutorials that could help.

It seems to me that you just need to know how to pass objects back and forth between Colab and Anvil using uplink so the docs may help as well.

I’m certain that the issue is quite simple but you have to do a bit more work to communicate that to us so we can help more directly. We’re pretty happy to help when we can :slightly_smiling_face:.

3 Likes

Sorry for being very brief, I have the following colab link (Google Colab) for a Yolov5 object detector, after running the code (and entering google drive API), you can upload an image to detect the objects inside it, the processed image will be saved in a folder called detect

What I am trying to do is:
A- to see if it is at all possible to avoid colab and work directly with anvil server (i.e. import the trained model file into anvil server and use it from there). if that is not possible, then;
B- I want to find a way to call “!python detect.py” command on images uploaded by anvil app to Colab, and then return the processed image (image with labels) and the list of the detected labels to anvil app.

Here is the anvil app Anvil | Login
Colab code:

!pip install anvil-uplink

import anvil.server
anvil.server.connect(“anvil API”)

import anvil.media

from keras.preprocessing import image

import numpy as np

import PIL as PIL

import tensorflow as tf 

import torch

from PIL import Image

@anvil.server.callable

def classify_image(file):

  with anvil.media.TempFile(file) as filename:

    img = image.load_img(filename)

  # Model

    model = torch.hub.load('ultralytics/yolov5', 'yolov5s', pretrained=True).autoshape()  # for PIL/cv2/np inputs and NMS

# Images

    img = Image.open(img)  # PIL image

# Inference

    results = model(img) 

  #img = img.resize((128,128),resample=PIL.Image.BICUBIC)

  #arr = image.img_to_array(img)

  #arr = np.expand_dims(arr, axis=0)

  #arr /= 255.0

  #img = tf.predict(img)

  return "test",results

I really appreciate any input/guidance

It seems that you can do that with anvil, but if possible, please upload the code of detect.py as well.

If the code need packages not in the list, you can ask the team install with paid plan:

1 Like

It is part of Yolov5.

So you need on paid plan to ask the team install yolov5 and use its

I have paid plan, but I have issues with the code it self, when I run it with colab, I get anvil.server.uplinkdisconnectederror: unlink disconnected, so I am not even sure If the code it self is correct.

Hi @adhamfaisal
One (slightly clunky) option is to separate the three steps:
-First an Anvil form publishes image file to a designated Google Drive Folder (say ‘image queue’)

  • A colab server continually ‘processes’ this image queue from Google Drive(in your case runs YOLO based semantic segmentation)
  • A dialog within the Anvil frontend form then queries the backend colab server, which returns processed results when available

I published an app called DejaVu to the community that demonstrates the last two bits albeit with MaskRCNN vs Yolo. See details at – DejaVu Instructions - Google Docs

3 Likes