Skip to content

Instantly share code, notes, and snippets.

View doleron's full-sized avatar
💭
I may be slow to respond.

Luiz doleron | Luiz d'Oleron doleron

💭
I may be slow to respond.
View GitHub Profile
@doleron
doleron / medium_yolov5_calling_model.cpp
Last active January 23, 2022 11:47
C++ calling model
std::vector<cv::Mat> predictions;
net.forward(predictions, net.getUnconnectedOutLayersNames());
const cv::Mat &output = predictions[0];
@doleron
doleron / medium_yolov5_unwrapping_data.py
Last active January 23, 2022 12:49
Python unwrapping the data
def unwrap_detection(input_image, output_data):
class_ids = []
confidences = []
boxes = []
rows = output_data.shape[0]
image_width, image_height, _ = input_image.shape
x_factor = image_width / 640
@doleron
doleron / medium_yolov5_unwrapping_data.cpp
Last active January 23, 2022 13:30
C++ unrwapping predictions
struct Detection
{
int class_id;
float confidence;
cv::Rect box;
};
void detect(const cv::Mat &input_image, constcv::Mat &output, std::vector<Detection> &output) {
float x_factor = input_image.cols / 640.;
@doleron
doleron / medium_yolov5_nms.py
Created January 23, 2022 12:51
Python Using to reduce duplicates
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.25, 0.45)
result_class_ids = []
result_confidences = []
result_boxes = []
for i in indexes:
result_confidences.append(confidences[i])
result_class_ids.append(class_ids[i])
result_boxes.append(boxes[i])
@doleron
doleron / medium_yolov5_print_detections.py
Last active January 23, 2022 13:13
Python print detections
class_list = []
with open("classes.txt", "r") as f:
class_list = [cname.strip() for cname in f.readlines()]
colors = [(255, 255, 0), (0, 255, 0), (0, 255, 255), (255, 0, 0)]
for i in range(len(result_class_ids)):
box = result_boxes[i]
class_id = result_class_ids[i]
@doleron
doleron / medium_yolov5_using_cuda.py
Created January 23, 2022 13:39
Python using CUDA
# load the model as usual
net = cv2.dnn.readNet('yolov5s.onnx')
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_CUDA)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CUDA_FP16)
auto net = cv::dnn::readNet("yolov5s.onnx");
net.setPreferableBackend(cv::dnn::DNN_BACKEND_CUDA);
net.setPreferableTarget(cv::dnn::DNN_TARGET_CUDA_FP16);
@doleron
doleron / medium_yolov5_image_and_annotations.py
Created January 26, 2022 10:08
Mediu YOLOv5 training image and annotations
import cv2
image_path = 'data/full/with_mask (4)'
image = cv2.imread(image_path + '.jpg')
class_list = ['using mask', 'without mask']
colors = [(0, 255, 0), (0, 255, 255)]
height, width, _ = image.shape
@doleron
doleron / medium_yolov5_training_set_data.py
Created January 26, 2022 10:22
Medium yolov5 set data
import os, shutil, random
# preparing the folder structure
full_data_path = 'data/obj/'
extension_allowed = '.jpg'
split_percentage = 90
images_path = 'data/images/'
if os.path.exists(images_path):
@doleron
doleron / medium_synthetic_data_generation.js
Last active February 1, 2022 11:56
Generating synthetic data in javascript
const N = 91;
// generating a sequence 91 elements from 0 to 2π
const xs = [...Array(N).keys()].map(x => 2*Math.PI*x / (N - 1));
// generating f(x) = sin(x)
const signal = xs.map(x => Math.sin(x));
// sampling 20 random elements and adding a N(0, 0.4) gaussian error
const sampleSize = 20;
const sample = [...Array(N).keys()].sort(() => 0.5 - jStat.uniform.sample(0, 1)).slice(0, sampleSize).map(index => ({x: index, y: signal[index] + jStat.normal.sample(0.0, 0.4)}));