Initial Commit
Browse files- FriendFacialRecognition/.idea/.gitignore +8 -0
- FriendFacialRecognition/.idea/bot.iml +8 -0
- FriendFacialRecognition/.idea/encodings.xml +6 -0
- FriendFacialRecognition/.idea/inspectionProfiles/Project_Default.xml +8 -0
- FriendFacialRecognition/.idea/inspectionProfiles/profiles_settings.xml +6 -0
- FriendFacialRecognition/.idea/misc.xml +4 -0
- FriendFacialRecognition/.idea/modules.xml +8 -0
- FriendFacialRecognition/.idea/vcs.xml +6 -0
- FriendFacialRecognition/CNN.py +83 -0
- FriendFacialRecognition/bot.py +37 -0
- FriendFacialRecognition/image_manipulation.py +74 -0
- FriendFacialRecognition/main.py +6 -0
FriendFacialRecognition/.idea/.gitignore
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
# Default ignored files
|
2 |
+
/shelf/
|
3 |
+
/workspace.xml
|
4 |
+
# Editor-based HTTP Client requests
|
5 |
+
/httpRequests/
|
6 |
+
# Datasource local storage ignored files
|
7 |
+
/dataSources/
|
8 |
+
/dataSources.local.xml
|
FriendFacialRecognition/.idea/bot.iml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<module type="PYTHON_MODULE" version="4">
|
3 |
+
<component name="NewModuleRootManager">
|
4 |
+
<content url="file://$MODULE_DIR$" />
|
5 |
+
<orderEntry type="inheritedJdk" />
|
6 |
+
<orderEntry type="sourceFolder" forTests="false" />
|
7 |
+
</component>
|
8 |
+
</module>
|
FriendFacialRecognition/.idea/encodings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="Encoding">
|
4 |
+
<file url="PROJECT" charset="UTF-8" />
|
5 |
+
</component>
|
6 |
+
</project>
|
FriendFacialRecognition/.idea/inspectionProfiles/Project_Default.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<profile version="1.0">
|
3 |
+
<option name="myName" value="Project Default" />
|
4 |
+
<inspection_tool class="PyInterpreterInspection" enabled="false" level="WARNING" enabled_by_default="false" />
|
5 |
+
<inspection_tool class="PyPep8Inspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
6 |
+
<inspection_tool class="PyPep8NamingInspection" enabled="false" level="WEAK WARNING" enabled_by_default="false" />
|
7 |
+
</profile>
|
8 |
+
</component>
|
FriendFacialRecognition/.idea/inspectionProfiles/profiles_settings.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<component name="InspectionProjectProfileManager">
|
2 |
+
<settings>
|
3 |
+
<option name="USE_PROJECT_PROFILE" value="false" />
|
4 |
+
<version value="1.0" />
|
5 |
+
</settings>
|
6 |
+
</component>
|
FriendFacialRecognition/.idea/misc.xml
ADDED
@@ -0,0 +1,4 @@
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectRootManager" version="2" project-jdk-name="Python 3.11 (bot)" project-jdk-type="Python SDK" />
|
4 |
+
</project>
|
FriendFacialRecognition/.idea/modules.xml
ADDED
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="ProjectModuleManager">
|
4 |
+
<modules>
|
5 |
+
<module fileurl="file://$PROJECT_DIR$/.idea/bot.iml" filepath="$PROJECT_DIR$/.idea/bot.iml" />
|
6 |
+
</modules>
|
7 |
+
</component>
|
8 |
+
</project>
|
FriendFacialRecognition/.idea/vcs.xml
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
<?xml version="1.0" encoding="UTF-8"?>
|
2 |
+
<project version="4">
|
3 |
+
<component name="VcsDirectoryMappings">
|
4 |
+
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
5 |
+
</component>
|
6 |
+
</project>
|
FriendFacialRecognition/CNN.py
ADDED
@@ -0,0 +1,83 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from image_manipulation import *
|
2 |
+
from keras import layers
|
3 |
+
from keras import models
|
4 |
+
import matplotlib.pyplot as plt
|
5 |
+
import tensorflow as tf
|
6 |
+
import keras
|
7 |
+
import numpy as np
|
8 |
+
|
9 |
+
def data_process():
|
10 |
+
train_datagen = ImageDataGenerator(rescale = 1./255)
|
11 |
+
test_datagen = ImageDataGenerator(rescale = 1./255)
|
12 |
+
train_generator = train_datagen.flow_from_directory(train_dir, target_size = (300, 350), batch_size = 8, class_mode = 'sparse')
|
13 |
+
validation_generator = test_datagen.flow_from_directory(validation_dir, target_size = (300, 350), batch_size = 8, class_mode = 'sparse')
|
14 |
+
return train_generator, validation_generator
|
15 |
+
|
16 |
+
def CNN_model():
|
17 |
+
# creating CNN model
|
18 |
+
model = models.Sequential()
|
19 |
+
model.add(layers.Conv2D(32, (3, 3), activation = 'relu', input_shape = (300, 350, 3)))
|
20 |
+
model.add(layers.MaxPooling2D((2, 2)))
|
21 |
+
model.add(layers.Conv2D(64, (3, 3), activation = 'relu'))
|
22 |
+
model.add(layers.MaxPooling2D((2, 2)))
|
23 |
+
model.add(layers.Conv2D(128, (3, 3), activation = 'relu'))
|
24 |
+
model.add(layers.MaxPooling2D((2, 2)))
|
25 |
+
model.add(layers.Conv2D(128, (3, 3), activation = 'relu'))
|
26 |
+
model.add(layers.MaxPooling2D((2, 2)))
|
27 |
+
model.add(layers.Dropout(0.2))
|
28 |
+
model.add(layers.Flatten())
|
29 |
+
model.add(layers.Dense(256, activation = 'relu'))
|
30 |
+
model.add(layers.Dense(2, activation = 'softmax'))
|
31 |
+
|
32 |
+
return model
|
33 |
+
|
34 |
+
def fit():
|
35 |
+
model = CNN_model()
|
36 |
+
model.compile(loss = 'sparse_categorical_crossentropy',
|
37 |
+
optimizer = keras.optimizers.RMSprop(learning_rate = 1e-5), metrics = ['acc'])
|
38 |
+
train_generator, validation_generator = data_process()
|
39 |
+
return model.fit(train_generator, steps_per_epoch = 25, epochs = 50,
|
40 |
+
validation_data = validation_generator, validation_steps = 6)
|
41 |
+
|
42 |
+
def plot(history):
|
43 |
+
acc = history.history['acc']
|
44 |
+
val_acc = history.history['val_acc']
|
45 |
+
loss = history.history['loss']
|
46 |
+
val_loss = history.history['val_loss']
|
47 |
+
epochs = range(1, len(acc) + 1)
|
48 |
+
plt.plot(epochs, acc, 'r', label = 'Training acc')
|
49 |
+
plt.plot(epochs, val_acc, 'b', label = 'Validation acc')
|
50 |
+
plt.title('Training and validation accuracy')
|
51 |
+
plt.legend()
|
52 |
+
plt.figure()
|
53 |
+
plt.plot(epochs, loss, 'r', label = 'Training loss')
|
54 |
+
plt.plot(epochs, val_loss, 'b', label = 'Validation loss')
|
55 |
+
plt.title('Training and validation loss')
|
56 |
+
plt.legend()
|
57 |
+
plt.show()
|
58 |
+
|
59 |
+
def test_image(model):
|
60 |
+
image_dir = "C:\\Ryan\\PersonalProject\\FriendRecog\\bot\\resized_images"
|
61 |
+
class_names = ["Brandon", "Manuel"]
|
62 |
+
target_size = (300, 350)
|
63 |
+
|
64 |
+
# List all image files in directory
|
65 |
+
image_files = [os.path.join(image_dir, file) for file in os.listdir(image_dir) if file.endswith('.png')]
|
66 |
+
|
67 |
+
# Loop over each image file
|
68 |
+
for image_path in image_files:
|
69 |
+
# Load the image
|
70 |
+
img = keras.utils.load_img(image_path, target_size = target_size)
|
71 |
+
|
72 |
+
# Preprocess the image
|
73 |
+
img_array = keras.utils.img_to_array(img)
|
74 |
+
img_array = tf.expand_dims(img_array, 0) # create a batch
|
75 |
+
|
76 |
+
# Use the pre-trained model to predict
|
77 |
+
predictions = model.predict(img_array)
|
78 |
+
score = tf.nn.softmax(predictions[0])
|
79 |
+
|
80 |
+
predicted_class = class_names[np.argmax(score)]
|
81 |
+
confidence = np.max(score)
|
82 |
+
|
83 |
+
return predicted_class, confidence
|
FriendFacialRecognition/bot.py
ADDED
@@ -0,0 +1,37 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from CNN import CNN_model, test_image
|
2 |
+
from image_manipulation import resize
|
3 |
+
import discord
|
4 |
+
from discord.ext import commands
|
5 |
+
import uuid
|
6 |
+
import requests
|
7 |
+
import shutil
|
8 |
+
import os
|
9 |
+
|
10 |
+
intents = discord.Intents.default()
|
11 |
+
intents.message_content = True
|
12 |
+
client = commands.Bot(command_prefix= '.', intents = intents)
|
13 |
+
|
14 |
+
@client.event
|
15 |
+
async def on_ready():
|
16 |
+
print("Bot is up and running!")
|
17 |
+
@client.command()
|
18 |
+
async def save(ctx):
|
19 |
+
try:
|
20 |
+
url = ctx.message.attachments[0].url
|
21 |
+
except IndexError:
|
22 |
+
print("Error: No attachments")
|
23 |
+
await ctx.send("No attachments detected. Please try again.")
|
24 |
+
else:
|
25 |
+
if url[0:26] == "https://cdn.discordapp.com":
|
26 |
+
r = requests.get(url, stream = True)
|
27 |
+
imageName = str(uuid.uuid4()) + '.png'
|
28 |
+
folder_path = "C:\Ryan\PersonalProject\\FriendRecog\\bot\images"
|
29 |
+
full_path = os.path.join(folder_path, imageName)
|
30 |
+
with open(full_path, 'wb') as file:
|
31 |
+
print('Saving Image: ' + imageName)
|
32 |
+
shutil.copyfileobj(r.raw, file)
|
33 |
+
|
34 |
+
resize()
|
35 |
+
model = CNN_model()
|
36 |
+
predicted_class, confidence = test_image(model)
|
37 |
+
await ctx.send("Prediction: " + str(predicted_class) + " with a " + 100 * str(confidence) + "% confidence level.")
|
FriendFacialRecognition/image_manipulation.py
ADDED
@@ -0,0 +1,74 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
import os
|
2 |
+
from PIL import Image
|
3 |
+
from keras.preprocessing import image
|
4 |
+
from keras.preprocessing.image import ImageDataGenerator
|
5 |
+
|
6 |
+
BRANDON_ORIGINAL_DATASET_DIR = "C:\Ryan\PP stuff\\try1\Classification Data-20240212T032009Z-001\Classification Data\\Brandon"
|
7 |
+
MANUEL_ORIGINAL_DATASET_DIR = "C:\\Ryan\\PP stuff\\try1\\Classification Data-20240212T032009Z-001\\Classification Data\\Manuel"
|
8 |
+
BASE_DIR = "C:\\Ryan\\PP stuff\\try1\\face_recog"
|
9 |
+
|
10 |
+
#create directories for train/validation/test sets
|
11 |
+
train_dir = os.path.join(BASE_DIR, 'train')
|
12 |
+
validation_dir = os.path.join(BASE_DIR, 'validation')
|
13 |
+
test_dir = os.path.join(BASE_DIR, 'test')
|
14 |
+
|
15 |
+
train_bran_dir = os.path.join(train_dir, 'brandon')
|
16 |
+
train_man_dir = os.path.join(train_dir, 'manuel')
|
17 |
+
|
18 |
+
validation_bran_dir = os.path.join(validation_dir, 'brandon')
|
19 |
+
validation_man_dir = os.path.join(validation_dir, 'manuel')
|
20 |
+
|
21 |
+
test_bran_dir = os.path.join(test_dir, 'brandon')
|
22 |
+
test_man_dir = os.path.join(test_dir, 'manuel')
|
23 |
+
|
24 |
+
def resize():
|
25 |
+
target_size = (300, 350)
|
26 |
+
|
27 |
+
input_dir = "C:\Ryan\PersonalProject\\FriendRecog\\bot\images"
|
28 |
+
output_dir = "C:\\Ryan\\PersonalProject\\FriendRecog\\bot\\resized_images"
|
29 |
+
|
30 |
+
try:
|
31 |
+
for filename in os.listdir(input_dir):
|
32 |
+
# Construct the full path to the image file
|
33 |
+
input_path = os.path.join(input_dir, filename)
|
34 |
+
|
35 |
+
# Open the image
|
36 |
+
with Image.open(input_path) as img:
|
37 |
+
# Resize the image
|
38 |
+
resized_img = img.resize(target_size)
|
39 |
+
|
40 |
+
# Construct the output path
|
41 |
+
output_path = os.path.join(output_dir, filename)
|
42 |
+
|
43 |
+
# Save the resized image
|
44 |
+
resized_img.save(output_path)
|
45 |
+
finally:
|
46 |
+
pass
|
47 |
+
|
48 |
+
def data_augmentation():
|
49 |
+
augmented_datagen = ImageDataGenerator(
|
50 |
+
rescale = 1. / 255,
|
51 |
+
rotation_range = 40,
|
52 |
+
width_shift_range = 0.2,
|
53 |
+
height_shift_range = 0.2,
|
54 |
+
shear_range = 0.2,
|
55 |
+
zoom_range = 0.2,
|
56 |
+
horizontal_flip = True,
|
57 |
+
fill_mode = "nearest")
|
58 |
+
augmented_generator = augmented_datagen.flow_from_directory(train_dir, target_size = (300, 350),
|
59 |
+
batch_size = 20,
|
60 |
+
class_mode = 'sparse')
|
61 |
+
|
62 |
+
augmented_dir = os.path.join(BASE_DIR, "augmented")
|
63 |
+
augmented_all = os.path.join(augmented_dir, "all")
|
64 |
+
os.mkdir(augmented_dir)
|
65 |
+
os.mkdir(augmented_all)
|
66 |
+
|
67 |
+
for i, (images, labels) in enumerate(augmented_generator):
|
68 |
+
if i >= 5:
|
69 |
+
break
|
70 |
+
for j in range(len(images)):
|
71 |
+
augmented_image = image.array_to_img(images[j])
|
72 |
+
filename = f"{i * len(images) + j}.png"
|
73 |
+
augmented_image_path = os.path.join(augmented_all, filename)
|
74 |
+
augmented_image.save(augmented_image_path)
|
FriendFacialRecognition/main.py
ADDED
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
from CNN import fit
|
2 |
+
from bot import client
|
3 |
+
|
4 |
+
if __name__ == '__main__':
|
5 |
+
model = fit()
|
6 |
+
client.run("MTIwOTcxMDMxNDQwOTU2NjIwOA.GWFDG0.qNkiHf_SfG-Jj4pMsD7P1D1urhq9RaTGLLIpTg")
|