Skip to content

Commit

Permalink
commit
Browse files Browse the repository at this point in the history
  • Loading branch information
jcao243 committed Feb 9, 2024
1 parent ec049a1 commit c74f697
Show file tree
Hide file tree
Showing 6 changed files with 621 additions and 0 deletions.
Binary file added ObjectDetector_YOLO_single_image/images/cat.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added ObjectDetector_YOLO_single_image/images/cat2.JPG
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 20 additions & 0 deletions ObjectDetector_YOLO_single_image/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<html>
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1">

<title>YOLO with image</title>

<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>

<script src="./ml5.min.js" type="text/javascript"></script>
<script src="sketch.js"></script>
</head>

<body>
<h1>YOLO image detection on single image</h1>
</body>

</html>
551 changes: 551 additions & 0 deletions ObjectDetector_YOLO_single_image/ml5.min.js

Large diffs are not rendered by default.

50 changes: 50 additions & 0 deletions ObjectDetector_YOLO_single_image/sketch.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
let objectDetector;
let img;
let objects = [];
let status;

function preload() {
img = loadImage("images/cat2.JPG");
}

function setup() {
createCanvas(640, 420);
image(img, 0, 0);
objectDetector = ml5.objectDetector("yolo", modelReady);
}

// Change the status when the model loads.
function modelReady() {
console.log("model Ready!");
status = true;
console.log("Detecting");
objectDetector.detect(img, gotResult);
}

// A function to run when we get any errors and the results
function gotResult(err, results) {
if (err) {
console.log(err);
}
console.log(results);
objects = results;

// Draw results on canvas
for (let i = 0; i < objects.length; i += 1) {
noStroke();
fill(0, 255, 0);
const x = objects[i].normalized.x * width;
const y = objects[i].normalized.y * width;
const objectWidth = objects[i].normalized.width * width;
const objectHeight = objects[i].normalized.height * height;
text(
`${objects[i].label} ${nfc(objects[i].confidence * 100.0, 2)}%`,
x + 5,
y + 15,
);
noFill();
strokeWeight(4);
stroke(0, 255, 0);
rect(x, y, objectWidth, objectHeight);
}
}

0 comments on commit c74f697

Please sign in to comment.