Code Monkey home page Code Monkey logo

yolo-mask-detection's Introduction

YOLO Mask Detection

Fast scene mask detection for real world images based on the darknet framework and it's YOLOv3 Tiny PRN architecture.

Now also YOLOv4 and YOLOv4-tiny are supported!

Demo

Image Source: Twitter @BAG_OFSP_UFSP

Dataset

The dataset for this pre-trained network is provided by VictorLin000 and contains 678 images of people with and without masks. In total there are 3 different classes annotated:

  • NONE - No mask at all.
  • BAD - Partial covered face.
  • GOOD - Mask coveres the essential parts.

You can download the dataset directly from google drive.

Training

The model has been trained on a 1080TI for about 2h over 6000 iteration with a batch size of 64 and 16 subdivisons (PRN & yolov4-tiny), 64 / 64 for yolov4.

Char

YOLOv3-tiny-prn / YOLOv4 / YOLOv4-tiny

To train the network yourself download the dataset, extract it into the training folder (/training/yolo/Mask_1.jpg) and download the initial weights into the training folder. To start the training run the following command:

darknet detector train obj.data yolov3-tiny-prn.cfg yolov3-tiny.conv.11

After the training, the resulting weights should be in the /training/backup/ folder. For instructions regarding YOLOv4, head over to AlexeyAB/darknet.

Inferencing

The weights have been trained on an image size 416x416 (PRN & tiny) / 608x608 (YOLOv4). It is also possible to inference it with a lower model size to increase the speed. A good performance / accuracy mix on CPUs has been discovered by using an image size of 128x128.

The model itself is fully compatible with the opencv dnn module and just ready to use.

Demo

There are either a python or a processing (java) demo you can run to test out your model directly with a webcam.

Python

To run the demo, please first install all the dependencies (requirements.txt) into a virtual environment and download the model and weights into the model folder (or run the shell script).

Then run the following command to start a webcam detector with Yolov3-Tiny-PRN:

# with python 3
python demo_webcam.py -n prn

# yolov4
python demo_webcam.py -s 608

# yolov4-tiny
python demo_webcam.-n tiny

Processing

For artists and makers there is also a basic example on how to use this network in Processing. The sketch itself is based on the following libraries:

Download

To download the models just run the download-models.sh in /models or use the following links:

If you are interested into the full size YOLOv3 pre-trained network, head over to this repository: VictorLin000/YOLOv3_mask_detect

About

Trained by cansik, the dataset fall under the terms and conditions of their owners (VictorLin000).

Demo images are from Unsplash:

yolo-mask-detection's People

Contributors

cansik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

yolo-mask-detection's Issues

This is mask detection. / 'Bad' condition does not appear.

Dear cansik
This is mask detection.

import ch.bildspur.vision.*;
import ch.bildspur.vision.result.*;

import processing.core.PApplet;
import processing.core.PConstants;
import processing.core.PImage;
import processing.video.Capture;

import java.nio.file.Paths;
import java.nio.file.Path;
import java.util.List;

// speed of the yolo algorithm (trained on 416)
int detectionSize = 128;

// size of the inferenced image 
// in relation to the original
float sizeFactor = 1.0;

// camera input width and height
int inputWidth = 640;
int inputHeight = 480;

Capture cam;
PImage inputImage;

DeepVision deepVision = new DeepVision(this);
YOLONetwork yolo;
List<ObjectDetectionResult> detections;

public void setup() {
  size(640, 480, FX2D);
  frameRate(30);
  colorMode(HSB, 360, 100, 100);

  println("creating model...");
  Path model = Paths.get(sketchPath("/models/mask-yolov4.cfg")).toAbsolutePath();
  Path weights = Paths.get(sketchPath("/models/mask-yolov4.weights")).toAbsolutePath();
  
  //Path model = Paths.get(sketchPath("/models/mask-yolov3-tiny-prn.cfg")).toAbsolutePath();
  //Path weights = Paths.get(sketchPath("/models/mask-yolov3-tiny-prn.weights")).toAbsolutePath();
  
  
  
  println(" model : " + model);
  println(" weights : " + weights);


  yolo = new YOLONetwork(model, weights, detectionSize, detectionSize);
  yolo.setLabels("good", "bad", "none");

  println("loading yolo model...");
  yolo.setup();

  String[] cams = Capture.list();
  println("Cameras: ");
  printArray(cams);

  //cam = new Capture(this, inputWidth, inputHeight, cams[0]);
  //cam.start();
  inputImage = new PImage(int(inputWidth * sizeFactor), int(inputHeight * sizeFactor), RGB);  
  inputImage = loadImage("victor-he-lf1ivjbtF2Q-unsplash.jpg");
  inputImage = loadImage("jeremy-stenuit-3mErKfgolzM-unsplash.jpg");  
  inputImage = loadImage("test2.jpg");  
  // inputImage = loadImage("test3.jpg");  
  inputImage.resize(width,height);
  
}

public void draw() {
  background(55);

  //if (cam.available()) {
  //  cam.read();
  //}
  //inputImage.copy(cam, 0, 0, cam.width, cam.height, 0, 0, inputImage.width, inputImage.height);



  yolo.setConfidenceThreshold(0.50f);
  detections = yolo.run(inputImage);
  image(inputImage, 0, 0);
  // cam.filter(GRAY);  image(cam, 0, 0);

  scale(1.0 / sizeFactor);
  for (ObjectDetectionResult detection : detections) {    
    noFill();
    strokeWeight(2f);

    switch(detection.getClassId()) {
    case 0:
      stroke(120, 80, 100);
      break;

    case 1:
      stroke(40, 80, 100);
      break;

    case 2:
      stroke(0, 80, 100);
      break;
    }
    noFill(); 
    rect(detection.getX(), detection.getY(), detection.getWidth(), detection.getHeight());


    fill(0,250); noStroke();
    String label = detection.getClassName();
    rect(detection.getX()-50, detection.getY()-25, 100, 20);
    fill(255); textAlign(CENTER);
    text(label + " " + nf(detection.getConfidence(), 0, 2), detection.getX(), detection.getY()-10);
    
  }

  surface.setTitle("Mask YOLO Test - FPS: " + Math.round(frameRate));
}

[result image]
https://drive.google.com/file/d/1sSd4qRMXRbAwwkpTXC8PIB2IRILEFB2s/view?usp=sharing

  1. 'Bad' condition does not appear.
  2. I used the yolo-v4 you trained.
    Why not? help me.

result

Weights file is not really weights! how can I use it or download it?

self.net = cv2.dnn.readNetFromDarknet(config, model)
cv2.error: OpenCV(4.5.1) /tmp/pip-req-build-n_alixql/opencv/modules/dnn/src/darknet/darknet_importer.cpp:207: error: (-212:Parsing error) Failed to parse NetParameter file: models/mask-yolov4-tiny.cfg in function 'readNetFromDarknet'

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.