Skip to content

Instantly share code, notes, and snippets.

Avatar

Rich Christiansen rinogo

  • Social5
  • Utah, USA
View GitHub Profile
@rinogo
rinogo / simple-email-debug.php
Created Apr 28, 2021
[Simple email debug] Instead of a bunch of `var_dump()` or `echo` statements (which can break AJAX responses, etc.), use these simple `mail()` lines.
View simple-email-debug.php
//String
mail("me@example.com", __FILE__ . ":" . __LINE__, "Example");
//Export variable
mail("me@example.com", __FILE__ . ":" . __LINE__, var_export($alternative_image, true));
@rinogo
rinogo / test-brisk.py
Created Apr 14, 2021
BRISK keypoint detection and brute force matching using OpenCV (Source: https://stackoverflow.com/a/65863713/114558)
View test-brisk.py
# Imports
import cv2 as cv
import matplotlib.pyplot as plt
import sys
# Open and convert the input and training-set image from BGR to GRAYSCALE
image1 = cv.imread(filename = sys.argv[1],
flags = cv.IMREAD_GRAYSCALE)
image2 = cv.imread(filename = sys.argv[2],
@rinogo
rinogo / call-with-trackbars.py
Last active Apr 30, 2021
An OpenCV utility function for displaying a set of easily-configurable trackbars and providing their results to an arbitrary callback - useful for quick prototyping and testing
View call-with-trackbars.py
#Usage: `python call-with-trackbars.py example.jpg`
import sys
import cv2
#### Example usage ####
def edge_detect(p):
edged = cv2.Canny(p["img"], p["low"] * 15, p["high"] * 15, apertureSize = (p["aperture"] * 2) + 3)
cv2.imshow("Edged", edged)
@rinogo
rinogo / optimal-resize.py
Created Mar 31, 2021
Optimally resize an image so that its line height is approximately 32 pixels (Keywords: OpenCV, Tesseract, OCR)
View optimal-resize.py
#Optimally resize `img` according to the bounding boxes specified in `boxes` (which is simply the (pruned) results from `pytesseract.image_to_data()`).
#Tesseract performs optimally when capital letters are ~32px tall (https://groups.google.com/g/tesseract-ocr/c/Wdh_JJwnw94/m/24JHDYQbBQAJ). Smaller text obviously can't be OCR'd as accurately, but weirdly enough, larger text causes problems as well. So, this function uses the bounding boxes we've found and resizes the image so that the median line height should be ~32px.
def optimal_resize(img, boxes):
median_height = np.median(boxes["height"])
target_height = 32 #See https://groups.google.com/g/tesseract-ocr/c/Wdh_JJwnw94/m/24JHDYQbBQAJ
scale_factor = target_height / median_height
print("Scale factor: " + str(scale_factor))
#If the image is already within `skip_percentage` percent of the target size, just return the original image (it's better to skip resizing if we can)
skip_percentage = 0.07
@rinogo
rinogo / playwright-wait-for-mutation-to-stop.js
Last active Mar 16, 2021
Wait for an element to remain unchanged for a period of time. Useful for waiting for asynchronous (AJAX) updates. Playwright, ES6, Promise, Mutation, MutationObserver
View playwright-wait-for-mutation-to-stop.js
///////
//To test this code, execute it locally or copy/paste it at https://try.playwright.tech/
//Usage example: `await waitForMutationToStop(await page.$("#container"));`
///////
// @ts-check
const playwright = require("playwright");
//Wait for `elem` to have no mutations for a period of `noMutationDuration` ms. If `timeout` ms elapse without a "no mutation" period of sufficient length, throw an error. If `waitForFirstMutation` is true, wait until the first mutation before starting to wait for the `noMutationDuration` period.
const waitForMutationToStop = async (elem, noMutationDuration = 3000, timeout = 60000, waitForFirstMutation = true) => {
@rinogo
rinogo / plugin.php
Created Jan 3, 2019
YOURLS plugin: keep query string
View plugin.php
<?php
/*
Plugin Name: Keep Query String
Description: Adds short URL query string, if any, to the long URL
Version: 0.1
Author: Ozh
*/
yourls_add_filter('redirect_location', 'ozh_kqs');
@rinogo
rinogo / UsHolidays.php
Created Nov 26, 2018
Simple unit-tested class to calculate federal holiday dates for the United States. Methods are included for determining if a date (timestamp) is considered a date and for accessing holidays by name and by timestamp.
View UsHolidays.php
<?php
//Adapted by rich at byu.net from https://www.damia.no/calculating-u-s-federal-holidays-with-php/
class UsHolidays {
private $year;
private $list;
const ONE_DAY = 86400; //Number of seconds in one day
@rinogo
rinogo / script.js
Last active Apr 10, 2018
Improved script.js for Contact Form 7 reCAPTCHA Wordpress plugin. It shows an error message if the user fails to check the reCAPTCHA ("I am not a robot") box.
View script.js
function contact_form_7_recaptcha_callback() {
jQuery(function($){
$('.g-recaptcha-explicit-id').each(function(){
$(this).data('grecaptcha_id', grecaptcha.render($('#' + this.value)[0], {
'sitekey' : contact_form_7_recaptcha_data.sitekey
}));
});
});
};
View people-names-indexed-by-gender.js
//Source data from http://answers.google.com/answers/threadview/id/107201.html
var male = ["abdul","abe","abel","abraham","abram","adalberto","adan","adolfo","adolph","agustin","ahmad","ahmed","al","alan","alberto","alden","aldo","alec","alejandro","alfonso","alfonzo","alfred","alfredo","allan","alonso","alonzo","alphonse","alphonso","alton","alvaro","alvin","amado","ambrose","amos","anderson","andreas","andres","andy","anibal","antione","antoine","anton","antone","antony","antwan","archie","arden","arlen","arlie","armand","armando","arnold","arnoldo","arnulfo","aron","arron","art","arturo","asa","august","augustus","aurelio","barney","barrett","barry","bart","barton","basil","beau","ben","benedict","benito","benjamin","bennett","benny","benton","bernard","bernardo","bert","bertram","bill","blaine","bo","bob","booker","boris","boyce","boyd","brad","bradford","bradley","bradly","brady","brain","branden","brant","brendan","brendon","brent","brenton","bret","brice","brock","broderick","brooks","bruce","bruno","b
View people-names-with-gender.csv
We can make this file beautiful and searchable if this error is corrected: It looks like row 4 should actually have 1 column, instead of 2. in line 3.
#Original Source: http://answers.google.com/answers/threadview/id/107201.html
#
#This data is all available in raw form from the United States Census
#Site, which is about as authoritative a source as you will get on the
#subject. I have used the following files:
#
#US Male Names: http://www.census.gov/genealogy/names/dist.male.first
#US Female Names: http://www.census.gov/genealogy/names/dist.female.first
#US Last Names: http://www.census.gov/genealogy/names/dist.all.last
#