Pedro MachadoIvica MaticTim Fernandez-Hart
Published © GPL3+

Fruit Detection Using MPSoCs

Fruit detection at the edge using a ZCU104.

AdvancedFull instructions providedOver 1 day2,195
Fruit Detection Using MPSoCs

Things used in this project

Story

Read more

Code

Inference Code - Video

C/C++
Used for running inference on ZCU-104
#include <glog/logging.h>
#include <iostream>
#include <memory>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <xilinx/ai/demo.hpp>
#include <xilinx/ai/yolov3.hpp>
#include <xilinx/ai/nnpp/yolov3.hpp>
#include "opencv2/opencv.hpp"
#include <memory>
#include <opencv2/core.hpp>
#include <vector>
#include <xilinx/ai/nnpp/yolov3.hpp>
#include <chrono>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
    const string classes[2] = { "Orange, Apple" };
    auto yolo = xilinx::ai::YOLOv3::create("fruit-detection-network", false);
    VideoCapture cap;
    if (!cap.open(argv[1]))
        return 0;
    while (1) {
        Mat img;
        cap >> img;
        if (img.empty())
            return 0;
        auto results = yolo->run(img);
        for (auto& box : results.bboxes) {
            int label = box.label;
            float xmin = box.x * img.cols + 1;
            float ymin = box.y * img.rows + 1;
            float xmax = xmin + box.width * img.cols;
            float ymax = ymin + box.height * img.rows;
            if (xmin < 0.)
                xmin = 1.;
            if (ymin < 0.)
                ymin = 1.;
            if (xmax > img.cols)
                xmax = img.cols;
            if (ymax > img.rows)
                ymax = img.rows;
            float confidence = box.score;
            if (label > -1) {
                rectangle(img, Point(xmin, ymin), Point(xmax, ymax),
                    Scalar(200, 255, 255), 1, 1, 0);
                putText(img, classes[label], Point(xmin, ymin - 10),
                    cv::FONT_HERSHEY_DUPLEX, 1.0, CV_RGB(118, 185, 0), 2);
            }
        }
        imshow("frame", img);
        waitKey(1);
        return 0;
    }
}

Inference Code - Images

C/C++
#include <glog/logging.h>
#include <iostream>
#include <memory>
#include <opencv2/core.hpp>
#include <opencv2/highgui.hpp>
#include <opencv2/imgproc.hpp>
#include <xilinx/ai/demo.hpp>
#include <xilinx/ai/yolov3.hpp>
#include <xilinx/ai/nnpp/yolov3.hpp>
#include "opencv2/opencv.hpp"
#include <memory>
#include <opencv2/core.hpp>
#include <vector>
#include <xilinx/ai/nnpp/yolov3.hpp>
#include <chrono>
using namespace std;
using namespace cv;
int main(int argc, char** argv)
{
    const string classes[2] = { "Orange", "Apple" };
    auto yolo = xilinx::ai::YOLOv3::create("fruit");
    Mat img;
    img = imread(argv[1], IMREAD_COLOR);
    auto t1 = std::chrono::high_resolution_clock::now();
    auto results = yolo->run(img);
    for (auto& box : results.bboxes) {
        int label = box.label;
        float xmin = box.x * img.cols;
        float ymin = box.y * img.rows;
        float xmax = xmin + box.width * img.cols;
        float ymax = ymin + box.height * img.rows;
        if (xmin < 0.)
            xmin = 1.;
        if (ymin < 0.)
            ymin = 1.;
        if (xmax > img.cols)
            xmax = img.cols;
        if (ymax > img.rows)
            ymax = img.rows;
        float confidence = box.score;
        if (label > -1) {
            if (label == 0) {
                rectangle(img, Point(xmin, ymin), Point(xmax, ymax),
                    Scalar(200, 0, 0), 1, 1, 0);
                putText(img, classes[label], Point(xmin, ymin - 10),
                    cv::FONT_HERSHEY_DUPLEX, 1.0, CV_RGB(200, 0, 0), 2);
            }
            else {
                rectangle(img, Point(xmin, ymin), Point(xmax, ymax),
                    Scalar(0, 200, 0), 1, 1, 0);
                putText(img, classes[label], Point(xmin, ymin - 10),
                    cv::FONT_HERSHEY_DUPLEX, 1.0, CV_RGB(0, 200, 0), 2);
            }
        }
    }
    auto t2 = std::chrono::high_resolution_clock::now();
    if (img.empty())
        return 1;
    String name = argv[1];
    name += "_detected.jpg";
    imwrite(name, img);
    auto duration = std::chrono::duration_cast<std::chrono::microseconds>(t2 - t1).count();
    cout << 1000000 / duration << " fps" << endl;
    return 0;
}

Credits

Pedro Machado

Pedro Machado

1 project • 4 followers
Pedro’s expertise includes FPGA design, computer vision, bio-inspired computing, robotics and computational intelligence.
Ivica Matic

Ivica Matic

0 projects • 2 followers
Tim Fernandez-Hart

Tim Fernandez-Hart

2 projects • 4 followers

Comments