def visualzation(model_path, img_path): model = tf.saved_model.load(model_path) perdictor = model.signatures['serving_default'] image = Image.open(img_path) # (batch, 2) image_shape = np.expand_dims(np.array([image.size[1], image.size[0]], dtype='float32'), axis=0) # letter box image_letter = letterbox_image(image, (416, 416)) image_array = np.expand_dims(np.array(image_letter, dtype='float32') / 255.0, axis=0) image_constant = tf.constant(image_array, dtype=tf.float32) image_shape = tf.constant(image_shape, dtype=tf.float32) # infer results = perdictor(input_2=image_constant, input_3=image_shape) # dictionary results_ = [] for key, value in results.items(): results_.append(value) # ------------------------------------------------------------------------------------- # visualize boxes = results_[0] scores = results_[1] classes = results_[2] # starting draw bounding boxes font = ImageFont.truetype(font='font/simhei.ttf', size=np.floor(2e-2 * image.size[1] + 0.5).astype('int32')) # thickness of bounding box and this thickness is changing according to img_size thickness = (image.size[0] + image.size[1]) // 500 for i, c in list(enumerate(classes)): predicted_class = class_names[c] box = boxes[i] score = scores[i] top, left, bottom, right = box 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(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], 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 j in range(thickness): draw.rectangle([left + j, top + j, right - j, bottom - j], outline=(0, 0, 255)) draw.rectangle([tuple(text_origin), tuple(text_origin + label_size)], fill=(128, 128, 128)) draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font) del draw return image
class_ = tf.concat(class_, axis=0) total_model = keras.Model([inputs, image_shape], [boxes_, scores_, class_]) # 5.save model tf.saved_model.save(total_model, save_path) return total_model if __name__ == '__main__': inputs_ = keras.Input(shape=(416, 416, 3)) img_shape = keras.Input(shape=()) model = save_model(inputs_, img_shape, (416, 416, 3), num_anchors, num_classes, score, iou) # test model # and through testing, the decoding codes are usable. image = Image.open(img_path) image_shape = np.array([image.size[1], image.size[0]], dtype='float32') image = letterbox_image(image, (416, 416)) image_array = np.expand_dims(np.array(image, dtype='float32') / 255.0, axis=0) boxes, scores, classes = model([image_array, image_shape]) print(classes) print(scores) print(boxes)
def detect_image(self, image, htimes=0): start = timer() # 调整图片使其符合输入要求 new_image_size = (image.width - (image.width % 32), image.height - (image.height % 32)) boxed_image = letterbox_image(image, new_image_size) # 得到灰度图片 image_data = np.array(boxed_image, dtype='float32') image_data /= 255. image_data = np.expand_dims(image_data, 0) # Add batch dimension. # 预测结果 out_boxes, out_scores, out_classes = self.sess.run( [self.boxes, self.scores, self.classes], feed_dict={ self.yolo_model.input: image_data, self.input_image_shape: [image.size[1], image.size[0]], K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), 'img')) # for i in out_classes: # if i==0: # htimes+=1 # break # if(htimes>2): # p= Process( target=winsound.PlaySound,args=("1.wav",winsound.SND_ALIAS)) # p.start() # htimes=0 # # p进程执行f函数,参数为'Qbob',注意后面的“,” # break # htimes = 0 # 设置字体 font = ImageFont.truetype(font='font/simhei.ttf', size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32')) thickness = (image.size[0] + image.size[1]) // 300 small_pic = [] for i, c in list(enumerate(out_classes)): predicted_class = self.class_names[c] box = out_boxes[i] score = out_scores[i] top, left, bottom, right = box 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(image.size[1], np.floor(bottom + 0.5).astype('int32')) right = min(image.size[0], 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[c]) draw.rectangle( [tuple(text_origin), tuple(text_origin + label_size)], fill=self.colors[c]) draw.text(text_origin, str(label, 'UTF-8'), fill=(0, 0, 0), font=font) del draw end = timer() print(end - start) return image, htimes