100% encontró este documento útil (1 voto)
183 vistas162 páginas

Presentacion Python Pueacademy

Este documento presenta un taller sobre la introducción al procesamiento de imágenes y la visión artificial con Python. La agenda incluye una introducción a Python, el curso PCAP de programación básica en Python de Cisco Networking Academy y una demostración práctica de procesamiento de imágenes con Python. El objetivo es analizar el contenido y formato del curso PCAP y ver cómo permite formar a los estudiantes en habilidades básicas de programación con Python.
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
100% encontró este documento útil (1 voto)
183 vistas162 páginas

Presentacion Python Pueacademy

Este documento presenta un taller sobre la introducción al procesamiento de imágenes y la visión artificial con Python. La agenda incluye una introducción a Python, el curso PCAP de programación básica en Python de Cisco Networking Academy y una demostración práctica de procesamiento de imágenes con Python. El objetivo es analizar el contenido y formato del curso PCAP y ver cómo permite formar a los estudiantes en habilidades básicas de programación con Python.
Derechos de autor
© © All Rights Reserved
Nos tomamos en serio los derechos de los contenidos. Si sospechas que se trata de tu contenido, reclámalo aquí.
Formatos disponibles
Descarga como PDF, TXT o lee en línea desde Scribd
Está en la página 1/ 162

Python Institute

15:35 a 16:45h

Una introducción práctica al procesamiento de


imágenes y la visión artificial con Python
Jordi Ariño | Software Developer Manager at PUE
PCAP
Programming Essentials
in Python
Una introducción práctica al
procesamiento de imágenes y
la visión artificial con Python
Jordi Ariño
Software Developer Manager at PUE
Agile Software Developer

[email protected]
@jordiAS2K
Introducción
To do Doing Done
Agenda
1.- Introducción
2.- Python en la actualidad
3.- CNA: Portfolio educativo
4.- PCAP: Programming Essentials in Python
5.- PCAP: Demo del curso
6.- Hands-On Labs
Introducción

Este workshop se enmarca dentro de la iniciativa de


colaboración entre Python Institute y el programa Cisco
Networking Academy.

Esta colaboración nace con el objetivo de ofrecer una serie


de recursos docentes, a disposición de los centros Cisco
Academy y sus instructores, para la formación de sus
alumnos en Python.

Entre estos recursos docentes ofrecidos destaca el curso


PCAP: Programming Essentials in Python disponible
dentro de la plataforma NetAcad.
Introducción

Objetivo: Analizar el contenido y formato del curso PCAP


y ver cómo permite formar a los alumnos en las habilidades
necesarias para iniciarse en la programación con Python.

Este curso permite obtener un conjunto de conocimientos


fundamentales y habilidades de programación necesarias
en los campos de uso de Python: IA, Data Science, Machine
Learning, visión por computador, IoT, estadística, …

Como caso práctico realizaremos una sencilla introducción


de cómo utilizar Python en el campo del procesamiento de
imágenes y vídeos dentro de la visión por computador o
visión artificial.
Introducción
Python Institute
Python Institute es un proyecto independiente sin
ánimo de lucro desarrollado por el Open Education and
Development Group (OpenEDG).

Nace con el principal objetivo de promover el lenguaje


de programación Python, y ayudar en la formación de
una nueva generación de programadores.

Ha desarrollado, en colaboración con Pearson VUE, un


programa de certificación independiente y vendor-
neutral en el lenguaje de programación Python.

Iniciativa de colaboración entre Python Institute y el


programa Cisco Networking Academy.
Introducción
Certificación PCAP
Nombre: Certified Associate in Python Programming
Código: PCAP-31-02

Versión: Python 3.x


Nivel: Associate
Pre-requisitos: Ninguno
Duración: 65 min
Preguntas: 40 preguntas
Formato: Tipo test (single/multiple choice)
Passing Score: 70% (28/40)
Idioma: Inglés
Introducción
Certificación PCAP
Exam block #1
Control and Evaluations (25%)

Exam block #2
Data Aggregates (25%)

Exam block #3
Functions and Modules (25%)

Exam block #4
Classes, Objects and Exceptions (25%)
Introducción
What snippet would you insert in the line indicated below:

To print the following string to the monitor after the loop


finishes its execution:

A. print(n)
B. print(n, sep=" ")
C. print(n, end=" ")
D. print(n, " ")
Introducción

Con la inscripción a este workshop habréis recibido acceso,


como alumno, al curso PCAP: Programming Essentials
in Python dentro de la plataforma NetAcad de Cisco.

Objetivo: Poder evaluar el curso y los recursos e-learning


que Cisco pone a disposición de centros y profesores para
la formación de sus alumnos en programación en Python de
forma práctica, oficial y reconocida.

Destacar, como valor añadido, que este curso ayuda a los


estudiantes en su preparación para la certificación oficial:
• PCAP – Python Certified Associate Programmer
Python en la actualidad
To do Doing Done
Agenda
1.- Introducción
2.- Python en la actualidad
3.- CNA: Portfolio educativo
4.- PCAP: Programming Essentials in Python
5.- PCAP: Demo del curso
6.- Hands-On Labs
Python en la actualidad

A día de hoy, Python es uno de los lenguajes de


programación más populares y extendidos, adoptado
en la mayoría de sectores e industrias.

Ha sido nombrado el lenguaje del año 2018 por el índice


TIOBE.

El índice PYPL (PopularitY of Programming Language),


considera a Python el lenguaje más popular y el que ha
experimentado un mayor crecimiento en estos últimos 5
años (17.1%).

La tendencia del mercado laboral muestra que la demanda


de profesionales de Python crece exponencialmente cada
año.
Python en la actualidad
TIOBE Programming Community Index
Python en la actualidad
TIOBE Programming Community Index
Python en la actualidad
TIOBE Programming Community Index
Python en la actualidad
TIOBE Programming Community Index
Python en la actualidad
PYPL PopularitY of Programming Language
Python en la actualidad
PYPL PopularitY of Programming Language
Python en la actualidad
Stack Overflow Trends
Python en la actualidad

¿Qué hace que Python sea tan especial?


• Es fácil de aprender: el tiempo necesario para
aprender Python es más corto que en muchos otros
lenguajes.
• Es fácil de enseñar: la carga de trabajo de enseñanza
es más pequeña que la que necesitan otros lenguajes.
• Es fácil de entender; a menudo, también es más fácil
entender el código de otra persona más rápido si está
escrito en Python.
• Es fácil de obtener, instalar y desplegar: Python es
gratuito, abierto y multiplataforma.
• Ofrece un gran ecosistema de paquetes, librerías y
frameworks
CNA: Porfolio educativo
To do Doing Done
Agenda
1.- Introducción
2.- Python en la actualidad
3.- CNA: Portfolio educativo
4.- PCAP: Programming Essentials in Python
5.- PCAP: Demo del curso
6.- Hands-On Labs
El portafolio educativo Colaborar para generar impacto
de Networking Academy Introduction to
Packet Tracer
Packet Tracer Hackatones Prototyping Lab
Prácticas
laborales

Exploratorio Básico Profesional


Networking Essentials CCNA R&S: Introduction to Networks, R&S
Mobility Fundamentals Essentials, Scaling Networks, Connecting
Redes Talleres de tecnología emergente: Programabilidad
Networks
CCNP R&S: Switch, Route, TShoot
de redes con Cisco APIC-EM*

CCNA Cyber Ops


Seguridad Introduction to Cybersecurity Cybersecurity Essentials
CCNA Security

IoT Fundamentals:
Connecting Things, Big Data & Analytics, IoT
IoT y análisis Introduction to IoT
Security*
Hackathon Playbook

NDG Linux Essentials NDG Linux I


SO y TI NDG Linux Unhatched
IT Essentials NDG Linux II

CLA: Programming Essentials in C


CLP: Advanced Programming in C*
CPA: Programming Essentials in C++
PCAP: Programming Essentials in Python CPP: Advanced Programming in C++
Programación Talleres de tecnología emergente:
Experimentando con API REST utilizando Cisco
Spark*

Laboral Be Your Own Boss Entrepreneurship

Instrucción digital Get Connected


El portafolio educativo Colaborar para generar impacto
de Networking Academy Introduction to
Packet Tracer
Packet Tracer Hackatones Prototyping Lab
Prácticas
laborales

Exploratorio Básico Profesional


Networking Essentials CCNA R&S: Introduction to Networks, R&S
Mobility Fundamentals Essentials, Scaling Networks, Connecting
Redes Talleres de tecnología emergente: Programabilidad
Networks
CCNP R&S: Switch, Route, TShoot
de redes con Cisco APIC-EM*

CCNA Cyber Ops


Seguridad Introduction to Cybersecurity Cybersecurity Essentials
CCNA Security

IoT Fundamentals:
Connecting Things, Big Data & Analytics, IoT
IoT y análisis Introduction to IoT
Security*
Hackathon Playbook

NDG Linux Essentials NDG Linux I


SO y TI NDG Linux Unhatched
IT Essentials NDG Linux II

CLA: Programming Essentials in C


CLP: Advanced Programming in C*
CPA: Programming Essentials in C++
PCAP: Programming Essentials in Python CPP: Advanced Programming in C++
Programación Talleres de tecnología emergente:
Experimentando con API REST utilizando Cisco
Spark*

Laboral Be Your Own Boss Entrepreneurship

Instrucción digital Get Connected


PCAP: Programming
Essentials in Python
To do Doing Done
Agenda
1.- Introducción
2.- Python en la actualidad
3.- CNA: Portfolio educativo
4.- PCAP: Programming Essentials in Python
5.- PCAP: Demo del curso
6.- Hands-On Labs
PCAP: Programming
Essentials in Python
Descripción general del curso
Diseñado para ser un curso fácil de entender y sencillo para
principiantes, centrado en diversos tipos de recopilación de datos,
herramientas de manipulación y operaciones de lógica y bit, y en
la creación de API REST básicas.

Beneficios
Con PCAP: Programming Essentials in Python, aprenderá a diseñar,
escribir, depurar y ejecutar programas codificados en el lenguaje Coordinado con
Python. No se requiere ningún conocimiento previo de programación. la certificación
El curso comienza con una orientación básica paso a paso hasta que Características
se vuelva experto en resolver problemas más complejos.
Público objetivo: estudiantes de secundaria y terciarios
Requisitos previos: ninguno
Componentes educativos
Requiere capacitación a cargo de instructor: no
• 5 módulos de contenido instructivo interactivo Idiomas: inglés
• Más de 30 prácticas de laboratorio incluidas Presentación del curso: con instructor
• Herramienta en línea integrada para realizar prácticas de laboratorio Tiempo estimado para completar el curso: 60 a 70 horas
• Quizzes y Tests de evaluación por módulo y sección Próximo curso recomendado: IoT Fundamentals, Networking
Essentials, NDG Linux Essentials
• Test de evaluación final
36
Programming Essentials in Python
Course Syllabus
El curso está formado por 5 módulos con contenido
interactivo, distribuidos en dos partes o secciones
(Basics e Intermediate).

Introduction
Module 0

Part I: Basics
Module 1: Basics I
Module 2: Basics II
Module 3: Basics II

Part II: Intermediate


Module 4: Intermediate I
Module 5: Intermediate II
Programming Essentials in Python
Edube Sandbox
Herramienta en línea integrada en el propio navegador
web que ofrece un interprete de Python.

https://edube.org/sandbox?language=python
Programming Essentials in Python
Edube Labs
El curso contiene más de 30 prácticas o laboratorios
con soluciones incluidas.
Programming Essentials in Python
Modelo de evaluación
El curso contiene una serie de quizzes de preparación
y tests de evaluación para cada módulo y sección.

Además existe un test de evaluación final.


PCAP: Demo del curso
To do Doing Done
Agenda
1.- Introducción
2.- Python en la actualidad
3.- CNA: Portfolio educativo
4.- PCAP: Programming Essentials in Python
5.- PCAP: Demo del curso
6.- Hands-On Labs
Hands-On Labs
Una introducción práctica al
procesamiento de imágenes y
la visión artificial con Python
To do Doing Done
Agenda
1.- Introducción
2.- Python en la actualidad
3.- CNA: Portfolio educativo
4.- PCAP: Programming Essentials in Python
5.- PCAP: Demo del curso
6.- Hands-On Labs
Área o disciplina dentro de la
Conseguir ITlas
que quemáquinas
incluye imiten
métodoselpara adquirir,
sistema procesar,
de visión analizar y comprender
humano
Resolverlas
el imágenes
problema del mundo real con
de interpretar el finautomática
de forma de obtenerelinformación a partir
contenido de de ellas
las imágenes y vídeos
Hands-On Labs > pip install opencv-contrib-python
Collecting opencv-contrib-python
Using cached
https://files.pythonhosted.org/packages/aa/4c/a3125e25da4b121b4077
c575eee8a691ffabb5d97afc5ae832fd5a7015e3/opencv_contrib_python-
4.1.0.25-cp37-cp37m-win32.whl
Collecting numpy>=1.14.5 (from opencv-contrib-python)
Using cached
https://files.pythonhosted.org/packages/ab/75/9ac63977cbca68e17406
a53a8c573a925a16771800be47a73f18c838f3fb/numpy-1.16.3-cp37-cp37m-
win32.whl
Installing collected packages: numpy, opencv-contrib-python
Successfully installed numpy-1.16.3 opencv-contrib-python-4.1.0.25

> pip list


Package Version
--------------------- --------
numpy 1.16.3
opencv-contrib-python 4.1.0.25
pip 19.1
setuptools 40.8.0

https://pypi.org/project/opencv-contrib-python/
load_image.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Image Visualizer"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the image using OpenCV


# The cv2.imread function returns a NumPy array representing the image
image = cv2.imread(args["image"])

# Show image info


print(f"Width: {image.shape[1]} pixels")
print(f"Height: {image.shape[0]} pixels")
print(f"Channels: {image.shape[2]}")
load_image.py
Hands-On Labs # Display the image to our screen
cv2.imshow(windowName, image)

# Note: OpenCV stores RGB channels in reverse order.


# While we normally think in terms of Red, Green and Blue;
# OpenCV stores them in the following order: Blue, Green and Red
# Get the pixel located at (0,0) represented as a tuple (b,g,r)
copy = image.copy()
(b, g, r) = copy[0, 0]
print(f"Pixel at (0,0) - RGB: ({r}, {g}, {b})")

# Modify the pixel located at (0,0)


copy[0, 0] = (0,0,255)
(b, g, r) = copy[0, 0]
print(f"Pixel at (0,0) - RGB: ({r}, {g}, {b})")

# Use NumPy array slicing capabilities to access larger rectangular


# portions of image
copy [0:100, 0:100] = (0,0,0)

# Write our image to disk


cv2.imwrite("images/demo.jpg", copy)

# Wait for a key press to finish program


cv2.waitKey(0)

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
load_video.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required = True,
help = "path to where the video resides")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Video Visualizer"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the video using OpenCV


video = cv2.VideoCapture(args["video"])
load_video.py
Hands-On Labs # Display the video to our screen
# Start to lopping over all frames in the video.
# At most basic level, a video is simply a sequence of images together
while video.isOpened():
(grabbed, frame) = video.read()

# Display the output


cv2.imshow(windowName, frame)

# Wait for a key press to finish program


key = cv2.waitKey(1)
if (key in [ord("q"), 27] or
cv2.getWindowProperty(windowName,cv2.WND_PROP_VISIBLE) < 1) :
break

# The reference to the video is released


video.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
load_cam.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-m", "--mirror", required = False,
action='store_true', help = "enable mirror mode")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Webcam Visualizer"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the webcam image using OpenCV


cam = cv2.VideoCapture(0)
load_cam.py
Hands-On Labs # Display the webcam to our screen
# Start to lopping over all frames in the webcam.
# At most basic level, a video is simply a sequence of images together
while True:
(grabbed, frame) = cam.read()

# Mirror effect: Flip on Y axis


if (args["mirror"]):
frame = cv2.flip(frame,1)

# Display the output


cv2.imshow(windowName, frame)

# Wait for a key press to finish program


key = cv2.waitKey(1)
if (key in [ord("q"), 27] or
cv2.getWindowProperty(windowName,cv2.WND_PROP_VISIBLE) < 1) :
break

# The reference to the video is released


cam.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
cam_capture.py
Hands-On Labs # Import the necessary packages
import time
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-m", "--mirror", required = False,
action='store_true', help = "enable mirror mode")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Webcam Visualizer"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the webcam image using OpenCV


cam = cv2.VideoCapture(0)
cam_capture.py
Hands-On Labs # Display the webcam to our screen
# Start to lopping over all frames in the webcam.
# At most basic level, a video is simply a sequence of images together
while True:
(grabbed, frame) = cam.read()

# Mirror effect: Flip on Y axis


if (args["mirror"]):
frame = cv2.flip(frame,1)

# Display the output


cv2.imshow(windowName, frame)

# Wait for a key press to capture a frame or finish the program


key = cv2.waitKey(1)
if (key == ord("p")) :
filename = time.strftime("%Y-%m-%d-%H-%M-%S")
cv2.imwrite(f"captures/{filename}.png", frame)
elif (key in [ord("q"), 27] or
cv2.getWindowProperty(windowName,cv2.WND_PROP_VISIBLE) < 1) :
break

# The reference to the video is released


cam.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
Hands-On Labs Detección de objetos
Hands-On Labs Sliding Window + Image Pyramid
Hands-On Labs Sliding Window + Image Pyramid
Hands-On Labs Sliding Window + Image Pyramid
img_faces_detection.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-fc", "--fclassifier", required = False,
default="classifiers/haarcascade_frontalface.xml",
help = "path to where the face cascade resides")
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Faces Detector"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the image using OpenCV


image = cv2.imread(args["image"])

# Convert image to grayscale


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_faces_detection.py
Hands-On Labs # In order to build face recognition system, we use the built-in
# Haar cascade classifiers in OpenCV.
# https://github.com/opencv/opencvdata/haarcascades
# This folder contains trained classifiers for detecting objects of a
# particular type, e.g. faces (frontal, profile), pedestrians etc
facesClassifier = cv2.CascadeClassifier(args["fclassifier"])

# “sliding window” approach


# These classifiers work by scanning an image from left to right, and
# top to bottom, at varying scale sizes.
faces = facesClassifier.detectMultiScale(gray, scaleFactor = 1.3,
minNeighbors = 5, minSize = (30,30))

print(f"I found {len(faces)} face(s)")

# loop over the faces and draw a rectangle surrounding each


for (i, (x, y, w, h)) in enumerate(faces):
cv2.rectangle(image, pt1 = (x, y), pt2 = (x + w, y + h), …)
cv2.putText(image, text = f"Face #{i}", org = (x, y - 10), …)

# Display the image to our screen


cv2.imshow(windowName, image)

# Wait for a key press to finish program


cv2.waitKey(0)

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
img_cats_detection.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-cc", "--cclassifier", required = False,
default="classifiers\\haarcascade_frontalcatface.xml",
help = "path to where the cat cascade resides")
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Cats Detector"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the image using OpenCV


image = cv2.imread(args["image"])

# Convert image to grayscale


gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
img_cats_detection.py
Hands-On Labs # In order to build cats recognition system, we use the built-in
# Haar cascade classifiers in OpenCV.
# https://github.com/opencv/opencvdata/haarcascades
# This folder contains trained classifiers for detecting objects of a
# particular type, e.g. faces (frontal, profile), pedestrians etc
catsClassifier = cv2.CascadeClassifier(args["cclassifier"])

# “sliding window” approach


# These classifiers work by scanning an image from left to right, and
# top to bottom, at varying scale sizes.
cats = catsClassifier.detectMultiScale(gray, scaleFactor = 1.05,
minNeighbors = 4, minSize = (30,30))
print(f"I found {len(cats)} cats(s)")

# loop over the cat faces and draw a rectangle surrounding each
for (i, (x, y, w, h)) in enumerate(cats):
cv2.rectangle(image, pt1 = (x, y), pt2 = (x + w, y + h), …)
cv2.putText(image, text = f"Face #{i}", org = (x, y - 10), …)

# Display the image to our screen


cv2.imshow(windowName, image)

# Wait for a key press to finish program


cv2.waitKey(0)

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
video_faces_detection.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-fc", "--fclassifier", required = False,
default="classifiers/haarcascade_frontalface_default.xml",
help = "path to where the face cascade resides")
ap.add_argument("-v", "--video", required = False,
help = "path to where the video resides")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Video: Cam Detector“
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the video using OpenCV


video = cv2.VideoCapture(args["video"])

# Face recognition system: built-in Haar cascade


facesClassifier = cv2.CascadeClassifier(args["fclassifier"])
video_faces_detection.py
Hands-On Labs # Start to lopping over all frames in the video.
# At most basic level, a video is simply a sequence of images
while video.isOpened():
# Read next frame
(grabbed, frame) = video.read()

# Convert frame to grayscale


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detecting the actual faces in the frame


faces = facesClassifier.detectMultiScale(gray, scaleFactor =
1.1, minNeighbors = 5, minSize = (30,30))

# loop over the faces and draw a rectangle surrounding each


for (x, y, w, h) in faces:
cv2.rectangle(frame, pt1 = (x, y), pt2 = (x + w, y + h),
color = (0, 0, 255), thickness = 2)

# Display the output


cv2.imshow(windowName, frame)

# Wait for a key press to finish program

# The reference to the video is released


video.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
cam_faces_detection.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-fc", "--fclassifier", required = False,
default="classifiers/haarcascade_frontalface.xml",
help = "path to where the face cascade resides")
ap.add_argument("-m", "--mirror", required = False,
action='store_true', help = "enable mirror mode")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode")
args = vars(ap.parse_args())

# Configure windows
windowName = "Webcam: Face Detector"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)

# Loading the video (webcam) using OpenCV


cam = cv2.VideoCapture(0)

# Face recognition system: built-in Haar cascade


facesClassifier = cv2.CascadeClassifier(args["fclassifier"])
cam_faces_detection.py
Hands-On Labs # Start to lopping over all frames in the webcam.
# At most basic level, a video is simply a sequence of images
while True:
# Read next frame
(grabbed, frame) = cam.read()

# Mirror effect: Flip on Y axis


if (args["mirror"]): frame = cv2.flip(frame,1)
# Convert frame to grayscale
gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

# Detecting the actual faces in the frame


faces = facesClassifier.detectMultiScale(gray, scaleFactor =
1.1, minNeighbors = 5, minSize = (30,30))

# loop over the faces and draw a rectangle surrounding each


for (x, y, w, h) in faces:
cv2.rectangle(frame, pt1 = (x, y), pt2 = (x + w, y + h),
color = (0, 0, 255), thickness = 2)

# Display the output


cv2.imshow(windowName, frame)

# The reference to the video is released


cam.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
eyes_tracking.py
Hands-On Labs # Import the necessary packages
import argparse
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-fc", "--fclassifier", required = False,
default="classifiers/haarcascade_frontalface_default.xml",
help = "path to where the face cascade resides (optional)")
ap.add_argument("-ec", "--eclassifier", required = False,
default="classifiers/haarcascade_eye.xml",
help = "path to where the eyes cascade resides (optional)")
ap.add_argument("-v", "--video", required = False,
help = "path to where the video resides (optional)")
ap.add_argument("-m", "--mirror", required = False,
action='store_true', help = "enable mirror mode (optional)")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode (optional)")
args = vars(ap.parse_args())

# Configure windows
windowName = "Eyes Tracking"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
eyes_tracking.py
Hands-On Labs # If --video is not supplied we will read video from the webcam.
# Otherwise, open the video file pointed by the --video argument
if (args["video"]):
video = cv2.VideoCapture(args["video"])
else:
video = cv2.VideoCapture(0)

# In order to build face and eyes recognition system, we use the built-
in Haar cascade classifiers in OpenCV.
facesClassifier = cv2.CascadeClassifier(args["fclassifier"])
eyesClassifier = cv2.CascadeClassifier(args["eclassifier"])

# Display the video or webcam to our screen


while True:
(grabbed, frame) = video.read()

# Detect if video finished


if (args["video"] and not grabbed): break

# Mirror effect on webcam: Flip on Y axis


if (not args["video"] and args["mirror"]):
frame = cv2.flip(frame,1)

# Convert frame to grayscale


gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
eyes_tracking.py
Hands-On Labs # Detecting the actual faces in the frame
faces = facesClassifier.detectMultiScale(gray,
scaleFactor = 1.1, minNeighbors = 5, minSize = (30,30))

# loop over the faces


for (fx, fy, fw, fh) in faces:
# Draw a rectangle surrounding each
cv2.rectangle(frame, pt1 = (fx, fy), …)

# Extract the face region of interest (ROI)


faceROI = gray[fy:fy + fh, fx:fx + fw]

# Detecting the actual eyes in the frame


eyes = eyesClassifier.detectMultiScale(faceROI,
scaleFactor = 1.1, minNeighbors = 10, minSize = (20,20))

# loop over the eyes and draw


for (ex,ey,ew,eh) in eyes:
cv2.rectangle(frame, pt1 = (fx + ex, fy + ey), …)

# Display the output


cv2.imshow(windowName, frame)

# The reference to the video is released


video.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
obj_tracking.py
Hands-On Labs # Import the necessary packages
import argparse
import numpy as np
import time
import cv2

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required = False,
help = "path to where the video resides (optional)")
ap.add_argument("-t", "--threshold", required = False,
action='store_true', help = "show threshold images (optional)")
ap.add_argument("-m", "--mirror", required = False,action='store_true’,
help = "enable mirror mode (optional)")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode (optional)")
args = vars(ap.parse_args())

# Configure windows
windowName = "Object Tracking"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
obj_tracking.py
Hands-On Labs # The object that will be tracking is a "yellow" object.
colorLower = (20, 100, 100)
colorUpper = (30, 255, 255)

# If --video is not supplied we will read video from the webcam.


# Otherwise, open the video file pointed by the --video argument
if (args["video"]):
video = cv2.VideoCapture(args["video"])
else:
video = cv2.VideoCapture(0)

# Display the video or webcam to our screen


while True:
(grabbed, frame) = video.read()

# Detect if video finished


if (args["video"] and not grabbed): break

# Mirror effect on webcam: Flip on Y axis


if (not args["video"] and args["mirror"]):
frame = cv2.flip(frame,1)

# Find shades of the yellow in the frame using the cv2.inRange


# The result is a thresholded image with pixels falling within
# the upper and lower set to white and pixels that do not fall
# into this range se as black
threshold = cv2.inRange(frame, colorLower, colorUpper)
threshold = cv2.GaussianBlur(threshold, (3,3), 0)
obj_tracking.py
Hands-On Labs # Display threshold images
if (args["threshold"]): cv2.imshow("threshold", threshold)

# Now that we have the thresholded image, we needs to find the


# largest contour in the image
(contours, h) = cv2.findContours(threshold.copy(),
mode = cv2.RETR_EXTERNAL, method = cv2.CHAIN_APPROX_SIMPLE)
if (len(contours) > 0) :
contour = sorted(contours, key = cv2.contourArea,
reverse = True)[0]

# To draw the contours, cv2.drawContours function is used.


rect = np.int32(cv2.boxPoints(cv2.minAreaRect(contour)))
cv2.drawContours(frame, contours = [rect],
contourIdx = -1, color = (0, 255, 0), thickness = 2)

cv2.imshow(windowName, frame)

# Wait for a key press to finish program


key = cv2.waitKey(1)
if (key in [ord("q"), 27] or …

# The reference to the video is released


video.release()

# Any open window created by OpenCV are closed


cv2.destroyAllWindows()
multi_object_tracking.py
Hands-On Labs # import the necessary packages
import argparse
import cv2

# construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-v", "--video", required = False,
help = "path to where the video resides (optional)")
ap.add_argument("-m", "--mirror", required = False,
action='store_true', help = "enable mirror mode (optional)")
ap.add_argument("-f", "--full", required = False, action='store_true’,
help = "use full screen mode (optional)")
ap.add_argument("-t", "--tracker", type=str, required = False,
default="csrt", help="OpenCV object tracker type (oprtional)")
args = vars(ap.parse_args())

# Configure windows
windowName = "Multi Tracking"
if (args["full"]):
cv2.namedWindow(windowName, cv2.WND_PROP_FULLSCREEN)
cv2.setWindowProperty(windowName, cv2.WND_PROP_FULLSCREEN,
cv2.WINDOW_FULLSCREEN)
multi_object_tracking.py
Hands-On Labs # Initialize a dictionary that maps strings to their corresponding
# OpenCV object tracker implementations
# I recommend the following three algorithms:
# KCF: Fast and accurate
# CSRT: More accurate than KCF but slower
# MOSSE: Extremely fast but not as accurate as either KCF or CSRT
OPENCV_OBJECT_TRACKERS = {
"csrt": cv2.TrackerCSRT_create,
"kcf": cv2.TrackerKCF_create,
"boosting": cv2.TrackerBoosting_create,
"mil": cv2.TrackerMIL_create,
"tld": cv2.TrackerTLD_create,
"medianflow": cv2.TrackerMedianFlow_create,
"mosse": cv2.TrackerMOSSE_create
}

# Initialize OpenCV's special multi-object tracker


# The class allows us to:
# Add new object trackers to the MultiTracker
# Update all object trackers inside the MultiTracker
trackers = cv2.MultiTracker_create()

# If --video is not supplied we will read video from the webcam.


if (args["video"]):
video = cv2.VideoCapture(args["video"])
else:
video = cv2.VideoCapture(0)
multi_object_tracking.py
Hands-On Labs # loop over frames from the video stream
while True:
# grab the current frame, then handle if we are using a
# VideoStream or VideoCapture object
(grabbed, frame) = video.read()

# Detect if video finished


if (args["video"] and not grabbed): break

# Mirror effect on webcam: Flip on Y axis


if (not args["video"] and args["mirror"]):
frame = cv2.flip(frame,1)

# grab the updated bounding box coordinates (if any) for each
# object that is being tracked
# The update method will locate the object’s new position and
# return a success boolean
# and the bounding box of the object.
(success, boxes) = trackers.update(frame)

# loop over the bounding boxes and draw then on the frame
for box in boxes:
(x, y, w, h) = [int(v) for v in box]
cv2.rectangle(frame, (x, y), (x + w, y + h), (0, 255, 0), 2)

# show the output frame


cv2.imshow(windowName, frame)
multi_object_tracking.py
Hands-On Labs key = cv2.waitKey(1)

# if the 's' key is selected, we are going to "select" a


# bounding box to track
if key == ord("s"):

# select the bounding box of the object we want to track (make


# sure you press ENTER or SPACE after selecting the ROI)
box = cv2.selectROI(windowName, frame, fromCenter=False,
showCrosshair=True)

# create a new object tracker for the bounding box and add it
# to our multi-object tracker
tracker = OPENCV_OBJECT_TRACKERS[args["tracker"]]()
trackers.add(tracker, frame, box)

# if the `q` key was pressed, break from the loop


elif (key in [ord("q"), 27] or …):
break

video.release()

# close all windows


cv2.destroyAllWindows()
Hands-On Labs
Hands-On Labs > pip install google-cloud-vision
Collecting google-cloud-vision
Downloading
https://files.pythonhosted.org/packages/f2/bf/112a0707a42596151669
3ac526725bc3f51db44fc3d02998da3ee2b82ef1/google_cloud_vision-
0.36.0-py2.py3-none-any.whl (383kB)
|████████████████████████████████| 389kB 1.6MB/s
Collecting google-api-core[grpc]<2.0.0dev,>=1.6.0 (from google-
cloud-vision)
Downloading
https://files.pythonhosted.org/packages/bf/e4/b22222bb714947eb459d
c91ebf95131812126a0b29d62e444be3f76dad64/google_api_core-1.9.0-
py2.py3-none-any.whl (65kB)
|████████████████████████████████| 71kB 2.3MB/s
Collecting google-auth<2.0dev,>=0.4.0 (from google-api-
core[grpc]<2.0.0dev,>=1.6.0->google-cloud-vision)

Successfully installed cachetools-3.1.0 certifi-2019.3.9 chardet-
3.0.4 google-api-core-1.9.0 google-auth-1.6.3 google-cloud-vision-
0.36.0 googleapis-common-protos-1.5.9 grpcio-1.20.1 idna-2.8
protobuf-3.7.1 pyasn1-0.4.5 pyasn1-modules-0.2.5 pytz-2019.1
requests-2.21.0 rsa-4.0 six-1.12.0 urllib3-1.24.2

https://pypi.org/project/google-cloud-vision/
Hands-On Labs > pip list
Package Version
------------------------ --------
cachetools 3.1.0
certifi 2019.3.9
chardet 3.0.4
google-api-core 1.9.0
google-auth 1.6.3
google-cloud-vision 0.36.0
googleapis-common-protos 1.5.9
grpcio 1.20.1
idna 2.8
numpy 1.16.3
opencv-contrib-python 4.1.0.25
pip 19.1
protobuf 3.7.1
pyasn1 0.4.5
pyasn1-modules 0.2.5
pytz 2019.1
requests 2.21.0

https://pypi.org/project/google-cloud-vision/
logo_detection.py
Hands-On Labs # Import the necessary packages
import io
import os
from google.cloud import vision
from google.cloud.vision import types
import argparse

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
args = vars(ap.parse_args())

# Authenticate API requests


os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials.json“

# The name of the image file to annotate


filename = os.path.join(os.path.dirname(__file__), args["image"])

# Loads the image into memory


with io.open(filename, 'rb') as file:
content = file.read()
logo_detection.py
Hands-On Labs # Instantiates a client
client = vision.ImageAnnotatorClient()
image = types.Image(content = content)

# Performs object localization on the image file


response = client.logo_detection(image)

# Process the response


logos = response.logo_annotations

# Print JSON response


print(logos)

print(f'Number of logos found: {len(logos)}')


print("------------------------------")
for logo in logos:
print(f'Logo: {logo.description}’)
print(f'Score: {logo.score}’)
print("------------------------------")
logo_detection.py
Hands-On Labs
logo_detection.py
Hands-On Labs
Number of logos found: 1
------------------------------
Logo: google
Score: 0.9927250146865845
------------------------------

{
mid: "/m/045c7b"
description: "google"
score: 0.9927250146865845
bounding_poly {
vertices {
}
vertices {
x: 759
}
vertices {
x: 759
y: 439
}
vertices {
y: 439
}
}
}
logo_detection.py
Hands-On Labs
logo_detection.py
Hands-On Labs
Number of logos found: 1
------------------------------
Logo: mcdonalds
Score: 0.9921749830245972
------------------------------

{
mid: "/m/07gyp7"
description: "mcdonalds"
score: 0.9921749830245972
bounding_poly {
vertices {
}
vertices {
x: 923
}
vertices {
x: 923
y: 600
}
vertices {
y: 600
}
}
}
object_detection.py
Hands-On Labs # Import the necessary packages
import io
import os
from google.cloud import vision
from google.cloud.vision import types
import argparse

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
args = vars(ap.parse_args())

# Authenticate API requests


os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials.json“

# The name of the image file to annotate


filename = os.path.join(os.path.dirname(__file__), args["image"])

# Loads the image into memory


with io.open(filename, 'rb') as file:
content = file.read()
object_detection.py
Hands-On Labs # Instantiates a client
client = vision.ImageAnnotatorClient()
image = types.Image(content = content)

# Performs object localization on the image file


response = client.object_localization(image)

# Process the response


objects = response.localized_object_annotations

# Print JSON response


print(objects)

print(f'Number of objects found: {len(objects)}')


for obj in objects:
print(f'{obj.name} (confidence: {obj.score})’)
print('Normalized bounding polygon vertices: ‘)
for vertex in obj.bounding_poly.normalized_vertices:
print(f' - ({vertex.x}, {vertex.y})')
object_detection.py
Hands-On Labs

Number of objects found: 5


Bird (confidence: 0.9233673810958862)
Duck (confidence: 0.8315161466598511)
Animal (confidence: 0.7112061977386475)
Wheel (confidence: 0.5616657733917236)
Toy (confidence: 0.5473584532737732)
text_detection.py
Hands-On Labs # Import the necessary packages
import io
import os
from google.cloud import vision
from google.cloud.vision import types
import argparse

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
args = vars(ap.parse_args())

# Authenticate API requests


os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials.json“

# The name of the image file to annotate


filename = os.path.join(os.path.dirname(__file__), args["image"])

# Loads the image into memory


with io.open(filename, 'rb') as file:
content = file.read()
text_detection.py
Hands-On Labs # Instantiates a client
client = vision.ImageAnnotatorClient()
image = types.Image(content = content)

# Performs object localization on the image file


response = client.text_detection(image)

# Process the response


texts = response.text_annotations

# Print JSON response


print(texts)

print(f'Number of text fragments found: {len(texts)}’)


for text in texts:
print("------------------------------")
print(f'{text.description}’)
print("------------------------------")
text_detection.py
Hands-On Labs

Number of text fragments found: 3


------------------------------------------
3685 HDP
------------------------------------------
3685
------------------------------------------
HDP
------------------------------------------
face_detection.py
Hands-On Labs # Import the necessary packages
import io
import os
from google.cloud import vision
from google.cloud.vision import types
import argparse

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
args = vars(ap.parse_args())

# Authenticate API requests


os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials.json“

# The name of the image file to annotate


filename = os.path.join(os.path.dirname(__file__), args["image"])

# Loads the image into memory


with io.open(filename, 'rb') as file:
content = file.read()
face_detection.py
Hands-On Labs # Instantiates a client
client = vision.ImageAnnotatorClient()
image = types.Image(content = content)

# Performs object localization on the image file


response = client.face_detection(image)

# Process the response


faces = response.face_annotations

# Print JSON response


print(faces)

print(f'Number of faces found: {len(faces)}’)


for face in faces:
print("-------------------------------------------------")
print(f"Confidence: {face.detection_confidence}")
print(f"Alegre: {face.joy_likelihood}")
print(f"Triste: {face.sorrow_likelihood}")
print(f"Enfadado: {face.anger_likelihood}")
print(f"Sorprendido: {face.surprise_likelihood}")
print(f"Borroso: {face.blurred_likelihood}")
print(f"Con sombrero: {face.headwear_likelihood}")
print("-------------------------------------------------")
face_detection.py
Hands-On Labs ------------------------------------------
Confidence: 0.9987232685089111
Alegre: 1
Triste: 2
Enfadado: 1
Sorprendido: 1
Borroso: 1
Con sombrero: 1
------------------------------------------
------------------------------------------
Confidence: 0.9991716146469116
Alegre: 5
Triste: 1
Enfadado: 1
Sorprendido: 1
Borroso: 1
Con sombrero: 1
------------------------------------------
------------------------------------------
Confidence: 0.999671459197998
Alegre: 1
Triste: 1
Enfadado: 1
Sorprendido: 5
Borroso: 1
Con sombrero: 1
------------------------------------------
landmark_detection.py
Hands-On Labs # Import the necessary packages
import io
import os
from google.cloud import vision
from google.cloud.vision import types
import argparse

# Construct the argument parser and parse the arguments


ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", required = True,
help = "path to where the image file resides")
args = vars(ap.parse_args())

# Authenticate API requests


os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = "credentials.json“

# The name of the image file to annotate


filename = os.path.join(os.path.dirname(__file__), args["image"])

# Loads the image into memory


with io.open(filename, 'rb') as file:
content = file.read()
landmark_detection.py
Hands-On Labs # Instantiates a client
client = vision.ImageAnnotatorClient()
image = types.Image(content = content)

# Performs object localization on the image file


response = client.landmark_detection(image)

# Process the response


landmarks = response.landmark_annotations

# Print JSON response


print(landmarks)

print(f'Number of landmarks found: {len(landmarks)}’)


for landmark in landmarks:
print("-------------------------------------------------")
print(f“Description: {landmark.description}")
print(f“Score: {landmark.score}")
print("-------------------------------------------------")
landmark_detection.py
Hands-On Labs

-------------------------------------------------
Description: Plaza de Cibeles
Score: 0.9911772012710571
-------------------------------------------------
landmark_detection.py
Hands-On Labs

-------------------------------------------------
Description: Puerta del Sol, Post Office
Score: 0.5609591007232666
-------------------------------------------------
¡Muchas gracias!
To do Doing Done

[email protected]
@jordiAS2K

#PUEAcademyDay19
¿Alguna pregunta?
www.pue.es/cisco
[email protected]

#PUEAcademyDay19

También podría gustarte