程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

Python controls its own mobile camera to take photos, and automatically sends the photos to the mailbox (for learning reference only)

編輯:Python

Hey, everyone, good duck ! I'm a panda

Let's finish this simple case with you today ~

Want to complete today's case , Just remember one key point :
You need a camera


Don't talk much , Let's get started

what are you having? python I won't answer the related error report 、 Or source code information / Module installation / Women's clothing bosses are proficient in skills You can come here :(https://jq.qq.com/?_wv=1027&k=2Q3YTfym) perhaps +V:python10010 Ask me

Start code

Tool import

import time
import cv2 # pip install opencv-python -i Mirror source URL 
from email.mime.image import MIMEImage # A library for building mail content 
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
import smtplib # Send E-mail 

Call the camera , Save the picture

Taking pictures , It's a camera with a mobile phone , The software uses :IP camera ( Android ), Because in the same LAN , open APP, The web address that appears inside is the address of the camera

def GetPicture():
""" Take pictures and save images :return: """
# Create a window 
cv2.namedWindow('camera', 1)
# Call the camera IP camera APP
video = "http://admin:[email protected]:8081/video"
cap = cv2.VideoCapture(video)
while True:
success, img = cap.read()
cv2.imshow("camera", img)
# Press the key to handle 
key = cv2.waitKey(10)
if key == 27:
# esc
break
if key == 32:
# Space 
fileaname = 'frames.jpg'
cv2.imwrite(fileaname, img)
# Release camera 
cap.release()
# close window 
cv2.destroyWindow("camera")

Run code , There will be an effect

Create a function , Set up my email content

def SetMsg():
""" Format mail :return: """
msg = MIMEMultipart('mixed')
# title 
msg['Subject'] = ' Photos of little sister '
msg['From'] = sender # Sender's email 
msg['To'] = receiver # Receiver's email 
# Message body 
text = ' Here comes the picture of your little sister , Please accept '
text_plain = MIMEText(text, 'plain', 'utf-8') # Text transcoding 
msg.attach(text_plain)
# Picture attachment 
SendImageFile = open('D:/ Control the camera to take pictures and send emails /frames.jpg', 'rb').read()
image = MIMEImage(SendImageFile)
# Change the name of the attached photo seen by the recipient to people.png.
image['Content-Disposition'] = 'attachment; filename = "people.png"'
msg.attach(image)
return msg.as_string()

Mail port settings

The authorization code can be obtained here

# Authorization code 
pwd = "******" # You'd better write your own 
# Server interface 
host = 'smtp.163.com'
port = 25
sender = '[email protected]' # You'd better write your own 
receiver = '[email protected]' # You'd better write your own 

Send mail function

def SendEmail(msg):
""" Send E-mail :param msg: Email content :return: """
smtp = smtplib.SMTP()
smtp.connect(host,port=25)
smtp.login(sender, pwd)
smtp.sendmail(sender, receiver, msg)
time.sleep(2)
smtp.quit()

encapsulate

if __name__ == '__main__':
# 1. Take photos and save 
GetPicture()
# 2. Format the message 
msg = SetMsg()
# 3. Send E-mail 
SendEmail(msg)

Run code , Demonstration effect

Take a picture first

Sent to email

What about? , Is today's case very simple ?

I hope my article can be helpful to you who are learning by yourself ~

I'm a panda , See you in the next article (*◡‿◡)


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved