INTELLIGENT ENVIRONMENT
  • Home
  • About
  • PROJECTS
  • Contact

Python

"Guard" - motion detection activate camera

Like I said "Magic mirror" can also be a secret house guard. It needs to be improved. Built-in motion detection, web camera. I still think I'd be fine - a microphone. I will use Raspberry Pi3 to process the application. Let's get started, for first create some detector, in that situation motion detector

import RPi.GPIO as GPIO
import time
from subprocess import call

def call_camera():
    call(['python3','/home/pi/Desktop/camera.py'])

def spyeye():
    GPIO.setmode(GPIO.BCM)
    SENSOR_PIN = 23
    GPIO.setup(SENSOR_PIN,GPIO.IN)
    try:
        while True: 
            signal = GPIO.input(SENSOR_PIN)
            if signal == 1:
                call_camera()
                break
            else:
                time.sleep(0.1)
    except:
        GPIO.cleanup()

spyeye()


How see function call_camera  encapsulate in function spyeye. After moment when Raspberry get on pin signal starting program where web camera start read video stream. The camera source look that.


from tkinter import *
import cv2
from PIL import Image, ImageTk

cap = cv2.VideoCapture(0)
i = 0
window = Tk()

window_one=Toplevel(window)
window_one.title('hallo')
window_one.configure(background = 'black')
window_one.geometry('650x650')
window_one.bind('<Escape>', lambda e: window.destroy())

window_two = Toplevel(window)
window_two.title('It s yuo')
window_two.configure(background = 'green')

foto_frame = Frame(window_two)
foto_frame.pack()

lmain = Label(foto_frame)
lmain.pack()

def close_def():
    cap.release()
    window.destroy()
 
def video_stream():
    
    window_two.deiconify()
                     
    _, frame = cap.read()
    frame = cv2.flip(frame, 1)
    cv2image = cv2.cvtColor(frame, cv2.COLOR_BGR2RGBA)
    
    img = Image.fromarray(cv2image)
    img = ImageTk.PhotoImage(image=img)
    lmain.img = img
    
    lmain.configure(image=img)
    lmain.after(1, video_stream)
    
def get_foto():
    global i
    while(True):
        _, frame = cap.read()
        frame = cv2.flip(frame, 1)
        i = i + 1
        cv2.imwrite('foto_%s.jpg' % i,frame)
        break
 
btn=Button(foto_frame, text="Quit",command = close_def ).pack()
btn2 = Button(foto_frame, text ="Get foto", command = get_foto).pack()

video_stream()

window.withdraw()
window.mainloop()

Lesson
Powered by Create your own unique website with customizable templates.
  • Home
  • About
  • PROJECTS
  • Contact