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