如何将相机拍摄的图像发送到电子邮件

时间:2019-07-17 16:42:21

标签: python smtp

我有一个程序可以检测人脸并将其保存到文件夹中。我想将这些图像发送到电子邮件ID,但是我不知道该怎么做。

以下是保存图像的代码:

import cv2


#import the cascade for face detection
FaceClassifier =cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
# access the webcam (every webcam has 
capture = cv2.VideoCapture(0)

while(True):
    # Capture frame-by-frame

    ret, frame = capture.read()
    if not capture:
        print ("Error opening webcam device")
        sys.exit(1)


    # to detect faces in video
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    faces = FaceClassifier.detectMultiScale(gray, 1.3, 5)

    # Resize Image 
    minisize = (frame.shape[1],frame.shape[0])
    miniframe = cv2.resize(frame, minisize)
    # Store detected frames in variable name faces
    faces =  FaceClassifier.detectMultiScale(miniframe)
   # Draw rectangle 
    for f in faces:
        x, y, w, h = [ v for v in f ]
        cv2.rectangle(frame, (x,y), (x+w,y+h), (255,255,255))
        #Save just the rectangle faces in SubRecFaces
        sub_face = frame[y:y+h, x:x+w]
        FaceFileName = "faces/face_" + str(y) + ".jpg"
        cv2.imwrite(FaceFileName, sub_face)
        #Display the image 
        cv2.imshow('Result',frame)
        cv2.waitKey(180)
        break

    # When everything is done, release the capture

img.release()
cv2.destroyAllWindows()

我不知道如何将保存的图像发送到电子邮件ID。请帮忙!

2 个答案:

答案 0 :(得分:0)

为此,我们使用SendGrid。这是一个基于Web的服务,并且有一个与之交互的python模块。

SendGrid

SendGrid Python Module

下面是一些附加PDF的示例代码,因此,基本上,您只需将FileType调用更改为任何图像,并将FileName调用更改为图像。

  ObjectMapper mapper = new ObjectMapper();
      String jsonString = "{\"name\":\"Mahesh\", \"age\":21}";

      //map json to student
      try{
         Student student = mapper.readValue(jsonString, Student.class);

         System.out.println(student);

         jsonString = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(student);

         System.out.println(jsonString);
      }
      catch (JsonParseException e) { e.printStackTrace();}
      catch (JsonMappingException e) { e.printStackTrace(); }
      catch (IOException e) { e.printStackTrace(); }
   }

答案 1 :(得分:0)

@Arpit说必须是smtp。这是使用smtplib做到这一点的简单方法:

# Import smtplib for the actual sending function
import smtplib

# And imghdr to find the types of our images
import imghdr

# Here are the email package modules we'll need
from email.message import EmailMessage

# Create the container email message.
msg = EmailMessage()
msg['Subject'] = 'Our family reunion'
# me == the sender's email address
# family = the list of all recipients' email addresses
msg['From'] = me
msg['To'] = ', '.join(family)
msg.preamble = 'You will not see this in a MIME-aware mail reader.\n'

# Open the files in binary mode.  Use imghdr to figure out the
# MIME subtype for each specific image.
for file in pngfiles:
    with open(file, 'rb') as fp:
        img_data = fp.read()
    msg.add_attachment(img_data, maintype='image',
                                 subtype=imghdr.what(None, img_data))

# Send the email via our own SMTP server.
with smtplib.SMTP('localhost') as s:
    s.send_message(msg)

来自这里: SMTPLIB