dibaloke
4/7/2019 - 4:53 PM

Opencv

#Grayscale Using opencv



import cv2
import matplotlib.pyplot as plt
import numpy as np

# imread is used to read an image
# First parameter is the image , second parameter is the applied filter
img = cv2.imread('watch.png', cv2.IMREAD_GRAYSCALE)
# img = cv2.imread('watch.jpg', cv2.IMREAD_UNCHANGED)

cv2.imshow('image', img)
cv2.waitKey(0)
cv2.destroyAllWindows()
#Grayscale using matplotlib

import cv2
import matplotlib.pyplot as plt
import numpy as np

# imread is used to read an image
# First parameter is the image , second parameter is the applied filter
img = cv2.imread('watch.png', cv2.IMREAD_GRAYSCALE)
# img = cv2.imread('watch.jpg', cv2.IMREAD_UNCHANGED)

# cv2.imshow('image', img)
# cv2.waitKey(0)
# cv2.destroyAllWindows()

plt.imshow(img, cmap='gray', interpolation='bicubic')
plt.show()
import cv2
import matplotlib.pyplot as plt
import numpy as np

# imread is used to read an image
# First parameter is the image , second parameter is the applied filter
img = cv2.imread('watch.png', cv2.IMREAD_GRAYSCALE)


cv2.imwrite('watchgray.png', img)
import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    cv2.imshow('frame', frame)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
#Two frame will be loaded

import cv2
import numpy as np

cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)

    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()
import numpy as np
import os
import cv2


filename = 'video.avi'
frames_per_second = 10.0
res = '720p'

# Set resolution for the video capture
# Function adapted from https://kirr.co/0l6qmh


def change_res(cap, width, height):
    cap.set(3, width)
    cap.set(4, height)


# Standard Video Dimensions Sizes
STD_DIMENSIONS = {
    "480p": (640, 480),
    "720p": (1280, 720),
    "1080p": (1920, 1080),
    "4k": (3840, 2160),
}


# grab resolution dimensions and set video capture to it.
def get_dims(cap, res='1080p'):
    width, height = STD_DIMENSIONS["480p"]
    if res in STD_DIMENSIONS:
        width, height = STD_DIMENSIONS[res]
    # change the current caputre device
    # to the resulting resolution
    change_res(cap, width, height)
    return width, height


# Video Encoding, might require additional installs
# Types of Codes: http://www.fourcc.org/codecs.php
VIDEO_TYPE = {
    'avi': cv2.VideoWriter_fourcc(*'jpeg'),
    # 'mp4': cv2.VideoWriter_fourcc(*'H264'),
    'mp4': cv2.VideoWriter_fourcc(*'jpeg'),
}


def get_video_type(filename):
    filename, ext = os.path.splitext(filename)
    if ext in VIDEO_TYPE:
        return VIDEO_TYPE[ext]
    return VIDEO_TYPE['avi']


cap = cv2.VideoCapture(0)
out = cv2.VideoWriter(filename, get_video_type(filename), 25, get_dims(cap, res))

while True:
    ret, frame = cap.read()
    out.write(frame)
    cv2.imshow('frame', frame)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break


cap.release()
out.release()
cv2.destroyAllWindows()
import numpy as np
import cv2
import smtplib
import imghdr
from email.message import EmailMessage


face_cascade = cv2.CascadeClassifier('face.xml')
eye_cascade = cv2.CascadeClassifier('eye.xml')

msg=EmailMessage()
msg['Subject']='Test for sending a email via python for measurment project'
msg['From']='dibaloke66@gmail.com'
msg['To']='dibaloke66@gmail.com'
msg.set_content('Alert ! Security system activated in your Smart home')
 


cap = cv2.VideoCapture(0)

def send_email():
    print("inside_send_email")
    with open('introduer.jpeg','rb') as f:
        file_data = f.read()
        file_type = imghdr.what(f.name)
        file_name = f.name 
    
    msg.add_attachment(file_data,maintype='image',subtype=file_type,filename=file_name)    
    with smtplib.SMTP_SSL('smtp.gmail.com',465) as smtp:
        smtp.login("dibaloke66@gmail.com","password")
 
        smtp.send_message(msg)
    
while 1:
    ret, img = cap.read()
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]

        cv2.imwrite('introduer.jpeg', img)
        
        eyes = eye_cascade.detectMultiScale(roi_gray)
        for (ex,ey,ew,eh) in eyes:
            cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
            send_email() 
    cv2.imshow('img',img)
   
    
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break
 
cap.release()

cv2.destroyAllWindows()