Skip to content

Instantly share code, notes, and snippets.

View SeanSyue's full-sized avatar
🏔️
Nature Lover

Yuchen Xue SeanSyue

🏔️
Nature Lover
View GitHub Profile
@SeanSyue
SeanSyue / numpy_count_occurrence.py
Last active May 20, 2018 15:32
Count the occurrence of certain item in an ndarray in Python. Ref: https://stackoverflow.com/a/28663910/8809592
import numpy as np
a = np.array([0, 3, 0, 1, 0, 1, 2, 1, 0, 0, 0, 0, 1, 3, 4])
unique, counts = numpy.unique(a, return_counts=True)
print(dict(zip(unique, counts)))
#include <opencv2/opencv.hpp>
using namespace cv;
int main() {
Mat img = imread("image.jpg");
namedWindow("test", WINDOW_NORMAL);
imshow("test", img);
waitKey(0);
return 0;
}
@SeanSyue
SeanSyue / sorted_alphanumeric.py
Last active February 19, 2023 20:28
Python list sort alphanumerically.
"""
Since os.listdir returns filenames in an arbitary order,
this function is very handy for generating well-ordered filenames list.
Credit: https://stackoverflow.com/questions/4813061/non-alphanumeric-list-order-from-os-listdir/48030307#48030307
"""
import re
def sorted_alphanumeric(data):
convert = lambda text: int(text) if text.isdigit() else text.lower()
alphanum_key = lambda key: [convert(c) for c in re.split('([0-9]+)', key)]
return sorted(data, key=alphanum_key)
@SeanSyue
SeanSyue / python_switch.py
Created August 12, 2018 02:50
Implement switch functionality in Python.
# https://bytebaker.com/2008/11/03/switch-case-statement-in-python/
def switch_(index_):
def zero():
print("You typed zero.\n")
def sqr():
print("n is a perfect square\n")
@SeanSyue
SeanSyue / first_module.py
Created August 12, 2018 03:05
Example on understanding "__name__ == '__main__'" in Python.
print("This will always be shown first\n")
def main():
print("Calling first_module's main method\n")
if __name__ == '__main__':
# the following will be shown only in `first_module.py`
main()
@SeanSyue
SeanSyue / zip_sorted.py
Created August 12, 2018 07:42
Combine `zip()` and `sorted()` to get a new long sorted list.
list1 = [3, 2, 4, 1, 1]
list2 = ['three', 'two', 'four', 'one', 'one2']
zipped = zip(list1, list2) # zip two lists
sorted_zip = sorted(zipped) # sort
sorted_zip_comb = zip(*sorted_zip) # unzip, then zip again
print(list(sorted_zip_comb))
@SeanSyue
SeanSyue / gen_bin_num.py
Created August 12, 2018 07:50
Example on how to generate binary numbers in Python.
from random import randint
from bitstring import BitArray
# `bin()` function explained
bin30 = bin(30)
print("--> bin30:\n", bin30)
print("--> type(bin30):\n", type(bin30))
print("--> int(bin30, 2):\n", int(bin30, 2))
# generate a long list of random binary numbers
@SeanSyue
SeanSyue / import_doc.py
Created August 12, 2018 07:59
A simple example on importing docstring in Python.
import mymodule
print(mymodule.__doc__)
print(mymodule.f.__doc__)
@SeanSyue
SeanSyue / yolo_gen_train_lst.py
Created August 14, 2018 02:56
Generate training list for darknet yolo training.
from pathlib import Path
for img_file in Path('./JPEGImages').iterdir():
with open('train.txt', 'w') as f:
print(img_file.resolve(), file=f)
@SeanSyue
SeanSyue / chrome_drive_download.py
Created August 24, 2018 04:10
[TEST] Download images from "meitulu" using `selenium`
import urllib.request
import requests
from selenium import webdriver
from selenium.webdriver import ActionChains
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC
from bs4 import BeautifulSoup
import time