将.py转换为.exe后,视频录制将无法在python上运行

时间:2020-06-08 21:37:22

标签: python opencv

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
from email.mime.base import MIMEBase
from email import encoders
import numpy as np
import cv2
m = 0

def sentMail():
    email_user = 'XXXX'
    email_password = 'XXXX'
    email_send = email_user

    subject = 'Python Test'

    msg = MIMEMultipart()
    msg['From'] = email_user
    msg['To'] = email_send
    msg['Subject'] = subject

    body = '''    MOTION DETECTED! MOTION DETECTED!
    MOTION DETECTED! MOTION DETECTED!
    MOTION DETECTED! MOTION DETECTED!'''
    msg.attach(MIMEText(body, 'plain'))

    filename = 'img.jpg'
    attachment = open(filename, 'rb')

    part = MIMEBase('application', 'octet-stream')
    part.set_payload((attachment).read())
    encoders.encode_base64(part)
    part.add_header('Content-Disposition', "attachment; filename= " + filename)

    msg.attach(part)
    text = msg.as_string()
    server = smtplib.SMTP('smtp.gmail.com', 587)
    server.starttls()
    server.login(email_user, email_password)

    server.sendmail(email_user, email_send, text)
    server.quit()

# Video Capture
# capture = cv2.VideoCapture(0)
capture = cv2.VideoCapture(0)
fourcc = cv2.VideoWriter_fourcc(*'XVID')
out = cv2.VideoWriter('output.avi', fourcc, 20, (640, 480))

# History, Threshold, DetectShadows
# History: Every 300 frames, the pixels on the screen become the new background
fgbg = cv2.createBackgroundSubtractorMOG2(300, 200, True)

# Keeps track of what frame we're on
frameCount = 0

while (1):

    # Return Value and the current frame
    ret, frame = capture.read()

    #  Check if a current frame actually exist
    if not ret:
        break

    frameCount += 1
    # Resize the frame since the video (demo.mov) is too big
    resizedFrame = cv2.resize(frame, (0, 0), fx=0.50, fy=0.50)  # source, desired size for the image, fx, fy

    # Get the foreground mask
    fgmask = fgbg.apply(resizedFrame)

    # Count all the non zero pixels within the mask (0 = black, 1 = white)
    count = np.count_nonzero(fgmask)

    print('Frame: %d, Pixel Count: %d' % (frameCount, count))


    # Determine how many pixels do you want to detect to be considered "movement"
    if (frameCount > 1 and count > 800):  # Lower the count is higher the sensitivity is
        m += 1
        print('Motion Detected')
        print(m)
        cv2.putText(resizedFrame, 'Motion Detected', (5, 25), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 0, 255), 2, cv2.LINE_AA)
        out.write(frame)
        if m == 60:
            img = frame
            imaje = frame
            cv2.imwrite("img.jpg", img)
            sentMail()
            m = 0


    cv2.imshow('Frame', resizedFrame)
    cv2.imshow('Mask', fgmask)

    if cv2.waitKey(1) & 0xff == ord('q'):
        break
    elif cv2.waitKey(1) & 0xff == ord('s'): # Press 's' to save that frame as a .png picture
        cv2.imwrite("frame" + str(frameCount) + ".png", frame)


capture.release()
out.release()
cv2.destroyAllWindows()

#我的代码在检测到运动时会保存图像和视频,如果我从PyCharm运行代码,则会保存它们,但是将代码从.py转换为.exe后,它不会保存视频,但会保存图像。 output.avi(video)显示为0KB。我尝试使用mp4和flv来操作,但也无法正常工作,也许是因为我没有为视频指定位置

0 个答案:

没有答案