Giter VIP home page Giter VIP logo

Comments (5)

Hyuto avatar Hyuto commented on September 28, 2024 1

Sorry but I'm not going to create new repo for this but you could use this project folder as I said before. Mostly YOLOv5 and YOLOv7 work the same way when we deploy it on onnxruntime, but the problem is YOLOv5 exported onnx model doesn't have NMS layer on it so we have to implement this manually (the performance is really bad compared to NMS on the model, but it works 😆) .

  1. Add YOLOv5 onnx model to the project. Move it to src folder

  2. Change model and model configuration in src/index.js

    ...
    // model configs
    const modelInfo = {
        name: "yolov5n.onnx",
        inputShape: [1, 3, 640, 640],
    };
    const confidenceThreshold = 0.25;
    const classThreshold = 0.25;
    const iouThreshold = 0.45; // overlap threshold
    ...

    Note: I use yolov5 lightest model yolov5n here, as for the thresholds you can tweak it yourself

  3. Add NMS algorithm on src/index.js

    ...
    const NMS = (boxes, overlapThresh) => {
        // source : https://github.com/Hyuto/yolov5-onnxruntime-web/blob/master/src/utils/nms.js
        if (boxes.length === 0) {
            return [];
        }
    
        const pick = [];
    
        boxes.sort((b1, b2) => {
            return b1.confidence - b2.confidence;
        });
    
        while (boxes.length > 0) {
            let last = boxes[boxes.length - 1];
            pick.push(last);
            let suppress = [last];
    
            for (let i = 0; i < boxes.length - 1; i++) {
                const box = boxes[i];
                const xx1 = Math.max(box.bounding[0], last.bounding[0]);
                const yy1 = Math.max(box.bounding[1], last.bounding[1]);
                const xx2 = Math.min(box.bounding[0] + box.bounding[2], last.bounding[0] + last.bounding[2]);
                const yy2 = Math.min(box.bounding[1] + box.bounding[3], last.bounding[1] + last.bounding[3]);
                const w = Math.max(0, xx2 - xx1 + 1);
                const h = Math.max(0, yy2 - yy1 + 1);
                const overlap = (w * h) / ((box.bounding[2] + 1) * (box.bounding[3] + 1));
    
                if (overlap > overlapThresh) {
                    suppress.push(boxes[i]);
                }
            }
    
            boxes = boxes.filter((box) => {
                return !suppress.find((supp) => {
                    return supp === box;
                });
            });
        }
        return pick;
    };
  4. Modifying detect function on src/index.js

    ...
    const detect = async (frame) => {
      const input = new Float32Array(frame.buffer);
      const tensor = new ort.Tensor("float32", input, modelInfo.inputShape); // to ort.Tensor
      const { output0 } = await session.run({ images: tensor }); // run session and get output layer
      const boxes = [];
    
      // looping through output
      for (let r = 0; r < output0.data.length; r += output0.dims[2]) {
        const data = output0.data.slice(r, r + output0.dims[2]); // get rows
        const scores = data.slice(5); // classes probability scores
        const confidence = data[4]; // detection confidence
        const classId = scores.indexOf(Math.max(...scores)); // class id of maximum probability scores
        const maxClassProb = scores[classId]; // maximum probability scores
    
        // filtering by thresholds
        if (confidence >= confidenceThreshold && maxClassProb >= classThreshold) {
          const [x, y, w, h] = data.slice(0, 4);
          boxes.push({
            classId: classId,
            probability: maxClassProb,
            confidence: confidence,
            bounding: [x - 0.5 * w, y - 0.5 * h, w, h],
          });
        }
      }
    
      // filtering boxes using Non Maximum Suppression algorithm
      const selectedBoxes = NMS(boxes, iouThreshold);
      return selectedBoxes;
    };
    ...
  5. And run the project like always yarn start

from yolov7-node.

Hyuto avatar Hyuto commented on September 28, 2024

With additional configuration on preprocessing model output, I think it's possible to serve yolov5 model.

from yolov7-node.

winktool avatar winktool commented on September 28, 2024

I am new to this field and look forward to your release of the new version about yolov5.

from yolov7-node.

winktool avatar winktool commented on September 28, 2024

Thank you very much!

from yolov7-node.

Hyuto avatar Hyuto commented on September 28, 2024

Since there's no problem occur, I'm closing this issue.

from yolov7-node.

Related Issues (5)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.