0

To start, I need to tell you I'm a newbie programmer, sorry if the terms I use or the things I do show unprofessionalism.

TL;DR: exe resulted from first code doesn't work, second code does, how can I make it work? If possible, how can I run the .exe solely without the background image and still have the background appear?

I'm basically making a program that runs facial detection on images that one chooses. In the background Where you can select the picture that will be face-detected, I'm trying to keep a window displaying another image that is just for show and nothing more really but is mandatory for the project.

Another thing I'd like to ask is whether it'd be possible to have the image be somehow able to be used without needing it to actually exist. The project requires a GUI executable that requires only the .exe file

Here is the code using absolute paths:

import cv2 as cv
import os
from tkinter import filedialog
import PySimpleGUI as sg
directory = os.path.dirname(__file__)
def Face_recognition():
    original_image = cv.imread(filedialog.askopenfilename(initialdir="/",
                                                          title="Select a File",
                                                          filetypes=(("PNG images",
                                                                      "*.png*"),
                                                                     ("JPEG images",
                                                                      "*.jpg*"),
                                                                     ("all files",
                                                                      "*.*"))))
    grayscale_image = cv.cvtColor(original_image, cv.COLOR_BGR2GRAY)
    face_cascade = cv.CascadeClassifier(directory + '\haarcascade_frontalface_alt.xml')
    detected_faces = face_cascade.detectMultiScale(grayscale_image,scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    )
    for (column, row, width, height) in detected_faces:
        cv.rectangle(
            original_image,
            (column, row),
            (column + width, row + height),
            (25, 255, 25),
            5
        )
    resized = cv.resize(original_image,(1920,1080), cv.INTER_AREA)
    cv.imshow('Image', resized)
    cv.waitKey(0)
    cv.destroyAllWindows()

#window_layout
path = os.path.join(directory, 'Unknown.png')
#path =  directory + '/Unknown.png'
layout = [[sg.Button("Browse")], [sg.Text("Face Detector")],[sg.Image('C:\\Users\\LENOVO\\PycharmProjects\\pythonProject\\Unknown.png')]]
window = sg.Window("Face Detector",layout)
while True:
    event, value = window.read()
    if event == "Browse":
        Face_recognition()
    if event == sg.WIN_CLOSED:
        break
window.close()

Here is the code using relative paths:

import cv2 as cv
import os
from tkinter import filedialog
import PySimpleGUI as sg
directory = os.path.dirname(__file__)
def Face_recognition():
    original_image = cv.imread(filedialog.askopenfilename(initialdir="/",
                                                          title="Select a File",
                                                          filetypes=(("PNG images",
                                                                      "*.png*"),
                                                                     ("JPEG images",
                                                                      "*.jpg*"),
                                                                     ("all files",
                                                                      "*.*"))))
    grayscale_image = cv.cvtColor(original_image, cv.COLOR_BGR2GRAY)
    face_cascade = cv.CascadeClassifier(directory + '\haarcascade_frontalface_alt.xml')
    detected_faces = face_cascade.detectMultiScale(grayscale_image,scaleFactor=1.1,
    minNeighbors=5,
    minSize=(30, 30),
    )
    for (column, row, width, height) in detected_faces:
        cv.rectangle(
            original_image,
            (column, row),
            (column + width, row + height),
            (25, 255, 25),
            5
        )
    resized = cv.resize(original_image,(1920,1080), cv.INTER_AREA)
    cv.imshow('Image', resized)
    cv.waitKey(0)
    cv.destroyAllWindows()

#window_layout
path = os.path.join(directory, 'Unknown.png')
#path =  directory + '/Unknown.png'
layout = [[sg.Button("Browse")], [sg.Text("Face Detector")],[sg.Image(path)]]
window = sg.Window("Face Detector",layout)
while True:
    event, value = window.read()
    if event == "Browse":
        Face_recognition()
    if event == sg.WIN_CLOSED:
        break
window.close()

The problem I have starts at lines 35 and after. Whenever I change the path to something relative, it just breaks, otherwise, it works completely fine. However, when I start using relative paths, for whatever reason, the program just refuses to use anything but a temporary folder it just created to look for the image used. I don't get it and it makes me grind my teeth in serious frustration.

Here is what I write in the terminal to create the .exe file.

pyinstaller Recognition.py --onefile -w

I use PyCharm to code by the way, here are the packages that might be related to my problems: PySimpleGUI Pyinstaller Opencv-python Opencv-contrib-python

Here is my Python projects folder which holds the background image giving me nightmares This is what happens when I make the path relative

Noobman
  • 1
  • 1
  • Your relative path is still collecting while creating the executable. Read more in this [Q & A](https://stackoverflow.com/questions/60937345/how-to-set-up-relative-paths-to-make-a-portable-exe-build-in-pyinstaller-with-p). You need to tell the program to be 'relative' or not when you are developing vs compiling. – Kat Dec 29 '21 at 21:15

0 Answers0