def get_FPS(self, image, test_interval): # 调整图片使其符合输入要求 image_shape = np.array(np.shape(image)[0:2]) crop_img,x_offset,y_offset = letterbox_image(image, (self.model_image_size[0],self.model_image_size[1])) photo = np.array(crop_img,dtype = np.float64) # 图片预处理,归一化 photo = preprocess_input(np.reshape(photo,[1,self.model_image_size[0],self.model_image_size[1],3])) preds = self.ssd_model.predict(photo) # 将预测结果进行解码 results = self.bbox_util.detection_out(preds, confidence_threshold=self.confidence) if len(results[0])>0: # 筛选出其中得分高于confidence的框 det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [i for i, conf in enumerate(det_conf) if conf >= self.confidence] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims(det_xmin[top_indices],-1),np.expand_dims(det_ymin[top_indices],-1),np.expand_dims(det_xmax[top_indices],-1),np.expand_dims(det_ymax[top_indices],-1) # 去掉灰条 boxes = ssd_correct_boxes(top_ymin,top_xmin,top_ymax,top_xmax,np.array([self.model_image_size[0],self.model_image_size[1]]),image_shape) t1 = time.time() for _ in range(test_interval): # 调整图片使其符合输入要求 image_shape = np.array(np.shape(image)[0:2]) crop_img,x_offset,y_offset = letterbox_image(image, (self.model_image_size[0],self.model_image_size[1])) photo = np.array(crop_img,dtype = np.float64) # 图片预处理,归一化 photo = preprocess_input(np.reshape(photo,[1,self.model_image_size[0],self.model_image_size[1],3])) preds = self.ssd_model.predict(photo) # 将预测结果进行解码 results = self.bbox_util.detection_out(preds, confidence_threshold=self.confidence) if len(results[0])>0: # 筛选出其中得分高于confidence的框 det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [i for i, conf in enumerate(det_conf) if conf >= self.confidence] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims(det_xmin[top_indices],-1),np.expand_dims(det_ymin[top_indices],-1),np.expand_dims(det_xmax[top_indices],-1),np.expand_dims(det_ymax[top_indices],-1) # 去掉灰条 boxes = ssd_correct_boxes(top_ymin,top_xmin,top_ymax,top_xmax,np.array([self.model_image_size[0],self.model_image_size[1]]),image_shape) t2 = time.time() tact_time = (t2 - t1) / test_interval return tact_time
def detect_image(self, image_id, image): self.confidence = 0.01 f = open("./input/detection-results/" + image_id + ".txt", "w") image_shape = np.array(np.shape(image)[0:2]) crop_img, x_offset, y_offset = letterbox_image( image, (self.model_image_size[0], self.model_image_size[1])) photo = np.array(crop_img, dtype=np.float64) # 图片预处理,归一化 photo = preprocess_input( np.reshape( photo, [1, self.model_image_size[0], self.model_image_size[1], 3])) preds = self.ssd_model.predict(photo) # 将预测结果进行解码 results = self.bbox_util.detection_out( preds, confidence_threshold=self.confidence) if len(results[0]) <= 0: f.close() return # 筛选出其中得分高于confidence的框 det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[ 0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= self.confidence ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims( det_xmin[top_indices], -1), np.expand_dims(det_ymin[top_indices], -1), np.expand_dims( det_xmax[top_indices], -1), np.expand_dims(det_ymax[top_indices], -1) # 去掉灰条 boxes = ssd_correct_boxes( top_ymin, top_xmin, top_ymax, top_xmax, np.array([self.model_image_size[0], self.model_image_size[1]]), image_shape) for i, c in enumerate(top_label_indices): predicted_class = self.class_names[int(c) - 1] score = str(top_conf[i]) top, left, bottom, right = boxes[i] f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str( int(top)), str(int(right)), str(int(bottom)))) f.close() return
def detect_image(self,image_id,image): self.confidence = 0.01 f = open("./input/detection-results/"+image_id+".txt","w") image_shape = np.array(np.shape(image)[0:2]) #---------------------------------------------------------# # 给图像增加灰条,实现不失真的resize # 也可以直接resize进行识别 #---------------------------------------------------------# if self.letterbox_image: crop_img = np.array(letterbox_image(image, (self.input_shape[1],self.input_shape[0]))) else: crop_img = image.convert('RGB') crop_img = crop_img.resize((self.input_shape[1],self.input_shape[0]), Image.BICUBIC) photo = np.array(crop_img,dtype = np.float64) # 图片预处理,归一化 photo = preprocess_input(np.reshape(photo,[1,self.input_shape[0],self.input_shape[1],3])) preds = self.ssd_model.predict(photo) # 将预测结果进行解码 results = self.bbox_util.detection_out(preds, confidence_threshold=self.confidence) if len(results[0])<=0: f.close() return # 筛选出其中得分高于confidence的框 det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [i for i, conf in enumerate(det_conf) if conf >= self.confidence] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims(det_xmin[top_indices],-1),np.expand_dims(det_ymin[top_indices],-1),np.expand_dims(det_xmax[top_indices],-1),np.expand_dims(det_ymax[top_indices],-1) #-----------------------------------------------------------# # 去掉灰条部分 #-----------------------------------------------------------# if self.letterbox_image: boxes = ssd_correct_boxes(top_ymin,top_xmin,top_ymax,top_xmax,np.array([self.input_shape[0],self.input_shape[1]]),image_shape) else: top_xmin = top_xmin * image_shape[1] top_ymin = top_ymin * image_shape[0] top_xmax = top_xmax * image_shape[1] top_ymax = top_ymax * image_shape[0] boxes = np.concatenate([top_ymin,top_xmin,top_ymax,top_xmax], axis=-1) for i, c in enumerate(top_label_indices): predicted_class = self.class_names[int(c)-1] score = str(top_conf[i]) top, left, bottom, right = boxes[i] f.write("%s %s %s %s %s %s\n" % (predicted_class, score[:6], str(int(left)), str(int(top)), str(int(right)),str(int(bottom)))) f.close() return
def detect_image(self, image): image_shape = np.array(np.shape(image)[0:2]) crop_img, x_offset, y_offset = letterbox_image(image, (300, 300)) photo = np.array(crop_img, dtype=np.float64) self.predict_all = [] # 图片预处理,归一化 photo = tf.keras.applications.imagenet_utils.preprocess_input( np.reshape(photo, [1, 300, 300, 3])) #self.ssd_model.summary() preds = self.ssd_model.predict(photo) # 将预测结果进行解码 results = self.bbox_util.detection_out(preds) if len(results[0]) <= 0: return image # 筛选出其中得分高于confidence的框 det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[ 0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= self.confidence ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims( det_xmin[top_indices], -1), np.expand_dims(det_ymin[top_indices], -1), np.expand_dims( det_xmax[top_indices], -1), np.expand_dims(det_ymax[top_indices], -1) boxes = ssd_correct_boxes(top_ymin, top_xmin, top_ymax, top_xmax, np.array([300, 300]), image_shape) font = ImageFont.truetype(font='model_data/simhei.ttf', size=np.floor(3e-2 * np.shape(image)[1] + 0.5).astype('int32')) thickness = (np.shape(image)[0] + np.shape(image)[1]) // 300 for i, c in enumerate(top_label_indices): predicted_class = self.class_names[int(c) - 1] score = top_conf[i] top, left, bottom, right = boxes[i] top = top - 5 left = left - 5 bottom = bottom + 5 right = right + 5 top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min( np.shape(image)[0], np.floor(bottom + 0.5).astype('int32')) right = min( np.shape(image)[1], np.floor(right + 0.5).astype('int32')) self.result_ = '{} {} {} {} {} {}'.format( "".join(predicted_class.split(" ")), score, left, top, right, bottom) self.predict_all.append(self.result_) # 画框框 label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) label = label.encode('utf-8') print(label) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=255) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=255) draw.text(text_origin, str(label, 'UTF-8'), fill=0, font=font) del draw return image
def detect_image(self, image): image_shape = np.array(np.shape(image)[0:2]) #---------------------------------------------------------# # 给图像增加灰条,实现不失真的resize # 也可以直接resize进行识别 #---------------------------------------------------------# if self.letterbox_image: crop_img = np.array( letterbox_image(image, (self.input_shape[1], self.input_shape[0]))) else: crop_img = image.convert('RGB') crop_img = crop_img.resize( (self.input_shape[1], self.input_shape[0]), Image.BICUBIC) photo = np.array(crop_img, dtype=np.float64) #-----------------------------------------------------------# # 图片预处理,归一化。 #-----------------------------------------------------------# photo = preprocess_input( np.reshape(photo, [1, self.input_shape[0], self.input_shape[1], 3])) preds = self.ssd_model.predict(photo) #-----------------------------------------------------------# # 将预测结果进行解码 #-----------------------------------------------------------# results = self.bbox_util.detection_out( preds, confidence_threshold=self.confidence) #--------------------------------------# # 如果没有检测到物体,则返回原图 #--------------------------------------# if len(results[0]) <= 0: return image #-----------------------------------------------------------# # 筛选出其中得分高于confidence的框 #-----------------------------------------------------------# det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[ 0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= self.confidence ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims( det_xmin[top_indices], -1), np.expand_dims(det_ymin[top_indices], -1), np.expand_dims( det_xmax[top_indices], -1), np.expand_dims(det_ymax[top_indices], -1) #-----------------------------------------------------------# # 去掉灰条部分 #-----------------------------------------------------------# if self.letterbox_image: boxes = ssd_correct_boxes( top_ymin, top_xmin, top_ymax, top_xmax, np.array([self.input_shape[0], self.input_shape[1]]), image_shape) else: top_xmin = top_xmin * image_shape[1] top_ymin = top_ymin * image_shape[0] top_xmax = top_xmax * image_shape[1] top_ymax = top_ymax * image_shape[0] boxes = np.concatenate([top_ymin, top_xmin, top_ymax, top_xmax], axis=-1) font = ImageFont.truetype(font='model_data/simhei.ttf', size=np.floor(3e-2 * np.shape(image)[1] + 0.5).astype('int32')) thickness = max( (np.shape(image)[0] + np.shape(image)[1]) // self.input_shape[0], 1) for i, c in enumerate(top_label_indices): predicted_class = self.class_names[int(c) - 1] score = top_conf[i] top, left, bottom, right = boxes[i] top = top - 5 left = left - 5 bottom = bottom + 5 right = right + 5 top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min( np.shape(image)[0], np.floor(bottom + 0.5).astype('int32')) right = min( np.shape(image)[1], np.floor(right + 0.5).astype('int32')) # 画框框 label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) label = label.encode('utf-8') print(label, top, left, bottom, right) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[int(c) - 1]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[int(c) - 1]) draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font) del draw return image
def detect_image(self, image): if type(image) is np.ndarray: image = Image.fromarray(cv2.cvtColor(image, cv2.COLOR_BGR2RGB)) image_shape = np.array(np.shape(image)[0:2]) crop_img, x_offset, y_offset = letterbox_image( image, (self.model_image_size[0], self.model_image_size[1])) photo = np.array(crop_img, dtype=np.float64) # 图片预处理,归一化 photo = preprocess_input( np.reshape( photo, [1, self.model_image_size[0], self.model_image_size[1], 3])) preds = self.ssd_model.predict(photo) # 将预测结果进行解码 results = self.bbox_util.detection_out( preds, confidence_threshold=self.confidence) if len(results[0]) <= 0: return image # 筛选出其中得分高于confidence的框 det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[ 0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= self.confidence ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims( det_xmin[top_indices], -1), np.expand_dims(det_ymin[top_indices], -1), np.expand_dims( det_xmax[top_indices], -1), np.expand_dims(det_ymax[top_indices], -1) # 去掉灰条 boxes = ssd_correct_boxes( top_ymin, top_xmin, top_ymax, top_xmax, np.array([self.model_image_size[0], self.model_image_size[1]]), image_shape) font = ImageFont.truetype(font='model_data/simhei.ttf', size=np.floor(3e-2 * np.shape(image)[1] + 0.5).astype('int32')) thickness = (np.shape(image)[0] + np.shape(image)[1]) // self.model_image_size[0] for i, c in enumerate(top_label_indices): predicted_class = self.class_names[int(c) - 1] score = top_conf[i] top, left, bottom, right = boxes[i] top = top - 5 left = left - 5 bottom = bottom + 5 right = right + 5 top = max(0, np.floor(top + 0.5).astype('int32')) left = max(0, np.floor(left + 0.5).astype('int32')) bottom = min( np.shape(image)[0], np.floor(bottom + 0.5).astype('int32')) right = min( np.shape(image)[1], np.floor(right + 0.5).astype('int32')) # 画框框 label = '{} {:.2f}'.format(predicted_class, score) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) label = label.encode('utf-8') print(label) if top - label_size[1] >= 0: text_origin = np.array([left, top - label_size[1]]) else: text_origin = np.array([left, top + 1]) for i in range(thickness): draw.rectangle([left + i, top + i, right - i, bottom - i], outline=self.colors[int(c) - 1]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[int(c) - 1]) draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font) del draw return image
def detect_image(self, image): image_shape = np.array(np.shape(image)[0:2]) crop_image,x_offset,y_offset = letterbox_image(image, (self.input_shape[0], self.input_shape[1])) photo = np.array(crop_image, dtype=np.float64) # Normalization photo = preprocess_input(np.reshape(photo, [1, self.input_shape[0], self.input_shape[1], 3])) preds = self.get_pred(photo).numpy() # Decode results = self.bbox_util.detection_out(preds, confidence_threshold=config.CONFIDENCE) if len(results[0]) <= 0: return image det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:,2], results[0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [i for i, conf in enumerate(det_conf) if conf >= config.CONFIDENCE] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin = np.expand_dims(det_xmin[top_indices], axis=-1) top_ymin = np.expand_dims(det_ymin[top_indices], axis=-1) top_xmax = np.expand_dims(det_xmax[top_indices], axis=-1) top_ymax = np.expand_dims(det_ymax[top_indices], axis=-1) boxes = ssd_correct_boxes(top_ymin, top_xmin, top_ymax, top_xmax, np.array((self.input_shape[0], self.input_shape[1])), image_shape) font = ImageFont.truetype(font='simhei.ttf',size=np.floor(3e-2 * np.shape(image)[1] + 0.5).astype('int32')) thickness = (np.shape(image)[0] + np.shape(image)[1]) // self.input_shape[0] for i, c in enumerate(top_label_indices): predicted_class = self.classes[int(c) - 1] score = top_conf[i] ymin, xmin, ymax, xmax = boxes[i] ymin = ymin - 5 xmin = xmin - 5 ymax = ymax - 5 xmax = xmax - 5 ymin = max(0, np.floor(ymin + 0.5).astype('int32')) xmin = max(0, np.floor(xmin + 0.5).astype('int32')) ymax = min(np.shape(image)[0], np.floor(ymax + 0.5).astype('int32')) xmax = min(np.shape(image)[1], np.floor(xmax + 0.5).astype('int32')) # draw Bounding box label = "{}:{:.2f}".format(predicted_class, score) print(label) draw = ImageDraw.Draw(image) label_size = draw.textsize(label, font) label = label.encode('utf-8') if ymin - label_size[1] >= 0: text_origin = np.array((xmin, ymin - label_size[1])) else: text_origin = np.array((xmin, ymin + 1)) for i in range(thickness): draw.rectangle( [xmin + i, ymin + i, xmax - i, ymax - i], outline=self.colors[int(c)-1]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[int(c)-1]) draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font) del draw return image
def get_FPS(self, image, test_interval): image_shape = np.array(np.shape(image)[0:2]) #---------------------------------------------------------# # 给图像增加灰条,实现不失真的resize # 也可以直接resize进行识别 #---------------------------------------------------------# if self.letterbox_image: crop_img = np.array( letterbox_image(image, (self.input_shape[1], self.input_shape[0]))) else: crop_img = image.convert('RGB') crop_img = crop_img.resize( (self.input_shape[1], self.input_shape[0]), Image.BICUBIC) photo = np.array(crop_img, dtype=np.float64) photo = preprocess_input( np.reshape(photo, [1, self.input_shape[0], self.input_shape[1], 3])) preds = self.ssd_model.predict(photo) results = self.bbox_util.detection_out( preds, confidence_threshold=self.confidence) if len(results[0]) > 0: det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[0][:, 2], results[ 0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= self.confidence ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims( det_xmin[top_indices], -1), np.expand_dims(det_ymin[top_indices], -1), np.expand_dims( det_xmax[top_indices], -1), np.expand_dims(det_ymax[top_indices], -1) #-----------------------------------------------------------# # 去掉灰条部分 #-----------------------------------------------------------# if self.letterbox_image: boxes = ssd_correct_boxes( top_ymin, top_xmin, top_ymax, top_xmax, np.array([self.input_shape[0], self.input_shape[1]]), image_shape) else: top_xmin = top_xmin * image_shape[1] top_ymin = top_ymin * image_shape[0] top_xmax = top_xmax * image_shape[1] top_ymax = top_ymax * image_shape[0] boxes = np.concatenate( [top_ymin, top_xmin, top_ymax, top_xmax], axis=-1) t1 = time.time() for _ in range(test_interval): preds = self.ssd_model.predict(photo) results = self.bbox_util.detection_out( preds, confidence_threshold=self.confidence) if len(results[0]) > 0: det_label = results[0][:, 0] det_conf = results[0][:, 1] det_xmin, det_ymin, det_xmax, det_ymax = results[ 0][:, 2], results[0][:, 3], results[0][:, 4], results[0][:, 5] top_indices = [ i for i, conf in enumerate(det_conf) if conf >= self.confidence ] top_conf = det_conf[top_indices] top_label_indices = det_label[top_indices].tolist() top_xmin, top_ymin, top_xmax, top_ymax = np.expand_dims( det_xmin[top_indices], -1), np.expand_dims( det_ymin[top_indices], -1), np.expand_dims( det_xmax[top_indices], -1), np.expand_dims(det_ymax[top_indices], -1) #-----------------------------------------------------------# # 去掉灰条部分 #-----------------------------------------------------------# if self.letterbox_image: boxes = ssd_correct_boxes( top_ymin, top_xmin, top_ymax, top_xmax, np.array([self.input_shape[0], self.input_shape[1]]), image_shape) else: top_xmin = top_xmin * image_shape[1] top_ymin = top_ymin * image_shape[0] top_xmax = top_xmax * image_shape[1] top_ymax = top_ymax * image_shape[0] boxes = np.concatenate( [top_ymin, top_xmin, top_ymax, top_xmax], axis=-1) t2 = time.time() tact_time = (t2 - t1) / test_interval return tact_time