def detect(self, image_obj, bbox_objs=None, batch_size=2): if bbox_objs is None: x2, y2 = image_obj.pil_image_obj.size bbox_objs = [BoundedBoxObject(0, 0, x2, y2, '', 0, '')] n_bbox = len(bbox_objs) result_objects = [] for row_idx, batch_start, batch_end in chunk_with_index( range(n_bbox), batch_size): batch_bbox_objs = bbox_objs[batch_start:batch_end] objs = image_obj.fetch_bbox_pil_objs(batch_bbox_objs) objects_frame = resize_and_stack_image_objs(self.image_size, objs) objects_frame = np.transpose(objects_frame, (0, 3, 1, 2)) objects_embedding = self.model.get_faces_feature(objects_frame) similar_matrix = objects_embedding.dot( self.registered_images_embedding.T) detected_idx = similar_matrix.argmax(1) for idx, bbox in enumerate(batch_bbox_objs): x1, y1, x2, y2, _, _, _ = bbox label = self.registered_ids[detected_idx[idx]] score = similar_matrix[idx, detected_idx[idx]] if score < self.threshold: label = self.unknown result_objects.append( BoundedBoxObject(x1, y1, x2, y2, label, score, '')) image_dict = { 'image_id': image_obj.image_id, 'detected_objects': result_objects, } detection_result = DetectionResult(image_dict) return detection_result
def detect(self, image_obj) -> DetectionResult: frame = np.array(image_obj.pil_image_obj) if self.resize is not None: frame = cv2.resize(frame, self.resize) ori_height, ori_width = frame.shape[:2] x_scale_up_ratio = ori_width / self.resize[0] y_scale_up_ratio = ori_height / self.resize[1] else: x_scale_up_ratio = 1 y_scale_up_ratio = 1 gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY) persons = self.person_cascade.detectMultiScale(gray, 1.1, 1) persons = np.array([[x, y, x + w, y + h] for (x, y, w, h) in persons]) picked_idx = non_max_suppression(persons, probs=None, overlapThresh=0.65) detected_objects = [] for idx in picked_idx: x1, y1, x2, y2 = persons[idx] x1 = x1 * x_scale_up_ratio x2 = x2 * x_scale_up_ratio y1 = y1 * y_scale_up_ratio y2 = y2 * y_scale_up_ratio detected_objects.append( BoundedBoxObject(x1, y1, x2, y2, 'person', 0, '')) image_dict = { 'image_id': image_obj.image_id, 'detected_objects': detected_objects, } detection_result = DetectionResult(image_dict) return detection_result
def detect(self, image_obj) -> DetectionResult: frame = np.array(image_obj.pil_image_obj) rects, weights = self.hog.detectMultiScale(frame, winStride=(3, 3), padding=(16, 16), scale=1.05) rects = np.array([[x, y, x + w, y + h] for (x, y, w, h) in rects]) picked_idx = non_max_suppression(rects, probs=None, overlapThresh=0.65) detected_objects = [] for idx in picked_idx: x1, y1, x2, y2 = rects[idx] score = weights[idx][0] if score < self.threshold: continue detected_objects.append( BoundedBoxObject(x1, y1, x2, y2, 'person', score, '')) image_dict = { 'image_id': image_obj.image_id, 'detected_objects': detected_objects, } detection_result = DetectionResult(image_dict) return detection_result
def detect_frame(self, frame): detected_objects = [] ret = self.face_detector.detect_face(frame, det_type=0) if ret: bbox, _ = ret for idx in range(bbox.shape[0]): x1, y1, x2, y2, score = bbox[idx] detected_objects.append(BoundedBoxObject(x1, y1, x2, y2, 'face', score, '')) image_dict = { 'image_id': 'frame', 'detected_objects': detected_objects, } detection_result = DetectionResult(image_dict) return(detection_result)
def detect(self, image_obj) -> DetectionResult: frame = np.array(image_obj.pil_image_obj) ori_height, ori_width = frame.shape[:2] frame_resized = cv2.resize(frame, self.resize) x_scale_up_ratio = ori_width / self.resize[0] y_scale_up_ratio = ori_height / self.resize[1] blob = cv2.dnn.blobFromImage(frame_resized, 0.007843, (300, 300), (127.5, 127.5, 127.5), False) # Set to network the input blob self.net.setInput(blob) # Prediction of network detections = self.net.forward() detected_objects = [] for i in range(detections.shape[2]): confidence = detections[0, 0, i, 2] # Confidence of prediction if confidence < self.threshold: # Filter prediction continue class_id = int(detections[0, 0, i, 1]) # Class label label = self.classNames[class_id] # Object location x1 = int(detections[0, 0, i, 3] * self.resize[0] * x_scale_up_ratio) y1 = int(detections[0, 0, i, 4] * self.resize[1] * y_scale_up_ratio) x2 = int(detections[0, 0, i, 5] * self.resize[0] * x_scale_up_ratio) y2 = int(detections[0, 0, i, 6] * self.resize[1] * y_scale_up_ratio) detected_objects.append( BoundedBoxObject(x1, y1, x2, y2, label, confidence, '')) image_dict = { 'image_id': image_obj.image_id, 'detected_objects': detected_objects, } detection_result = DetectionResult(image_dict) return detection_result
def detect(self, image_obj, label='face', train=True): detected_objects = [] frame = swap_channel_rgb_bgr(np.array(image_obj.pil_image_obj)) ret = self.face_detector.detect_face(frame, det_type=0) if ret: bbox, _ = ret # boundingboxes shape n, 5 for idx in range(bbox.shape[0]): x1, y1, x2, y2, score = bbox[idx] if train: score=1 detected_objects.append(BoundedBoxObject(x1, y1, x2, y2, label, score, '')) image_dict = { 'image_id': image_obj.image_id, 'detected_objects': detected_objects, } detection_result = DetectionResult(image_dict) return detection_result
face_detector = MtcnnFaceDetector(args.mtcnn_path, ctx=mx.gpu(0)) # Préprocess de l'image raw_image_path = 'demo/183club/test_imag1.jpg' #ImageId is used to standardize the image_id format train_image_id = ImageId(channel='demo', timestamp=arrow.now().timestamp, file_format='jpg') train_image_obj = Image(train_image_id, raw_image_path=raw_image_path) register_image_bbox_objs = [ BoundedBoxObject(x1=1526, y1=755, x2=1730, y2=1007, label='Zoe', score=1, meta=''), #BoundedBoxObject(x1=946, y1=551, x2=1149, y2=784, label='Amine', score=1, meta=''), BoundedBoxObject(x1=1364, y1=564, x2=1492, y2=720, label='Lu', score=1, meta='') #BoundedBoxObject(x1=2286, y1=1272, x2=2629, y2=1738, label='Hedi', score=1, meta='') ] objs = train_image_obj.fetch_bbox_pil_objs(register_image_bbox_objs)
output0_data = results.as_numpy("prob") n_bbox = int(output0_data[0, 0, 0, 0]) bbox_matrix = output0_data[0, 1:(n_bbox * 7 + 1), 0, 0].reshape(-1, 7) detected_objects = [] if n_bbox: labels = set(bbox_matrix[:, 5]) for label in labels: idices = np.where((bbox_matrix[:, 5] == label) & (bbox_matrix[:, 6] >= BBOX_CONF_THRESH)) sub_bbox_matrix = bbox_matrix[idices] box_confidences = bbox_matrix[idices, 6] keep_idices = nms(sub_bbox_matrix[:, :4], sub_bbox_matrix[:, 6]) sub_bbox_matrix = sub_bbox_matrix[keep_idices] for idx in range(sub_bbox_matrix.shape[0]): x, y, w, h, _, label, score = sub_bbox_matrix[idx, :] x1 = (x - w / 2) / scale_ratio x2 = min((x + w / 2) / scale_ratio, ori_w) y1 = (y - h / 2) / scale_ratio y2 = min((y + h / 2) / scale_ratio, ori_h) if x1 == x2: continue if y1 == y2: continue detected_objects.append( BoundedBoxObject(x1, y1, x2, y2, label, score, "")) ImageHandler.draw_bbox(image_obj.pil_image_obj, detected_objects) ImageHandler.save(image_obj.pil_image_obj, FLAGS.drawn_img)