Skip to content

Instantly share code, notes, and snippets.

View waveform80's full-sized avatar

Dave Jones waveform80

View GitHub Profile
@waveform80
waveform80 / gist:3390484
Created August 19, 2012 00:12
GStreamer 1 image overlay on video
# Working (overlays image on videotestsrc)
gst-launch-1.0 videomixer name=mix ! videoconvert ! xvimagesink multifilesrc location="test.png" caps="image/png,framerate=0/1" ! pngdec ! imagefreeze ! alpha method=0 alpha=0.5 ! mix. videotestsrc ! "video/x-raw,format=AYUV,framerate=25/1,width=320,height=240" ! mix.
# Fails (attempts to overlay image on webcam source)
gst-launch-1.0 videomixer name=mix ! videoconvert ! xvimagesink multifilesrc location="test.png" caps="image/png,framerate=0/1" ! pngdec ! imagefreeze ! alpha method=0 alpha=0.5 ! mix. v4l2src device=/dev/video0 ! "video/x-raw,format=YUY2,width=320,height=240" ! videoconvert ! mix.
# Output of failing command:
cat << EOF
Setting pipeline to PAUSED ...
@waveform80
waveform80 / demeter_nonroot_1204.rst
Created October 26, 2012 14:55
Non-root Demeter installation under Ubuntu 12.04

Demeter Installation

These instructions cover a non-root manual build of Bruce Ravel's Demeter package under Ubuntu 12.04. The instructions are deliberately written with a non-technical audience in mind and hence might read as somewhat patronizing for the technical reader. The main

@waveform80
waveform80 / gist:4124157
Created November 21, 2012 10:29
LEAST alternative
CASE WHEN ContractPrice < DiscountPrice
THEN
CASE WHEN ContractPrice < ListPrice THEN ContractPrice ELSE ListPrice END
ELSE
CASE WHEN DiscountPrice < ListPrice THEN DiscountPrice ELSE ListPrice END
END
@waveform80
waveform80 / gist:4124953
Created November 21, 2012 13:54
Example of recursive date generation in SQL
CREATE VIEW some_dates AS
WITH RECURSIVE dates(i, d) AS (
VALUES (1, DATE('1970-01-01'))
UNION ALL
SELECT i + 1, d + 1 DAY FROM dates WHERE d < DATE('2070-12-31')
)
SELECT
d.d AS a_date,
YEAR(d.d) AS a_year,
MONTH(d.d) AS a_month,
@waveform80
waveform80 / gist:4657034
Last active December 11, 2015 20:39
Pivotting columns in SQL...
WITH qdps_with_rows AS (
SELECT
ROW_NUMBER() OVER (PARTITION BY q.product_code) AS row_num,
q.*
FROM qdps q
)
SELECT
p.product_code,
SUM(CASE WHEN q.row_num = 1 THEN q.value END) AS qdp1,
SUM(CASE WHEN q.row_num = 2 THEN q.value END) AS qdp2,
@waveform80
waveform80 / getladata.py
Created May 2, 2013 17:17
Converting flat SQL output to a JSON array of hashes in Python
#!/usr/bin/env python
import json
from itertools import izip, cycle, groupby
from operator import itemgetter, attrgetter
# Imagine this is the result of some DB query like:
# SELECT region, year, value FROM some_table ORDER BY region, year
data = [
('Southwark', 2009, 71.89),
def process_iis_directory(d):
print('Processing IIS directory %s' % d)
for entry in os.listdir(d):
entry = os.path.join(d, entry)
if entry.endswith('.gz'):
print('Processing %s into CSV' % entry)
with gzip.open(entry, 'rb') as uncompressed, \
io.BufferedReader(uncompressed) as buffered, \
io.TextIOWrapper(buffered, encoding='latin1') as infile, \
io.open(os.path.splitext(entry)[0] + '.csv', 'wb') as outfile, \
@waveform80
waveform80 / factorial.py
Last active December 25, 2015 19:48
Factorial in a functional style
def factorial(i):
if i > 1:
return factorial(i - 1) * i
elif i == 1:
return 1
else:
raise ValueError('Cannot take factorial of %d' % i)
@waveform80
waveform80 / README.md
Last active September 20, 2022 19:32
Recording motion before and after detection

Capturing video both before and after motion detection

Assuming you want to record 10 seconds of video before motion is detected, and 10 seconds of video after motion is detected, you can simply create a circular buffer with enough space for 20 seconds of video, wait until motion is detected, then wait 10 seconds more, and only then write the contents of the circular buffer to disk. basic_circular.py demonstrates this.

The second example is a little more complicated and results in a couple of files per motion event: the 10 seconds before and the all seconds that motion is still occurring after first detection. Once motion is detected, we start recording subsequent frames to a file, and while that's going on, dump the circular buffer to another file. Once we stop detecting motion, we close the "after" file, and split recording back to the circular stream. This is demonstrated in advanced_circular.py.

@waveform80
waveform80 / lots_of_jpegs.py
Last active April 19, 2017 07:00
Custom output for writing individual JPEGs from an MJPEG
import io
import picamera
class MyOutput(object):
def __init__(self):
self.file_num = 0
self.output = None
def write(self, buf):
if buf.startswith(b'\xff\xd8'):