Skip to content

Instantly share code, notes, and snippets.

View Breta01's full-sized avatar
🤹
All over the place

Bretislav Hajek Breta01

🤹
All over the place
View GitHub Profile
# Import necessary libraries
import numpy as np
import matplotlib.pyplot as plt
import cv2
# Load image and convert it from BGR to RGB
image = cv2.cvtColor(cv2.imread("folder/imageName.jpg"), cv2.COLOR_BGR2RGB)
def resize(img, height=800):
""" Resize image to given height """
rat = height / img.shape[0]
return cv2.resize(img, (int(rat * img.shape[1]), height))
# Resize and convert to grayscale
img = cv2.cvtColor(resize(image), cv2.COLOR_BGR2GRAY)
# Bilateral filter preserv edges
img = cv2.bilateralFilter(img, 9, 75, 75)
# Getting contours
im2, contours, hierarchy = cv2.findContours(edges, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
# Finding contour of biggest rectangle
# Otherwise return corners of original image
# Don't forget on our 5px border!
height = edges.shape[0]
width = edges.shape[1]
MAX_COUNTOUR_AREA = (width - 10) * (height - 10)
def fourCornersSort(pts):
""" Sort corners: top-left, bot-left, bot-right, top-right """
# Difference and sum of x and y value
# Inspired by http://www.pyimagesearch.com
diff = np.diff(pts, axis=1)
summ = pts.sum(axis=1)
# Top-left point has smallest sum...
# np.argmin() returns INDEX of min
return np.array([pts[np.argmin(summ)],
@Breta01
Breta01 / Interact_from_ipywidgets.ipynb
Last active July 27, 2019 12:42
Example of using interact from ipywidgets
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
@Breta01
Breta01 / display_widget.py
Last active July 2, 2017 16:51
Display only one widget
import ipywidgets as widgets
# Import display function
from IPython.display import display
# Create costume widget
w = widgets.IntProgress(value = 50, description='Loading:')
# Display widget
display(w)
@Breta01
Breta01 / ipywidgets_event_handlers.py
Created February 23, 2017 19:49
Handling click on button widget
from IPython.display import display, clear_output
# Creating and Displaying button
button = widgets.Button(description="Button Text!")
display(button)
# Callback function with button parameter
def clicked(b):
clear_output()
print("Result of button click: ")
@Breta01
Breta01 / android_jupyter_install.sh
Last active December 19, 2018 08:59
Run clear GNURoot Debian, download android_jupyter_install.sh and run command: sh android_jupyter_install.sh
#!/bin/bash
# Update
apt-get update && apt-get upgrade -y
# Install python+packages and curl
apt-get install -y python3 python3-pip python3-numpy python3-scipy python3-matplotlib ipython3 ipython3-notebook python3-pandas python3-nose curl wget
# Update pip
python3 -m easy_install -U pip
# Correctinng python file
# which causes stuck during package installation
@Breta01
Breta01 / SavingModelTF.py
Last active January 9, 2020 14:30
Code for creating, training and saving TensorFlow model.
import tensorflow as tf
### Linear Regression ###
# Input placeholders
x = tf.placeholder(tf.float32, name='x')
y = tf.placeholder(tf.float32, name='y')
# Model parameters
W1 = tf.Variable([0.1], tf.float32)
W2 = tf.Variable([0.1], tf.float32)
W3 = tf.Variable([0.1], tf.float32)
b = tf.Variable([0.1], tf.float32)
@Breta01
Breta01 / Importing TF model.py
Last active March 6, 2018 21:14
Importing and using TensorFlow graph (model)
sess = tf.Session()
# Import graph from the path and recover session
saver = tf.train.import_meta_graph('models/model_name.meta', clear_devices=True)
saver.restore(sess, 'models/model_name')
# There are TWO options how to access the operation (choose one)
# FROM SAVED COLLECTION:
activation = tf.get_collection('activation')[0]
# BY NAME: