Object Detection in PHP with 15 lines of code

Object Detection in PHP

Object Detection is a powerful deep learning application used in many industries in this modern time. There are lots of processes and algorithms run to detect an object accurately. So surely, it needs to write lots of codes to develop an object detecting system in real life.

But wait…

In this article, I will show you how you can do this in PHP just with 15 lines of code.

Yes, what you have just listened to is right.

At this time, PHP is still not a suitable programming language for machine learning. Also, PHP doesn’t have the speed to perform the task. Machine learning related work needs programming languages that work faster. For example, Python is a suitable programming language for machine learning and deep learning because of its speed and simplicity.

Now you may think, then how object detection is possible in PHP.

Well, here I am going to introduce you to a Machine Learning API provider that provides web API for object detection as well as many other machine learning purposes. Using their API, object detection is possible in PHP just within a few lines of code.

The API provider that we are going to use is NanoNets. We are going to use their service to do our object detecting task.

Before you start writing code in PHP, you have to create an account on NanoNets. There you will get your API. You also have to create an object detection model on NanoNets. Creating an object detection model is quite an easy task. All you need is just to add some category names and upload pictures.

After your pictures uploaded, you have to Annotate objects from images using the drag and drop method that NanoNets provide. In the end, NanoNets will train your model just with your one click.

Object Detection code in PHP using NanoNets API

Now let’s see our PHP code to detect our object from an image.

The complete code that returns the result from NanoNets is given below:

<?php
$imgUrl = "https://site.com/path-to-image.jpg";
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, "urls=".$imgUrl."");
curl_setopt($curl, CURLOPT_HTTPHEADER, array(
  "content-type: application/x-www-form-urlencoded",
        'Authorization: Basic '. base64_encode("NANONETS_API_KEY:")
));
curl_setopt($curl, CURLOPT_URL, 'https://app.nanonets.com/api/v2/ObjectDetection/Model/{MODEL_ID}/LabelUrls/');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
echo $result;
?>

That’s it…

In the above code, you only have to replace the model ID and Nanonets API Key with your own and replace the image URL with a proper image URL path. The image URL should be accessible publicly on the internet.

If everything’s goes right, it will return a JSON response that will look like you can see below:

{
  "message": "Success",
  "result": [
    {
      "message": "Success",
      "input": "https://site.com/path-to-image.jpg",
      "prediction": [
        {
          "label": "People",
          "xmin": 716,
          "ymin": 353,
          "xmax": 1023,
          "ymax": 791,
          "score": 0.9691711
        }
      ]
    }
  ]
}

The above JSON response is when only one object found in the picture. If there is more than one object available in the picture, it can detect all of the objects.

Below is the JSON response with 4 objects found in an image:

{
  "message": "Success",
  "result": [
    {
      "message": "Success",
      "input": "https://site.com/path-to-image.jpg",
      "prediction": [
        {
          "label": "People",
          "xmin": 212,
          "ymin": 20,
          "xmax": 399,
          "ymax": 630,
          "score": 0.99909675
        },
        {
          "label": "People",
          "xmin": 402,
          "ymin": 42,
          "xmax": 614,
          "ymax": 630,
          "score": 0.9973943
        },
        {
          "label": "People",
          "xmin": 637,
          "ymin": 52,
          "xmax": 798,
          "ymax": 630,
          "score": 0.99497247
        },
        {
          "label": "People",
          "xmin": 775,
          "ymin": 33,
          "xmax": 989,
          "ymax": 630,
          "score": 0.9945598
        }
      ]
    }
  ]
}

In the JSON response, we can see each object details. It shows the object label names with four points position in the X-axis and Y-axis and accuracy score. Using the axis position, we can draw a rectangle around each object.

I am not going to discuss the rectangle drawing process in this article. If you want me to draw rectangle marks around the detected objects, then comment below. I will write a new post which will show how to draw rectangles using the axis pont values return by the NanoNets.

Also, read:

 

NanoNets has all the algorithm to train your model from your chosen annotate. SO you don’t think about it at all. NanoNets has done a great job. For their service, object detection code in PHP within minutes is possible. Machine learning is now an easy task for their service.

I hope, you become excited now to go through NanoNets machine learning API.

One response to “Object Detection in PHP with 15 lines of code”

  1. Ernesto says:

    Only 15 lines to communicate with a webserver sending an url?
    Wonderful!

Leave a Reply

Your email address will not be published. Required fields are marked *