Skip to content

Instantly share code, notes, and snippets.

View kevinlinxc's full-sized avatar

Kevin Lin kevinlinxc

View GitHub Profile
@kevinlinxc
kevinlinxc / keyboard_to_letter_contour.py
Created March 23, 2024 19:00
On key press, display a letter in the center of cv2 imshow, and get points on the contour of the letter
from PIL import Image, ImageDraw, ImageFont
import cv2
import numpy as np
def get_bounding_rect_area(contour):
# calculate area using the minimum bounding rectangle instead of whatever cv2.contourArea does
rect = cv2.minAreaRect(contour)
width = rect[1][0]
height = rect[1][1]
return width * height
@kevinlinxc
kevinlinxc / opencv_trackbars_example.py
Created March 15, 2024 00:33
opencv_trackbars_example.py
import cv2
import numpy as np
def nothing(x):
pass
# Create a black image, a window
img = np.zeros((300, 512, 3), np.uint8)
@kevinlinxc
kevinlinxc / serial_sender.py
Created February 4, 2024 04:00
Serial Sender
import serial
import serial.tools.list_ports
def get_ports():
"""
Get a list of available serial ports.
Returns:
List of serial ports, or None if no ports are available.
"""
serial_ports = serial.tools.list_ports.comports()
@kevinlinxc
kevinlinxc / haversine.py
Last active February 10, 2024 08:34
Haversine Distance Calculator
"""Calculate the difference between two GPS coordinates using the Haversine formula"""
from math import radians, cos, sin, sqrt, atan2
def haversine(coord1, coord2):
# Radius of the Earth in kilometers
R = 6378.137
# Converting coordinates from degrees to radians
lat1, lon1 = radians(coord1[0]), radians(coord1[1])
@kevinlinxc
kevinlinxc / arduino_port_detector.py
Created October 14, 2023 23:24
Arduino Port Detector Utility
"""
This simple helper script auto-detects the Arduino port.
Saves time because you don't need to check the Arduino IDE/device manager to get the port number each time.
"""
import serial.tools.list_ports
def get_port():
"""
Get the port name of a connected Arduino if one is connected.
@kevinlinxc
kevinlinxc / frame_video.py
Created October 14, 2023 23:20
Simple OpenCV video genereator where each frame is the frame number
import cv2
import numpy
# make a video where each frame is the frame number
writer = cv2.VideoWriter("frame_numbers.mp4", cv2.VideoWriter_fourcc(*"mp4v"), 30, (1920, 1080))
for i in range(6572):
frame = numpy.zeros((1080, 1920, 3), dtype=numpy.uint8)
cv2.putText(frame, str(i), (500, 500), cv2.FONT_HERSHEY_SIMPLEX, 10, (255, 255, 255), 2)
@kevinlinxc
kevinlinxc / triangulation.py
Created October 1, 2023 05:32
Pygeodesy Testing
# pip install pygeodesy
import pygeodesy.sphericalNvector
def intersection_point(lat_long_1, bearing_1, lat_long_2, bearing_2):
vector1 = pygeodesy.sphericalNvector.LatLon(*lat_long_1)
vector2 = pygeodesy.sphericalNvector.LatLon(*lat_long_2)
return vector1.intersection(bearing_1, vector2, bearing_2)
@kevinlinxc
kevinlinxc / pdfdiff.md
Last active September 10, 2023 04:37
How to diff 2 PDFs

Diffing PDFs

Recently, I wanted to find the textual differences between two PDFS, in the same way that you would compare plain text files with Git/GitHub. I wanted the nice side-by-side view too, not just the Git diff terminal output.

Edit: After writing this gist, I realized that https://www.diffchecker.com/pdf-compare/ pretty much does what I want. This was still a fun experiment though.

I tried a few different ways of extracting the text (Tesseract OCR, Copy and Pasting all the text), but eventually I found the best solution for me was a tool called textract, which uses pdftotext under the hood. This did the best job, as it didn't have weird misread symbols like OCR and it didn't have extra nonsense copied by C/P.

@kevinlinxc
kevinlinxc / rules.txt
Last active September 8, 2023 20:20
SAE AeroDesign Ruleset 2023-2024
2024 Collegiate Design Series
SAE Aero Design Rules
Version 2024.0
i
Forward
Welcome to SAE Aero Design 2024! This competition has been affording students with
relevant aircraft design challenges and real-world engineering experience since 1986. Technical
@kevinlinxc
kevinlinxc / shared_mem_lib.py
Last active October 1, 2023 05:32
Image transfer using shared memory in Python
import numpy as np
from multiprocessing import shared_memory
# Define global dimensions of 1080 video, would need to change this for a different camera resolution
w, h = 1920, 1080
class SharedMemImagePub:
def __init__(self):
# Create a named SharedMemory object