Skip to content

Commit

Permalink
Main files for the Image Recognition
Browse files Browse the repository at this point in the history
  • Loading branch information
PubudU99 committed Jan 28, 2024
1 parent dbf0230 commit 262b356
Show file tree
Hide file tree
Showing 6 changed files with 192 additions and 0 deletions.
79 changes: 79 additions & 0 deletions combined.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import os
from keras.models import load_model
import cv2
import numpy as np
from PIL import Image
import time

# Set the environment variable to disable oneDNN custom operations
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'

# Load the model
model = load_model("keras_Model.h5", compile=False)

# Load the labels
class_names = open("labels.txt", "r").readlines()

# Specify the output directory
output_directory = "C:/Users/Pubudu Madusith/Pictures/Camera Roll/Cropped"
os.makedirs(output_directory, exist_ok=True) # Create the directory if it doesn't exist

# Specify the index of the external webcam (you may need to adjust this)
external_webcam_index = 1 # Change this to the correct index for your external webcam

# Open the external webcam
cap = cv2.VideoCapture(external_webcam_index)

while True:
# Capture frame from the external webcam
ret, frame = cap.read()

# Display the captured frame
cv2.imshow("External Webcam", frame)

# Convert the frame to a PIL image
im = Image.fromarray(cv2.cvtColor(frame, cv2.COLOR_BGR2RGB))

# Perform cropping
width, height = im.size
left_crop = 100 # Adjust the left crop value as needed
right_crop = width - 100 # Adjust the right crop value as needed
top = 0
bottom = height
im_cropped = im.crop((left_crop, top, right_crop, bottom))

# Resize the cropped image to match the model's input shape
im_cropped = im_cropped.resize((224, 224))

# Construct the full path for the cropped image
output_path = os.path.join(output_directory, f"Cropped_frame.jpg")

# Save the cropped image to the output directory
im_cropped.save(output_path)

# Convert the cropped image to a numpy array
cropped_image = np.asarray(im_cropped, dtype=np.float32).reshape(1, 224, 224, 3)

# Normalize the image array
cropped_image = (cropped_image / 127.5) - 1

# Predict the model
prediction = model.predict(cropped_image)
index = np.argmax(prediction)
class_name = class_names[index].strip()
confidence_score = prediction[0][index]

# Print prediction and confidence score
print(f"Class: {class_name[2:]}, Confidence Score: {str(np.round(confidence_score * 100))[:-2]}%")

# Wait for 2 seconds before processing the next frame
time.sleep(2)

# Check for the escape key (27 is the ASCII for the esc key)
keyboard_input = cv2.waitKey(1)
if keyboard_input == 27:
break

# Release the external webcam and destroy the window
cap.release()
cv2.destroyAllWindows()
Binary file added converted_keras.zip
Binary file not shown.
43 changes: 43 additions & 0 deletions cropping.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from PIL import Image
import os

# Input directory path
input_directory = "C:/Users/Pubudu Madusith/Pictures/Camera Roll"

# Output directory path for cropped images
output_directory = "C:/Users/Pubudu Madusith/Pictures/Camera Roll/Cropped"
os.makedirs(output_directory, exist_ok=True) # Create the directory if it doesn't exist

# List all image files in the input directory
image_files = [f for f in os.listdir(input_directory) if f.lower().endswith(('.png', '.jpg', '.jpeg', '.gif', '.bmp'))]

# Loop through each image file and crop
for image_file in image_files:
# Construct the full path for the image file
image_path = os.path.join(input_directory, image_file)

# Opens an image in RGB mode
im = Image.open(image_path)

# Size of the image in pixels (size of the original image)
width, height = im.size

# Setting the points for cropped image
left_crop = 100 # Adjust the left crop value as needed
right_crop = width - 100 # Adjust the right crop value as needed
top = 0
bottom = height

# Cropped image of the specified dimensions
im_cropped = im.crop((left_crop, top, right_crop, bottom))

# Construct the full path for the cropped image
output_path = os.path.join(output_directory, f"Cropped_{image_file}")

# Save the cropped image to the output directory
im_cropped.save(output_path)

# Optionally, show the cropped image
im_cropped.show()

print("Cropping and saving complete.")
Binary file added keras_model.h5
Binary file not shown.
4 changes: 4 additions & 0 deletions labels.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
0 SCENT
1 Purse
2 Colongne
3 No Object
66 changes: 66 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import os
from keras.models import load_model
import cv2
import numpy as np
import tensorflow as tf

# Set the environment variable to disable oneDNN custom operations
os.environ['TF_ENABLE_ONEDNN_OPTS'] = '0'

# Load the model
model = load_model("keras_Model.h5", compile=False)

# Load the labels
class_names = open("labels.txt", "r").readlines()

# Specify the input and output directories
input_directory = "C:/Users/Pubudu Madusith/Pictures/Camera Roll" # Change this to your image directory
# output_directory = "D:/3yp_Project/ESP32/PythonTensorFlow/Processed"

# Create the output directory if it doesn't exist
# os.makedirs(output_directory, exist_ok=True)

# List all files in the input directory
image_files = [f for f in os.listdir(input_directory) if f.endswith(('.jpg', '.jpeg', '.png'))]

for image_file in image_files:
image_path = os.path.join(input_directory, image_file)

# Read the image from the file
image = cv2.imread(image_path)

# Resize the image
image = cv2.resize(image, (224, 224), interpolation=cv2.INTER_AREA)

# Show the image in a window
cv2.imshow("Image from Directory", image)

# Make the image a numpy array and reshape it to the model's input shape
image = np.asarray(image, dtype=np.float32).reshape(1, 224, 224, 3)

# Normalize the image array
image = (image / 127.5) - 1

# Predict the model
prediction = model.predict(image)
index = np.argmax(prediction)
class_name = class_names[index].strip()
confidence_score = prediction[0][index]

# Print prediction and confidence score
# print(f"Image: {image_file}, Class: {class_name[2:]}, Confidence Score: {str(np.round(confidence_score * 100))[:-2]}%")
print(class_name[2:])

# os.remove(image_path)
# print(f"Original image {image_file} deleted.")

# Wait for a key press to continue processing the next image
keyboard_input = cv2.waitKey(1)

# 27 is the ASCII for the esc key on your keyboard.
if keyboard_input == 27:
break

# Destroy the window after processing all images
cv2.destroyAllWindows()

0 comments on commit 262b356

Please sign in to comment.