Skip to content

Instantly share code, notes, and snippets.

import os
import sys
import subprocess
import timeout_decorator
import random
import logging
if sys.version_info[0] < 3:
import pipes as shlex
else:
'''
Demonstration of using `splice` with non-blocking IO
Lots of code is similar to 'splice.py', take a look at that module for more
documentation.
'''
#https://blogs.gnome.org/markmc/2013/06/04/async-io-and-python/
import os
import os.path
import errno
import fcntl
@phuocphn
phuocphn / threading_callback.py
Last active May 10, 2018 14:29
Python threading with callback function (callback function run after thread is finished)
import time
import threading
class BaseThread(threading.Thread):
def __init__(self, callback=None, callback_args=None, *args, **kwargs):
target = kwargs.pop('target')
super(BaseThread, self).__init__(target=self.target_with_callback, *args, **kwargs)
self.callback = callback
self.method = target
# -*- coding: utf-8 -*-
import unittest
import re
import datetime
from dateutil.relativedelta import relativedelta
#Standard datetime_string is string have all written by English, no GMT+xx, no [ago, today, yesterday] tag
def convert_to_standard_datetime_string(datetime_string):
@phuocphn
phuocphn / results.txt
Created May 11, 2018 16:11 — forked from mikeboers/results.txt
Comparing speed of reading video frames via a subprocess, or via PyAV
2013-10-05 10:19:59 mikeboers@maxwell: ~/Documents/Python/PyAV
$ time python video_frames_via_pipe.py
real 0m0.564s
user 0m0.729s
sys 0m0.241s
2013-10-05 10:20:01 mikeboers@maxwell: ~/Documents/Python/PyAV
$ time python video_frames_via_pyav.py
@phuocphn
phuocphn / gEncodeH264WithPreviews.sh
Created September 6, 2018 14:25 — forked from jetsonhacks/gEncodeH264WithPreviews.sh
Using Gstreamer, take a h264 stream from a Logitech c920, preview it on the display and save it to a file along with the webcam audio.
#!/bin/sh
# Preview and save webcam video on NVIDIA Jetson TK1
# Grab audio and video (in h264 format) from Logitech c920 @ 1920x1080
# Preview @ 1280x720 on screen
# Store video to file named gEncode1080p.mp4
# Logitech c920 is video1 on this machine
VELEM="v4l2src device=/dev/video1 do-timestamp=true"
# Video capability from the camera - get h264 1920x1080
VCAPS="video/x-h264, width=1920, height=1080, framerate=30/1"
@phuocphn
phuocphn / webcam_gstreamer
Created September 6, 2018 15:18 — forked from yoganand/webcam_gstreamer
Gstreamer Pipeline for webcam to rtmpserver
gst-launch-1.0 v4l2src ! "video/x-raw,width=640,height=480,framerate=15/1" ! \
x264enc bitrate=1000 ! video/x-h264,profile=high ! h264parse ! queue ! \
flvmux name=mux pulsesrc ! audioresample ! audio/x-raw,rate=48000 ! \
queue ! voaacenc bitrate=32000 ! queue ! mux. mux. ! rtmpsink location="rtmp://127.0.0.1/mytv/mystream live=1"
import os
import numpy as np
import tensorflow as tf
import tflearn
import matplotlib.pyplot as plt
from tensorflow.examples.tutorials.mnist import input_data
import tensorlayer as tl
import math
tf.reset_default_graph()
# TF-Slim is a lightweight library for defining, training and evaluating complex models in TensorFlow.
@phuocphn
phuocphn / py3_AES.py
Last active March 10, 2019 06:31
Encryption using pycrypto, AES, and PKCS5 padding
'''
Author: komuw/pycrypto_AES.py
Security issues:
+ https://stackoverflow.com/questions/2641720/for-aes-cbc-encryption-whats-the-importance-of-the-iv
+ https://defuse.ca/cbcmodeiv.htm
+ https://passingcuriosity.com/2009/aes-encryption-in-python-with-m2crypto/
'''
from Crypto.Cipher import AES
@phuocphn
phuocphn / distributed_relu_pytorch.py
Created February 29, 2020 12:37
distributed_relu_pytorch.py
import os
import tempfile
import torch
import torch.distributed as dist
import torch.nn as nn
import torch.optim as optim
import torch.multiprocessing as mp
import numpy as np
import random
from torch.nn.parallel import DistributedDataParallel as DDP