Sattawat
Sattawat

Reputation: 149

Why can't I import functions in python files in another folder?

from keras.models import load_model
import cv2
import numpy as np
from PIL import Image
from MaskDetection.MaskCropper import cropEyeLineFromMasked


IMAGE_SIZE = (224, 224)
actors_dict = {0: 'Andrew Garfield',
               1: 'Angelina Jolie',
               2: 'Anthony Hopkins',
               3: 'Ben Affleck',
               4: 'Beyonce Knowles'}


def actor_recognition(img, original_img):
    model = load_model('D:/project/FMDR/MaskDetection/models/face_mask_detection.hdf5')
    # image = cv2.imread('./Images4Test/')
    # imageRGB = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    image_resized = cv2.resize(img, IMAGE_SIZE)
    # image_np = image_resized / 255.0  # Normalized to 0 ~ 1
    image_exp = np.expand_dims(image_resized, axis=0)
    # image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)

    # result = vgg16.predict_classes(image_exp)
    result = model.predict(image_exp)
    key = np.argmax(result, axis=1)
    actor = actors_dict.get(key[0])
    cv2.putText(original_img, actor, (20, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
    Image.fromarray(original_img).show()
    # print(result)


PATH_IMG = 'D:\project\FMDR\Datasets\SysAgWmaskCropped\Test\Angelina Jolie\1.GettyImages-1169983509L.jpg'

if __name__ == '__main__':
    img = cv2.imread(PATH_IMG)
    # Image.fromarray(img).show()
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    # Image.fromarray(img).show()
    img_crop = cropEyeLineFromMasked(img)
    Image.fromarray(img_crop).show()
    actor_recognition(img_crop, img)

this error 2022-04-20 23:41:25.568495: W tensorflow/stream_executor/platform/default/dso_loader.cc:64] Could not load dynamic library 'cudart64_110.dll'; dlerror: cudart64_110.dll not found 2022-04-20 23:41:25.568712: I tensorflow/stream_executor/cuda/cudart_stub.cc:29] Ignore above cudart dlerror if you do not have a GPU set up on your machine. Traceback (most recent call last): File "D:\project\FMDR\MaskDetection\actors_recognition.py", line 5, in from MaskDetection.MaskCropper import cropEyeLineFromMasked ModuleNotFoundError: No module named 'MaskDetection'

i have function cropEyeLineFromMasked in file MaskCropper.py which it is in the folder MaskDetection

Upvotes: 0

Views: 82

Answers (1)

Adithya Ranjith
Adithya Ranjith

Reputation: 1

When python imports something, it imports it from its predefined locations...it essentially searches a predefined list of locations from where modules should be imported. If your module is not present in that list of directories or locations, it will return an error. You can find the list by typing:

import sys
print(sys.path)

so because the path of the directory(folder) containing your function is not there in sys.path, python can't find the function.

you have to first append or insert the directory to sys.path by doing sys.path.insert(0,'path') or sys.path.append('path')

when you do this your directory path is added to sys.path and so you can now call the function from the module in the required directory

Hope it helps

Upvotes: 0

Related Questions