Computer Vision News - April 2022

12 Computer Vision Tool Below is the loading of the OpenCv face detector module and the detection of the face coordinators on the image detector = cv2.CascadeClassifier("/content/Facial-Recognition-MMAI844-Tutorial/xml/frontal_face. xml") scale_factor = 1.2 min_neighbors = 5 min_size = (40, 40) biggest_only = True flags = cv2.CASCADE_FIND_BIGGEST_OBJECT | \ cv2.CASCADE_DO_ROUGH_SEARCH if biggest_only else \ cv2.CASCADE_SCALE_IMAGE faces_coord = detector.detectMultiScale(frame, scaleFactor=scale_factor, minNeighbors=min_neighbors, minSize=min_size, flags=flags) print("Type: " + str(type(faces_coord))) print("this is face coordinator in the picture is {}".format(faces_coord)) print("Face is successfully detected!! Let draw a box on the picture") Define a function to draw a rectangle around the detected face def draw_rectangle(image, coords): for (x, y, w, h) in coords: w_rm = int(0.2 * w / 2) cv2.rectangle(image, (x + w_rm, y), (x + w - w_rm, y + h), (0, 0, 255), 8) draw_rectangle(frame,faces_coord) cv2.putText(frame, name, (faces_coord[0][0], faces_coord[0][1]), cv2.FONT_HERSHEY_SIMPLEX , 2, (66, 53, 243), 6) plt_show(frame) ############################################################ # Wrap up the code into a face detector module for later use ############################################################ class FaceDetector(object): def __init__(self, xml_path): self.classifier = cv2.CascadeClassifier(xml_path) def detect(self, image, biggest_only=True): scale_factor = 1.2 min_neighbors = 5 min_size = (30, 30) biggest_only = True flags = cv2.CASCADE_FIND_BIGGEST_OBJECT | \ cv2.CASCADE_DO_ROUGH_SEARCH if biggest_only else \ cv2.CASCADE_SCALE_IMAGE faces_coord = self.classifier.detectMultiScale(image, scaleFactor=scale_factor, minNeighbors=min_neighbors, minSize=min_size, flags=flags) return faces_coord

RkJQdWJsaXNoZXIy NTc3NzU=