#!/usr/bin/python
# -*- coding: utf-8 -*-

# Programa simples com camera webcam e opencv

import cv2
import os,sys, os.path
import numpy as np


def image_da_webcam(img):
    """
    ->>> !!!! FECHE A JANELA COM A TECLA ESC !!!! <<<<-
        deve receber a imagem da camera e retornar uma imagems filtrada.
    """
    img = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)
     # Detect edges in the image and threshold it
    edges = cv2.Laplacian(img, cv2.CV_8U, ksize=5)
    
    ret, mask = cv2.threshold(edges, 100, 255, cv2.THRESH_BINARY_INV)

    return mask


vc = cv2.VideoCapture(0) ### significa que vou usar a minha webcam, 1,2,3

#vc = cv2.VideoCapture("people-walking.mp4") ### suport video mp4. .avi  .mkv

#vc = cv2.VideoCapture("rtsp://192.168.52.25:800") ### significa que vou usar a minha webcam, 1,2,3


if vc.isOpened(): # try to get the first frame
    rval, frame = vc.read()
else:
    rval = False

while rval:
    
    img = image_da_webcam(frame)

    cv2.imshow("preview", img)
    cv2.imshow("original", frame)

    rval, frame = vc.read()
    key = cv2.waitKey(20)
    if key == 27: # exit on ESC
        break

cv2.destroyWindow("preview")
vc.release()