From 0b5bf2102fb09eeda7890e9973a1fa9f5e92b372 Mon Sep 17 00:00:00 2001 From: TechGirl007 Date: Tue, 15 Oct 2024 09:34:31 +0800 Subject: [PATCH 1/2] Create haar_classifier.py This is the 'beginner's guide' to implementing a haar cascading classifier to detect a fact in an image. --- computer_vision/haar_classifier.py | 68 ++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) create mode 100644 computer_vision/haar_classifier.py diff --git a/computer_vision/haar_classifier.py b/computer_vision/haar_classifier.py new file mode 100644 index 000000000000..19575dd4b8a9 --- /dev/null +++ b/computer_vision/haar_classifier.py @@ -0,0 +1,68 @@ +""" +Haar Cascading Classifiers + +Objective : Detect Object (Face) + +Resources Haar Classifiers : +https://www.analyticsvidhya.com/blog/2022/04/object-detection-using-haar-cascade-opencv/#:~:text=Haar%20cascade%20is%20an%20algorithm,%2C%20buildings%2C%20fruits%2C%20etc +https://machinelearningmastery.com/using-haar-cascade-for-object-detection/ +Resource for Haar classifiers: https://github.com/opencv/opencv/tree/master/data/haarcascades + +Download photo from : +https://unsplash.com/ + +1. Open Colab and create new notebook. +2. Download a free to download photo from unsplash or use one of your own. +3. Upload the photo in the model +4. Use haar classifier to detect face in image +""" + +from google.colab.patches import cv2_imshow #to assist with image processing and showing +import cv2 +import numpy as np + + +# Defining the list of image URLs from Unsplash +foto = [ + "https://spash.com/[chosen photo URL]" + "https://splash.com/[chosen photo URL]" +] + +def load_image_from_url(url): + resp = urlopen(url) + image = np.asarray(bytearray(resp.read()), dtype="uint8") + image = cv2.imdecode(image, cv2.IMREAD_COLOR) + return image + +for i, image_url in enumerate(foto): + img = load_image_from_url(image_url) + print(f"Displaying image {i+1}") + cv2_imshow(img) +#The above code should be in one cell and display the photo you chose. + +###########next cell############## + +# Loading the pre-trained Haar cascade for face detection +face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') #you can use others but this one is used the most. Resource link above. + +#detecting the face and covert to grayscale +def detect_faces(image): + gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) + faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=20, minSize=(20,20)) #Setting general parameters. Resouces above to understand more. + for (x, y, w, h) in faces: + cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2) #blue colour bounding box + return image + + +# Processing each loaded and resized image with Haar cascade +for i, url in enumerate(foto, start=1): + img=load_image_from_url(url) + img_with_faces = detect_faces(img.copy()) + print(f"Detecting Face {i}") + cv2_imshow(img_with_faces) #face + +#this should display the image with a blue bounding box. + + + + From 3abe01b3f1f584e60c18561fe92eb128f90a8b42 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Tue, 15 Oct 2024 01:36:12 +0000 Subject: [PATCH 2/2] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- computer_vision/haar_classifier.py | 40 ++++++++++++++++-------------- 1 file changed, 22 insertions(+), 18 deletions(-) diff --git a/computer_vision/haar_classifier.py b/computer_vision/haar_classifier.py index 19575dd4b8a9..8b9764ddbf10 100644 --- a/computer_vision/haar_classifier.py +++ b/computer_vision/haar_classifier.py @@ -17,16 +17,16 @@ 4. Use haar classifier to detect face in image """ -from google.colab.patches import cv2_imshow #to assist with image processing and showing +from google.colab.patches import ( + cv2_imshow, +) # to assist with image processing and showing import cv2 import numpy as np # Defining the list of image URLs from Unsplash -foto = [ - "https://spash.com/[chosen photo URL]" - "https://splash.com/[chosen photo URL]" -] +foto = ["https://spash.com/[chosen photo URL]" "https://splash.com/[chosen photo URL]"] + def load_image_from_url(url): resp = urlopen(url) @@ -34,35 +34,39 @@ def load_image_from_url(url): image = cv2.imdecode(image, cv2.IMREAD_COLOR) return image + for i, image_url in enumerate(foto): img = load_image_from_url(image_url) print(f"Displaying image {i+1}") cv2_imshow(img) -#The above code should be in one cell and display the photo you chose. +# The above code should be in one cell and display the photo you chose. ###########next cell############## # Loading the pre-trained Haar cascade for face detection -face_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_frontalface_default.xml') #you can use others but this one is used the most. Resource link above. +face_cascade = cv2.CascadeClassifier( + cv2.data.haarcascades + "haarcascade_frontalface_default.xml" +) # you can use others but this one is used the most. Resource link above. -#detecting the face and covert to grayscale + +# detecting the face and covert to grayscale def detect_faces(image): gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) - faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=20, minSize=(20,20)) #Setting general parameters. Resouces above to understand more. - for (x, y, w, h) in faces: - cv2.rectangle(image, (x, y), (x+w, y+h), (255, 0, 0), 2) #blue colour bounding box + faces = face_cascade.detectMultiScale( + gray, scaleFactor=1.1, minNeighbors=20, minSize=(20, 20) + ) # Setting general parameters. Resouces above to understand more. + for x, y, w, h in faces: + cv2.rectangle( + image, (x, y), (x + w, y + h), (255, 0, 0), 2 + ) # blue colour bounding box return image # Processing each loaded and resized image with Haar cascade for i, url in enumerate(foto, start=1): - img=load_image_from_url(url) + img = load_image_from_url(url) img_with_faces = detect_faces(img.copy()) print(f"Detecting Face {i}") - cv2_imshow(img_with_faces) #face - -#this should display the image with a blue bounding box. - - - + cv2_imshow(img_with_faces) # face +# this should display the image with a blue bounding box.