Skip to content

Instantly share code, notes, and snippets.

@etrigger
etrigger / gist:ee662fb9c684967c20562629f2b94db6
Created July 15, 2022 01:47 — forked from bazub/gist:3877971
Grayscale/Binary images using PIL
im=Image.open("1.jpg")
#im=im.rotate(1)
im.save("e.jpg")
im2=im.convert("L")
im2.save("b.jpg")
threshold = 100
im = im2.point(lambda p: p > threshold and 255)
im.save("d.jpg")
img="d.jpg"
result = tesseract.ProcessPagesWrapper(img,api)
@etrigger
etrigger / rename_state_dict_keys.py
Created March 15, 2022 04:25 — forked from the-bass/rename_state_dict_keys.py
Rename the parameters of a PyTorch module's saved state dict. Last tested with PyTorch 1.0.1.
import torch
from collections import OrderedDict
def rename_state_dict_keys(source, key_transformation, target=None):
"""
source -> Path to the saved state dict.
key_transformation -> Function that accepts the old key names of the state
dict as the only argument and returns the new key name.
target (optional) -> Path at which the new state dict should be saved
@etrigger
etrigger / test_multiprocess.py
Created February 20, 2022 16:10 — forked from Taekyoon/test_multiprocess.py
Test Pytorch multiprocess IterableDataset
import torch
import math
import time
SLEEP_TIME = 0.1
class MyMapDataset(torch.utils.data.Dataset):
def __init__(self, size):
self.dataset = [i for i in range(size)]
@etrigger
etrigger / run_mlm_big_text_files.py
Created February 16, 2022 13:29 — forked from finiteautomata/run_mlm_big_text_files.py
Train MLM with big text files (Workaround)
"""
This is a workaround for `examples/run_mlm.py` for pretraining models
with big text files line-by-line.
For the time being, `datasets` is facing some issues dealing with really
big text files, so we use a custom dataset until this is fixed.
August 3th 2021
Author: Juan Manuel Pérez
@etrigger
etrigger / autocorrrect.py
Created August 13, 2019 04:29 — forked from bgreenlee/autocorrrect.py
Simple ngram autocorrect #python #algorithms
import os.path
import collections
from operator import itemgetter
WORDFILE = '/usr/share/dict/words'
class Autocorrect(object):
"""
Very simplistic implementation of autocorrect using ngrams.
"""
@etrigger
etrigger / autocorrrect.py
Created July 14, 2019 06:55
Simple ngram autocorrect #python #algorithms
from future import print_function
import os.path
import collections
from operator import itemgetter
if hasattr(__builtins__, 'raw_input'):
input = raw_input
WORDFILE = '/usr/share/dict/words'
@etrigger
etrigger / rotate_point_opencv.cpp
Created April 13, 2016 11:28 — forked from ashwin/rotate_point_opencv.cpp
Rotate point in OpenCV
cv::Point2f RotatePoint(const cv::Point2f& p, float rad)
{
const float x = std::cos(rad) * p.x - std::sin(rad) * p.y;
const float y = std::sin(rad) * p.x + std::cos(rad) * p.y;
const cv::Point2f rot_p(x, y);
return rot_p;
}
cv::Point2f RotatePoint(const cv::Point2f& cen_pt, const cv::Point2f& p, float rad)
class Solution {
public:
int maximalRectangle(vector<vector<char> > &matrix) {
if(matrix.size()==0 || matrix[0].size()==0)return 0;
vector<vector<int>>m(matrix.size()+1,vector<int>(matrix[0].size()+1,0));
for(int i=0;i<matrix.size();i++)
for(int j=matrix[0].size()-1;j>=0;j--)
m[i][j]=matrix[i][j]=='1'?1+m[i][j+1]:0;
int max=0;
for(int i=0;i<matrix[0].size();i++){
#include <std::vector>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
/*
This work by Ryan Muller released under the Creative Commons CC0 License
http://creativecommons.org/publicdomain/zero/1.0/
*/
/**
#include <std::vector>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <stdio.h>
/*
This work by Ryan Muller released under the Creative Commons CC0 License
http://creativecommons.org/publicdomain/zero/1.0/
*/
/**