Skip to content

Instantly share code, notes, and snippets.

View vittorio-nardone's full-sized avatar

Vittorio Nardone vittorio-nardone

View GitHub Profile
import cv2, queue, threading, time
import boto3
from botocore.exceptions import ClientError
import io
import RPi.GPIO as GPIO
import imutils
# Video streaming URL
webcam_url = 'rtsp://user:pwd@192.168.1.1:554/11'
@vittorio-nardone
vittorio-nardone / object_detection.py
Last active January 17, 2020 09:30
Object detection sample in Python
cap = VideoCapture(webcam_url)
model = cv2.dnn.readNetFromTensorflow('models/frozen_inference_graph.pb',
'models/ssd_mobilenet_v2_coco_2018_03_29.pbtxt')
# Get a frame
frame = cap.read()
# Submit it to model
model.setInput(cv2.dnn.blobFromImage(frame, size=(300, 300), swapRB=True))
output = model.forward()
@vittorio-nardone
vittorio-nardone / find_contours.py
Last active January 17, 2020 11:25
Find contours in pyhton
# Find rectangles in image and return a list of cropped images.
def extract_rectangles(image):
result = []
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.bilateralFilter(gray, 11, 17, 17)
edged = cv2.Canny(gray, 30, 200)
cnts = cv2.findContours(edged.copy(), cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
@vittorio-nardone
vittorio-nardone / text_detection.py
Created January 17, 2020 09:27
Text detection in Python and AWS Rekognition
def detect_text(photo, bucket):
"""Detect text in an image hosted as bucket object
:param phone: Object name
:param bucket: Bucket name
:return:
"""
client=boto3.client('rekognition')
@vittorio-nardone
vittorio-nardone / main.yml
Last active February 11, 2020 11:53
Ansible role to install Apache on Ubuntu
---
- name: Install apache2 packages
apt:
name: apache2
state: present
- name: Ensure Apache is running
service:
name: apache2
state: started
enabled: true
@vittorio-nardone
vittorio-nardone / test_default.py
Created February 11, 2020 12:11
Molecule test for service Apache2
import os
import pytest
import testinfra.utils.ansible_runner
testinfra_hosts = testinfra.utils.ansible_runner.AnsibleRunner(
os.environ['MOLECULE_INVENTORY_FILE']
).get_hosts('all')
@vittorio-nardone
vittorio-nardone / .travis.yml
Last active February 11, 2020 13:47
TravisCI configuration to test an Ansible role on AWS EC2
sudo: required
language: python
python:
- '3.6'
install:
- pip install -r test-requirements.txt
env:
global:
- EC2_REGION=eu-west-1
script:
@vittorio-nardone
vittorio-nardone / request-cert.py
Created May 4, 2020 14:11
Request a new cert for specified domain, hosted in a public access S3 bucket
def request_certs(emails, domains):
''' Request a new cert for specified domain, hosted in a public access S3 bucket.
'auth-hook.py' script is used in validation, to upload Certbot token to the bucket
'cleanup-hook.py' script is used after validation to remove token file in bucket
'''
certbot_args = [
# Override directory paths to use /tmp folder
'--config-dir', '/tmp/certbot/config',
'--work-dir', '/tmp/certbot/work',
'--logs-dir', '/tmp/certbot/logs',
@vittorio-nardone
vittorio-nardone / update_symlinks.py
Created May 4, 2020 14:13
The dark side of the symlinks, required by Certbot
@vittorio-nardone
vittorio-nardone / upload_auth.py
Created May 4, 2020 14:14
Write token file and upload it to correct S3 bucket
def upload_auth():
''' Write token file and upload it to correct S3 bucket and folder
S3 Bucket name and cert domain must be the same!
'''
f = open("/tmp/verification.txt", "w")
f.write(os.environ['CERTBOT_VALIDATION'])
f.close()
s3object = ".well-known/acme-challenge/" + os.environ['CERTBOT_TOKEN']
upload_file("/tmp/verification.txt", os.environ['CERTBOT_DOMAIN'], s3object)