Skip to content

Instantly share code, notes, and snippets.

@twiddles
twiddles / mongoid_1_1.rb
Created April 14, 2011 09:15
tested on jruby 1.6
require 'rubygems'
require 'mongoid'
class Coach
include Mongoid::Document
field :name, :type => String
belongs_to :coached, :class_name => 'Team', :inverse_of => :coach, :foreign_key => "coach_id"
belongs_to :assisted, :class_name => 'Team', :inverse_of => :assist, :foreign_key => "assist_id"
end
require 'rubygems'
require 'mongoid'
Mongoid.configure do |config|
config.master = Mongo::Connection.new.db("test")
end
class User
include Mongoid::Document
field :name
@twiddles
twiddles / gist:1422786
Created December 2, 2011 10:52
sanitize config value
package snippet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class Snippet {
public static void main(String[] args) {
String[] examples = new String[] { "configGlossary:poweredByIcon -> CONFIG_GLOSSARY_POWERED_BY_ICON", "124$32SomeSampleString_thatI_have -> SOME_SAMPLE_STRING_THAT_I_HAVE", "myJSP -> MY_JSP" };
for (String s : examples) {
for source in SOURCES:
print('Processing:', source)
img = io.imread(source)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
filename = os.path.basename(source)
cv2.imwrite('processed_' + filename, img)
# Output
# Processing: lena.jpg
# Processing: monarch.png
for source in SOURCES:
print('Processing:', source)
try:
img = io.imread(source)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
filename = os.path.basename(source)
cv2.imwrite('processed_' + filename, img)
except Exception as e:
print('Skipping', source, 'because of', e)
for source in SOURCES:
print('Processing:', source)
try:
img = io.imread(source)
img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
filename = os.path.basename(source)
cv2.imwrite('processed_' + filename, img)
img = cv2.resize(img, (64, 64))
cv2.imwrite('thumbnail_' + filename, img)
Processing: lena.jpg
Processing: monarch.png
Processing: https://github.com/opencv/opencv/raw/master/samples/data/aero3.jpg
Processing: https://github.com/opencv/opencv/raw/master/samples/data/fruits.jpg
Processing: https://www.somewhere.in/no/mans/land.jpg
Skipping https://www.somewhere.in/no/mans/land.jpg because of <urlopen error [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)>
import matplotlib.pyplot as plt
def dominant_colors(img):
img = cv2.resize(img, (64, 64))
all_colors = img.reshape(-1, 3)
kmeans = KMeans(n_clusters=8).fit(all_colors)
palette = kmeans.cluster_centers_.astype(np.uint8)
# sort values from brightest to darkest
idx = np.argsort(palette.sum(axis=1))[::-1]
from keras.applications.resnet50 import ResNet50
from keras.preprocessing import image
from keras.applications.resnet50 import preprocess_input, decode_predictions
import numpy as np
model = ResNet50(weights='imagenet')
def save_tags(filename):
img = image.load_img(filename, target_size=(224, 224))
x = image.img_to_array(img)
@twiddles
twiddles / .cs
Created July 24, 2023 21:24
Procedure Level Generator
public void Initialize(int newWidth, int newHeight)
{
Random.InitState(seed + GameManager.Instance.currentLevel);
foreach (Transform child in transform)
{
Destroy(child.gameObject);
}
width = newWidth;
height = newHeight;