Skip to content

Commit

Permalink
Merge pull request #13345 from Carreau/no-sys-path-append
Browse files Browse the repository at this point in the history
Don't add ipython unconditionally to sys.path
  • Loading branch information
Carreau committed Dec 26, 2021
2 parents b2c3261 + 96c5d75 commit a2ae81e
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions IPython/core/application.py
Expand Up @@ -185,6 +185,17 @@ def _profile_changed(self, change):
get_ipython_package_dir(), u'config', u'profile', change['new']
)

add_ipython_dir_to_sys_path = Bool(
False,
"""Should the IPython profile directory be added to sys path ?
This option was non-existing before IPython 8.0, and ipython_dir was added to
sys path to allow import of extensions present there. This was historical
baggage from when pip did not exist. This now default to false,
but can be set to true for legacy reasons.
""",
).tag(config=True)

ipython_dir = Unicode(
help="""
The name of the IPython directory. This directory is used for logging
Expand Down Expand Up @@ -284,21 +295,24 @@ def _ipython_dir_changed(self, change):
str_old = os.path.abspath(old)
if str_old in sys.path:
sys.path.remove(str_old)
str_path = os.path.abspath(new)
sys.path.append(str_path)
ensure_dir_exists(new)
readme = os.path.join(new, 'README')
readme_src = os.path.join(get_ipython_package_dir(), u'config', u'profile', 'README')
if not os.path.exists(readme) and os.path.exists(readme_src):
shutil.copy(readme_src, readme)
for d in ('extensions', 'nbextensions'):
path = os.path.join(new, d)
try:
ensure_dir_exists(path)
except OSError as e:
# this will not be EEXIST
self.log.error("couldn't create path %s: %s", path, e)
self.log.debug("IPYTHONDIR set to: %s" % new)
if self.add_ipython_dir_to_sys_path:
str_path = os.path.abspath(new)
sys.path.append(str_path)
ensure_dir_exists(new)
readme = os.path.join(new, "README")
readme_src = os.path.join(
get_ipython_package_dir(), "config", "profile", "README"
)
if not os.path.exists(readme) and os.path.exists(readme_src):
shutil.copy(readme_src, readme)
for d in ("extensions", "nbextensions"):
path = os.path.join(new, d)
try:
ensure_dir_exists(path)
except OSError as e:
# this will not be EEXIST
self.log.error("couldn't create path %s: %s", path, e)
self.log.debug("IPYTHONDIR set to: %s" % new)

def load_config_file(self, suppress_errors=IPYTHON_SUPPRESS_CONFIG_ERRORS):
"""Load the config file.
Expand Down

1 comment on commit a2ae81e

@abirmouelhi
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import cv2

def generate_dataset():
face_classifier = cv2.CascadeClassifier("haarcascade_frontalface_default.xml")
def face_cropped(img):
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_classifier.detectMultiScale(gray, 1.3, 5)

    if faces is ():
        return None
    for (x,y,w,h) in faces:
        cropped_face = img[y:y+h,x:x+w]
    return cropped_face

cap = cv2.VideoCapture(1)
img_id = 0

while True:
    ret, frame = cap.read()
    if face_cropped(frame) is not None:
        img_id+=1
        face = cv2.resize(face_cropped(frame), (200,200))
        face = cv2.cvtColor(face, cv2.COLOR_BGR2GRAY)
        #file_name_path = "data/"+"Ishwar."+str(img_id)+".jpg"
        file_name_path = "Images for visualization/"+str(img_id)+'.jpg'
        cv2.imwrite(file_name_path, face)
        cv2.putText(face, str(img_id), (50,50), cv2.FONT_HERSHEY_COMPLEX, 1, (0,255,0), 2 )
        
        cv2.imshow("Cropped_Face", face)
        if cv2.waitKey(1)==13 or int(img_id)==20:
            break
            
cap.release()
cv2.destroyAllWindows()
print("Collecting samples is completed !!!")

#generate_dataset()

  1. Create label
    import numpy as np # pip install numpy

def my_label(image_name):
name = image_name.split('.')[-3]
# if you have two person in your dataset

if name=="Ishwar":

return np.array([1,0])

elif name=="Manish":

return np.array([0,1])

# if you have three person in your dataset
if name=="Ishwar":
    return np.array([1,0,0])
elif name=="Manish":
    return np.array([0,1,0])
elif name=="Bijay":
    return np.array([0,0,1])
  1. Create data
    import os
    from random import shuffle
    from tqdm import tqdm

def my_data():
data = []
for img in tqdm(os.listdir("data")):
path=os.path.join("data",img)
img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
img_data = cv2.resize(img_data, (50,50))
data.append([np.array(img_data), my_label(img)])
shuffle(data)
return data

data = my_data()

train = data[:2400]
test = data[2400:]
X_train = np.array([i[0] for i in train]).reshape(-1,50,50,1)
print(X_train.shape)
y_train = [i[1] for i in train]
X_test = np.array([i[0] for i in test]).reshape(-1,50,50,1)
print(X_test.shape)
y_test = [i[1] for i in test]

  1. Creating the model

import warnings

warnings.filterwarnings('ignore')

import tensorflow as tf
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression

tf.reset_default_graph()
convnet = input_data(shape=[50,50,1])
convnet = conv_2d(convnet, 32, 5, activation='relu')

32 filters and stride=5 so that the filter will move 5 pixel or unit at a time

convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)

convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 3, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate = 0.001, loss='categorical_crossentropy')
model = tflearn.DNN(convnet, tensorboard_verbose=1)
model.fit(X_train, y_train, n_epoch=12, validation_set=(X_test, y_test), show_metric = True, run_id="FRS" )

  1. Let's visualize the data and make prediction
    def data_for_visualization():
    Vdata = []
    for img in tqdm(os.listdir("Images for visualization")):
    path = os.path.join("Images for visualization", img)
    img_num = img.split('.')[0]
    img_data = cv2.imread(path, cv2.IMREAD_GRAYSCALE)
    img_data = cv2.resize(img_data, (50,50))
    Vdata.append([np.array(img_data), img_num])
    shuffle(Vdata)
    return Vdata

Vdata = data_for_visualization()

import matplotlib.pyplot as plt # pip install matplotlib

fig = plt.figure(figsize=(20,20))
for num, data in enumerate(Vdata[:20]):
img_data = data[0]
y = fig.add_subplot(5,5, num+1)
image = img_data
data = img_data.reshape(50,50,1)
model_out = model.predict([data])[0]

if np.argmax(model_out) == 0:
    my_label = 'Ishwar'
elif np.argmax(model_out) == 1:
    my_label = 'Manish'
else:
    my_label = 'Bijay'
    
y.imshow(image, cmap='gray')
plt.title(my_label)

y.axes.get_xaxis().set_visible(False)
y.axes.get_yaxis().set_visible(False)

plt.show()

Please sign in to comment.