Categories:

Social Share

Sample Object Detection model deployment for reference

The following files can be used as a reference for  Object Detection Inference - Hardhat Detection and Google Docs - Cluzter Model Guidelines


Guidelines

Inference.zip Folder structure:

requirements.txt

Trained Model file

inference.py

Other files and folders used


Inference.py file format:

Import Statements

Onetime executable operations

{Ex: Loading the Model, label encoding  etc.}

def predict(Input arguments as per the use-case)

{

Data Preprocessing

Inference

Return output based on the use-case

}

*Do not change the naming convention for the entities marked inblue
  • inference.py and requirements.txt are mandatory files
  • inference.py should contain a predict function which takes 2 arguments.
    First arg(string): img path to read the input image from
    Second arg(string): Directory to save the output image. For example if the 2nd argument is variable output_dir, the image can be saved as output_dir+’image.jpg’
  • The return value of the predict function should be the file location where output image is saved


Sample inference.py
import os, sys import cv2 import numpy as np import tensorflow as tf #load the model and any other custom defined functions/variables detection_model = tf.saved_model.load('saved_model') def predict(img, output_directory): #mandatory function: First arg: img path to read from, Second arg: Dir to save the output image img = cv2.imread(img) #code for predictions #........ #......... output_file = output_directory + "image.jpg" cv2.imwrite(output_file,image_np_with_detections) return output_file​

Sample requirements.txt
tensorflow==2.4.1 numpy​ opencv-python-headless​