Skip to content

Instantly share code, notes, and snippets.

View araffin's full-sized avatar

Antonin RAFFIN araffin

View GitHub Profile
@araffin
araffin / color_mask.py
Created October 6, 2017 15:56
Find the center of a white line in an image using OpenCV
from __future__ import division
import cv2
import numpy as np
# Input Image
image = cv2.imread("my_image.jpg")
# Convert to HSV color space
hsv = cv2.cvtColor(image, cv2.COLOR_RGB2HSV)
# Define range of white color in HSV
lower_white = np.array([0, 0, 212])
@araffin
araffin / image_preprocessing.py
Created October 6, 2017 17:39
Preprocess a RGB image to feed it to a neural net
def preprocessImage(image, width, height):
"""
Preprocessing script to convert image into neural net input array
:param image: (cv2 RBG image)
:param width: (int)
:param height: (int)
:return: (numpy array)
"""
image = cv2.resize(image, (width, height), interpolation=cv2.INTER_LINEAR)
x = image.flatten() # create a big 1D-array
@araffin
araffin / order.h
Last active April 2, 2018 19:22
Order Enum
// From https://github.com/sergionr2/RacingRobot
// replicated in https://github.com/araffin/cpp-arduino-serial
#ifndef ORDER_H
#define ORDER_H
// Define the orders that can be sent and received
enum Order {
HELLO = 0, // Hello order to initiate communication with the Arduino
SERVO = 1, // Command the Servomotor (control the direction of the car)
MOTOR = 2, // Command the Motor (control the speed of the car)
@araffin
araffin / example_serial.cpp
Last active April 2, 2018 19:15
Example Use of Robust Serial in C++
// Extracted from https://github.com/araffin/cpp-arduino-serial/
#include <robust_serial.hpp>
// Open Serial Port
serial_file.open(serial_filename);
// Send the order "MOTOR", i.e. to change the speed of the car
write_order(serial_file, MOTOR);
// with parameter speed=56 (going forward at 56% of the maximum speed)
// The parameter "speed" is encoded as a 8 bits (1 bytes) signed int
@araffin
araffin / example_serial.py
Last active March 3, 2023 16:12
Example Use of Robust Serial in Python
# From https://github.com/araffin/python-arduino-serial
from robust_serial import Order, write_order, write_i8, write_i16
from robust_serial.utils import open_serial_port
# Open serial port with a baudrate of 9600 (bits/s)
serial_file = open_serial_port(baudrate=9600)
# Send the order "MOTOR", i.e. to change the speed of the car
# equivalent to write_i8(serial_file, Order.MOTOR.value)
write_order(serial_file, Order.MOTOR)
@araffin
araffin / example_serial.rs
Last active April 8, 2018 12:18
Example Use of Robust Serial in Rust
// Extracted from https://github.com/araffin/rust-arduino-serial
extern crate robust_arduino_serial;
use robust_arduino_serial::*;
// Open Serial Port
let mut port = serial::open(&serial_port).unwrap();
// Please see the original file to have a complete example
...
// Send the order "MOTOR", i.e. to change the speed of the car
// equivalent to write_i8(&mut port, Order::MOTOR as i8)
@araffin
araffin / classic_serial.cpp
Created April 2, 2018 19:24
Example of common approach to use Arduino Serial
// If we have received data
if (Serial.available() > 0)
{
// Read the order sent by the computer
order_received = Serial.read();
// If the received byte is the character 'a'
if (order_received == 'a')
// This correspond to the action GO_FORWARD
// It will be used later to send speed order to the motor
action = GO_FORWARD;
@araffin
araffin / demo_baselines.py
Last active April 10, 2020 19:13
Getting Started With Stable Baselines
# from https://github.com/hill-a/stable-baselines
import gym
from stable_baselines.common.policies import MlpPolicy
from stable_baselines import PPO2
env = gym.make('CartPole-v1')
model = PPO2(MlpPolicy, env, verbose=1)
# Train the agent
@araffin
araffin / a2c_lunar.py
Last active October 16, 2022 13:53
Training, Saving and Loading an A2C agent
import gym
from stable_baselines import A2C
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import DummyVecEnv
# Create and wrap the environment
env = gym.make('LunarLander-v2')
env = DummyVecEnv([lambda: env])
import gym
import numpy as np
from stable_baselines.common.policies import MlpPolicy
from stable_baselines.common.vec_env import SubprocVecEnv
from stable_baselines.common import set_global_seeds
from stable_baselines import ACKTR
def make_env(env_id, rank, seed=0):
"""