Skip to content

Instantly share code, notes, and snippets.

View docPhil99's full-sized avatar

Phil docPhil99

  • University of Sussex
View GitHub Profile
@docPhil99
docPhil99 / opencv_qt_label.md
Last active April 13, 2024 15:48
How to display opencv video in pyqt apps

The code for this tutorial is here

Opencv provides are useful, but limited, method of building a GUI. A much more complete system could be acheived using pyqt. The question is, how do we display images. There are quite a few possible routes but perhaps the easiest is to use QLabel since it has a setPixmap function. Below is some code that creates two labels. It then creates a grey pixmap and displays it one of the labels. code: staticLabel1.py

from PyQt5.QtWidgets import QWidget, QApplication, QLabel, QVBoxLayout
from PyQt5.QtGui import QPixmap, QColor
import sys
@docPhil99
docPhil99 / macFFmpeg.md
Last active March 1, 2024 12:31
Mac webcam FFmpeg

#Capture and stream a webcam To capture using the iSight camera on a Mac, or infact any other webcam connected to the Mac, we can use FFmpeg. First get a list of the devices installed.

ffmpeg -f avfoundation -list_devices true -i "" 

This will list the aviable video and audio devices.

The below will capture at 30fps and the set video size to a file. ffmpeg -f avfoundation -framerate 30 -video_size 640x480 -i "0:none" out.avi

@docPhil99
docPhil99 / rename_USB_device.md
Created December 23, 2023 13:17
Renaming USB sound card

My USB speakers seem to have some buggy firmware in that the device name has been corrupted with some binary. At first I thought it was unicode, but uni2ascii said overwise. Since I need to be able to type the name into config files I had to rename the device using udev.

First we need to fine the name of the device. Run udevadm monitor --kernel --subsystem-match=sound

Unplug and re-plug in the DAC, this results in:


KERNEL[1585.716702] remove   /devices/pci0000:00/0000:00:12.0/usb4/4-5/4-5:1.0/sound/card1/controlC1 (sound)
@docPhil99
docPhil99 / pi_networking.md
Last active November 10, 2023 00:16
Setup a Raspberry Pi (or any Ethernet enabled device) on a private network

My system consists of a host PC that has two ethernet cards. What I want to do is use one for connecting to the inernet and the other to run a private network of Raspberry Pi computers.

My first ethenet card is easy, it's basically grabbing its setting via DHCP from my router. What I need to do is create a different subnet for the second card. So first of all I need to look up current IP address. On linux I can run ip a I need to find the address and mask from there. In my case it's the line starting inet 192.168.86.36/24 The 24 is mask, in this case the last digit of the IP address are all on the same subnet. See https://www.calculator.net/ip-subnet-calculator.html here if you want to calculate your own range.

For the second card I need to pick a static IP address that's on a different subnet. There are a number of IP address that are reserved for private networks so it's best to use one of these.

Class A- 10.0.0.0 to 10.255.255.255
Class B- 172.16.0.0 to 172.31.255.255
Class C- 192.168.0.0 to 19
@docPhil99
docPhil99 / arg_parse_demo.py
Last active November 1, 2023 20:20
Simple demo of argparse in python
"""Simple demo of argparse in python, see http://zetcode.com/python/argparse/"""
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('name') # positional argument, note no dash!
parser.add_argument('-n', type=int, required=True, help="the number") # Required argument, must be a int, eg. -n 4
parser.add_argument('-e', type=int, default=2, help="defines the value") # Optional argument with a default
parser.add_argument('-o', '--output', action='store_true', help="shows output") # a binary flag
@docPhil99
docPhil99 / pythonVideoCapture.md
Last active September 28, 2023 18:11
How to write lossless video in Python

OpenCV does a reasonable job of reading videos from file or webcams. It's simple and mostly works. When it comes to writing videos, it however leaves a lot to be desired. There is little control over the codecs and it is almost impossible to know which codecs are installed. It also wants to know things like the frame size at intailisation. This isn't always a problem, but if you don't know it yet it means you have to set up the video writer inside your main processing loop.

To make something as cross-platform compatible as possible it would be nice to use FFmpeg. There are a few python wrappers around, but as far as I can tell they are mainly used for transcoding type applications. One solution is run FFmpeg as a subprocess and set its input to accept a pipe. Then every video frame is passed through the pipe. You write this yourself, in fact it's only a few lines of code. However, the scikit-video package will do this for us, with some nice boilerplate to ma

@docPhil99
docPhil99 / loguru.md
Last active June 29, 2023 12:33
Python Loguru Hacking

Loguru is my favorite way to perform logging in python. It's generally so simple to use but a few things are missing from the documentation.

Logging over the network

This is based on the loguru example, but also includes the hostname of the sender. To add extra fields the pefered method is to add to record["extra"] dictionary (which is normally empty). The client code sends the log messages over the network and to stderr.

class LoggingSocketHandler:
    def __init__(self, host, port, timeout=600):
@docPhil99
docPhil99 / build_squeezelite.md
Last active June 5, 2023 03:18
Setting up a headless linux music server
  1. Stop the old service if it is running with sudo service squeezelite stop
  2. Remove the old version if installed with sudo apt-get remove squeezelite
  3. Install its dependancies sudo apt-get install lsb-base libasound2 libavcodec57 libavcodec-extra57 libavformat57 libavutil55 libc6 libfaad2 libflac8 liblirc-client0 libmad0 libmpg123-0 libsoxr0 libvorbisfile3
  4. Create a directory to download the source files in your home drive mkdir sources; cd sources
  5. Clone the source code git clone https://github.com/ralph-irving/squeezelite.git
  6. cd squeezelite
  7. sudo apt-get install libavcodec-dev libavformat-dev
  8. sudo apt-get install libsoxr-lsr0 libsox-dev sox libsoxr-dev
  9. Build it with make all OPTS="-DDSD -DRESAMPLE -DFFMPEG"
  10. sudo cp squeezelite /usr/bin/. to copy the file to you path
@docPhil99
docPhil99 / Goodman_speckle_test.py
Last active April 17, 2023 11:18
Python version of Joseph Goodman's Mathematica code for simulating laser speckle. Both free space and in an optical system are combined into the one file.
# my conversion of Goodman's code
import numpy as np
import matplotlib.pyplot as plt
# Program for simulating Speckle Formation by Free Space Propagation
# Translate from Mathematica
# Ref: Goodman, Joseph W. 2020. Speckle Phenomena in Optics: Theory and Applications, Second Edition. SPIE.
n = 1024 # nxn array size
@docPhil99
docPhil99 / controlling_ha.md
Last active March 7, 2023 17:12
Controlling Home Assistant remotely via curl

It's actually fairly easy to control Home Assistant remotely using curl but I couldn't find a complete solution on how to do this, so here goes...

  1. Activate the api in configuration.yaml by adding the line api:
  2. Get an Authorization token from HA (It's a Long-Lived Access Tokens which can be created on your HA user profile page)
  3. This list of exposed states can be found using curl -X GET -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" http://YOUR_IP:8123/api/states | prettyjson
  • Note prettyjson is an alias for python -m json.tool, you don't need this it's just easier to read.
  1. To get the state of a device append the entity.id to the URL, eg curl -X GET -H "Authorization: Bearer YOUR_TOKEN" -H "Content-Type: application/json" http://YOUR_IP:8123/api/states/switch.mylight | prettyjson
  2. The API documentation shows you how to change the states but this does not actually turn on the lights, etc. Instead, us