def detect(self, image_cv) -> DetectorResponse: #TO BE MODIFIED ---------------------- gender = False #TO BE MODIFIED ---------------------- faces_bbox, faces_conf = cv.detect_face(image_cv) bbox = [] label = [] conf = [] for bbox_item, conf_item in zip(faces_bbox, faces_conf): startX = bbox_item[0] startY = bbox_item[1] endX = bbox_item[2] endY = bbox_item[3] c = float(conf_item) l = 'person' b = [int(startX), int(startY), int(endX), int(endY)] bbox.append(b) label.append(l) conf.append(c) # for faces_item, conf_item in zip(faces, conf): # logger.debug("type={}, confidence={:.2f}%".format(faces_item,conf_item)) # (startX, startY) = faces_item[0], faces_item[1] # (endX, endY) = faces_item[2], faces_item[3] # c = float(conf_item) # l = 'face' # b = [int(startX), int(startY), int(endX), int(endY)] # bbox.append(b) # label.append(l) # conf.append(c) # if gender == True: # face_crop = np.copy(image_cv[startY:endY, startX:endX]) # (gender_label_arr, gender_confidence_arr) = cv.detect_gender(face_crop) # idx = np.argmax(gender_confidence_arr) # gender_label = gender_label_arr[idx] # gender_confidence = "{:.2f}%".format(gender_confidence_arr[idx] * 100) # #obj['gender'] = gender_label # #obj['gender_confidence'] = gender_confidence model_response = DetectorResponse(self.get_model_name()) for l, c, b in zip(label, conf, bbox): model_response.add(b, l, c, self.get_model_name()) return model_response
def detect( self, image_cv ) -> DetectorResponse: bbox, label, conf = cv.detect_common_objects(image_cv) model_response = DetectorResponse(self.get_model_name()) for l, c, b in zip(label, conf, bbox): model_response.add(b,l,c,self.get_model_name()) return model_response
def detect(self, image_cv) -> DetectorResponse: logger.debug("[REKOGNITION] request via boto3...") imgHeight, imgWidth = image_cv.shape[:2] pil_img = Image.fromarray( image_cv ) # convert opencv frame (with type()==numpy) into PIL Image stream = io.BytesIO() pil_img.save(stream, format='JPEG') # convert PIL Image to Bytes bin_img = stream.getvalue() logger.debug("request via boto3...") response = self.client.detect_labels(Image={'Bytes': bin_img}) #logger.debug("Reading {}".format(response)) logger.debug("...response received") bbox = [] label = [] conf = [] for reko_label in response['Labels']: for instance in reko_label['Instances']: if reko_label['Name'] in self.rekognition_labels: #extracting bounding box coordinates left = int(imgWidth * instance['BoundingBox']['Left']) top = int(imgHeight * instance['BoundingBox']['Top']) width = int(imgWidth * instance['BoundingBox']['Width']) height = int(imgHeight * instance['BoundingBox']['Height']) x1 = left y1 = top x2 = left + width y2 = top + height l = self.__convert_label(reko_label['Name']) c = float(reko_label['Confidence'] / 100) b = [x1, y1, x2, y2] bbox.append(b) label.append(l) conf.append(c) model_response = DetectorResponse(self.get_model_name()) for l, c, b in zip(label, conf, bbox): model_response.add(b, l, c, self.get_model_name()) return model_response
def detect(self, image_cv) -> DetectorResponse: pil_image = Image.fromarray( image_cv ) # convert opencv frame (with type()==numpy) into PIL Image # Run inference. ans = self.engine.detect_with_image(pil_image, threshold=0.05, keep_aspect_ratio=True, relative_coord=False, top_k=10) bbox = [] conf = [] label = [] # Display result. if ans: for obj in ans: #sample output #score = 0.97265625 #box = [417.078184068203, 436.7141185646848, 2443.3632068037987, 1612.3385782686541] b = obj.bounding_box.flatten().tolist() b = [int(i) for i in b] #convert to int c = float(obj.score) l = 'person' bbox.append(b) conf.append(c) label.append(l) else: logger.debug('No face detected!') model_response = DetectorResponse(self.get_model_name()) for l, c, b in zip(label, conf, bbox): model_response.add(b, l, c, self.get_model_name()) return model_response
def nms(self, merged: DetectorResponse, conf_threshold: float, nms_threshold: float) -> (DetectorResponse, object): start_total = datetime.datetime.now() b, l, c, m = merged.get_blcm_vectors() indices = cv2.dnn.NMSBoxes(b, c, conf_threshold, nms_threshold) nms_out = DetectorResponse("nms") for i in indices: i = i[0] nms_out.add(b[i], l[i], c[i], m[i]) for record in nms_out.data: print("nms: {},{},{},{}".format(record.label, record.conf, record.model_name, record.bbox)) stop_total = datetime.datetime.now() total_time = stop_total - start_total logger.info('TOTAL detection took {}'.format(total_time)) return nms_out, total_time
def merge(self, batch_response: RunBatchResponse) -> (DetectorResponse, object): logger.debug('merging...') start_total = datetime.datetime.now() merged = DetectorResponse("merged") for response_item in batch_response.response_list: merged.data += response_item.model_response.data for record in merged.data: print("merged: {},{},{},{}".format(record.label, record.conf, record.model_name, record.bbox)) stop_total = datetime.datetime.now() total_time = stop_total - start_total logger.info('TOTAL detection took {}'.format(total_time)) return merged, total_time
def detect(self, image_cv) -> DetectorResponse: pil_image = Image.fromarray( image_cv ) # convert opencv frame (with type()==numpy) into PIL Image # Run inference. ans = self.engine.detect_with_image(pil_image, threshold=0.05, keep_aspect_ratio=True, relative_coord=False, top_k=10) pre_bbox = [] pre_label = [] pre_conf = [] # Display result. if ans: for obj in ans: if self.labels: #sample output #score = 0.97265625 #box = [417.078184068203, 436.7141185646848, 2443.3632068037987, 1612.3385782686541] box = obj.bounding_box.flatten().tolist() box = [int(i) for i in box] #convert to int l = self.labels[obj.label_id] c = float(obj.score) b = box # x = int(box[0]) # y = int(box[1]) # w = int(box[2] - box[0]) # h = int(box[3] - box[1]) # b = [x,y,w,h] pre_bbox.append(b) pre_label.append(l) pre_conf.append(c) # if c > conf_min: # post_bbox.append(b) # post_label.append(l) # post_conf.append(c) # else: # logger.debug("DISCARDED as conf={} and threashold={}".format(c, conf_min)) # Perform non maximum suppression to eliminate redundant overlapping boxes with # lower confidences. #la prima soglia rappresenta la confidenza minima che accetti per un bbox #ti pare di aver capito che la seconda soglia rappresenta #l'area in % di sovrapposizione tra due bbox indices = cv2.dnn.NMSBoxes(pre_bbox, pre_conf, 0.2, 0.3) logger.debug("NMS indices {}".format(indices)) bbox = [] label = [] conf = [] for i in indices: j = i[0] bbox.append(pre_bbox[j]) label.append(pre_label[j]) conf.append(pre_conf[j]) else: logger.debug('No object detected!') model_response = DetectorResponse(self.get_model_name()) for l, c, b in zip(label, conf, bbox): model_response.add(b, l, c, self.get_model_name()) return model_response
def detect(self, image_cv) -> DetectorResponse: Height, Width = image_cv.shape[:2] scale = 0.00392 blob = cv2.dnn.blobFromImage(image_cv, scale, (416, 416), (0, 0, 0), True, crop=False) self.net.setInput(blob) outs = self.net.forward(self.__get_output_layers()) class_ids = [] confidences = [] boxes = [] conf_threshold = 0.5 nms_threshold = 0.4 for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x = int(detection[0] * Width) center_y = int(detection[1] * Height) w = int(detection[2] * Width) h = int(detection[3] * Height) x = center_x - w / 2 y = center_y - h / 2 class_ids.append(class_id) confidences.append(float(confidence)) boxes.append([x, y, w, h]) indices = cv2.dnn.NMSBoxes(boxes, confidences, conf_threshold, nms_threshold) bbox = [] label = [] conf = [] for i in indices: i = i[0] box = boxes[i] x = box[0] y = box[1] w = box[2] h = box[3] bbox.append([ int(round(x)), int(round(y)), int(round(x + w)), int(round(y + h)) ]) label.append(str(self.classes[class_ids[i]])) conf.append(float(confidences[i])) model_response = DetectorResponse(self.get_model_name()) for l, c, b in zip(label, conf, bbox): model_response.add(b, l, c, self.get_model_name()) return model_response