Thursday, April 27, 2023

Face Recognition with OpenCV python code

 import cv2


# Load the Haar Cascade face detection classifier

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


# Load the trained face recognition model

recognizer = cv2.face.LBPHFaceRecognizer_create()

recognizer.read('trained_model.xml')


# Set the video capture device (0 is usually the default webcam)

cap = cv2.VideoCapture(0)


while True:

    # Read a frame from the video stream

    ret, frame = cap.read()


    # Convert the frame to grayscale

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)


    # Detect faces in the grayscale frame

    faces = face_cascade.detectMultiScale(gray, scaleFactor=1.2, minNeighbors=5)


    # Loop through each face detected

    for (x, y, w, h) in faces:

        # Crop the face region from the grayscale frame

        face_gray = gray[y:y+h, x:x+w]


        # Resize the face image to match the training image size

        face_gray = cv2.resize(face_gray, (100, 100))


        # Predict the label (person) of the face using the trained model

        label, confidence = recognizer.predict(face_gray)


        # Draw a rectangle around the face and display the predicted label

        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

        cv2.putText(frame, f'Person {label} ({confidence:.2f})', (x, y-10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)


    # Display the frame

    cv2.imshow('Face Recognition', frame)


    # Exit the loop if 'q' is pressed

    if cv2.waitKey(1) == ord('q'):

        break


# Release the video capture device and close the OpenCV window

cap.release()

cv2.destroyAllWindows()


 Note that this code assumes you have already trained a face recognition model and saved it to a file (in this case, trained_model.xml). If you haven't done this yet, you will need to train the model on a dataset of labeled face images before you can use it for recognition.






Object Detection with OpenCV-Python code

 import cv2


# Load the pre-trained face detection classifier

face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')


# Load the image

img = cv2.imread('test.jpg')


# Convert the image to grayscale

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)


# Detect faces in the grayscale image

faces = face_cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=5, minSize=(30, 30))


# Draw rectangles around the detected faces

for (x, y, w, h) in faces:

    cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)


# Display the result

cv2.imshow('img', img)

cv2.waitKey(0)

cv2.destroyAllWindows()

In this example, the cv2.CascadeClassifier function is used to load the pre-trained Haar Cascade classifier file for face detection. The detectMultiScale function is used to detect faces in the image. The scaleFactor parameter determines how much the image size is reduced at each image scale, the minNeighbors parameter controls the number of neighbors a detection candidate needs to retain, and the minSize parameter specifies the minimum size of the face to detect. Finally, the cv2.rectangle function is used to draw a rectangle around each detected face in the image, and the cv2.imshow function is used to display the result.

Wednesday, April 26, 2023

How cache can be enabled for embeded text as well for search query results in Azure AI ?

 Great question, Rahul! Caching in the context of Azure AI (especially when using **RAG pipelines with Azure OpenAI + Azure AI Search**) can...