Skip to content

Instantly share code, notes, and snippets.

View matteobarbieri's full-sized avatar

Matteo Barbieri matteobarbieri

  • Epidemic Sound
  • Stockholm
View GitHub Profile
@matteobarbieri
matteobarbieri / pair_k4.sh
Created March 31, 2021 12:26
Keychron K4 pairing script
#!/bin/bash
####################
### INSTRUCTIONS ###
####################
# 1. To speed up things, switch to bt mode and select the right channel (it will make things easier in the next steps)
# 2. Switch back to USB mode (so that you can actually enter commands)
# 3. Run this script and then quickly switch to bt mode and once the blinking stops hold FN + the corresponding bt channel;
# for instance, I have paired it with Windows on channel 1, and on Linux on channel 2, so in my case it's FN + 2. Release it
@matteobarbieri
matteobarbieri / smart_classify.py
Created October 25, 2020 10:55
Ruining Sudoku part 3: smart_classify.py
import torch
def smart_classify(
resized, net, threshold=75, device='cuda',
conf_threshold=0.9, debug=False):
"""
First determined whether the cell is empty or not, checking the number of
pixels different from zero.
Then use a neural network to classify digits.
@matteobarbieri
matteobarbieri / printed_MNIST.py
Created October 25, 2020 10:30
Ruining Sudoku part 3: printed_MNIST.py
from torch.utils.data import Dataset
import torch
from PIL import Image
from PIL import ImageFont
from PIL import ImageDraw
import glob
import random
@matteobarbieri
matteobarbieri / sudoku_solver.py
Created October 23, 2020 15:00
Ruining Sudoku  -  A Data Science project (Part 3: Digits recognition and Sudoku solver) - sudoku_solver.py
def find_empty_grid(x): # checks for empty spaces '0'.
for i in range(len(x)):
for j in range(len(x[0])):
if x[i][j] == 0:
return (i, j) # return row, col
return None
def solve_array(x):
@matteobarbieri
matteobarbieri / crop_and_warp.py
Created October 18, 2020 15:29
Ruining Sudoku part 2: crop_and_warp.py
import cv2
import numpy as np
def crop_and_warp(img: np.ndarray, crop_rect):
"""
Crops and warps a rectangular section from an image into a square of
similar size.
"""
# Rectangle described by top left, top right, bottom right and bottom left
@matteobarbieri
matteobarbieri / find_corners_of_largest_polygon.py
Created October 18, 2020 14:48
Ruining Sudoku part 2: find_corners_of_largest_polygon.py
import cv2
import numpy as np
import operator
def find_corners_of_largest_polygon(img: np.ndarray):
"""Finds the 4 extreme corners of the largest contour in the image."""
# Find contours
contours, h = cv2.findContours(
@matteobarbieri
matteobarbieri / pre_process_image.py
Created October 18, 2020 14:37
Ruining Sudoku part 2: pre_process_image.py
import cv2
import numpy as np
def pre_process_image(img: np.ndarray, skip_dilate: bool=True):
"""
Uses a blurring function, adaptive thresholding and dilation to expose
the main features of an image.
"""
# Gaussian blur with a kernal size (height, width) of 9.
import numpy as np
from scipy.signal import convolve2d
def print_board(board):
R, C = board.shape
for i in range(R):
row = ""
for j in range(C):
git clone https://github.com/CSSEGISandData/COVID-19
cd COVID-19
conda create --name jupyter python=3 matplotlib seaborn \
pandas scikit-learn
conda activate jupyter
conda install -c conda-forge jupyterlab
mkdir notebooks
jupyter lab