def predict(sess, image_file, specificPath=False): """ Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes """ if specificPath: image, image_data = preprocess_image(image_file, model_image_size=(608, 608)) else: image, image_data = preprocess_image("temp/" + image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) output = ('Found {} Objects in {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("out", image_file), quality=90) output_image = scipy.misc.imread(os.path.join("out", image_file)) ##plt.imshow(output_image) ##plt.show() #return out_scores, out_boxes, out_classes , os.path.join("out", image_file) return output, out_boxes, out_classes, os.path.join("out", image_file)
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # Preprocess your image image, image_data = preprocess_image(image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run( [scores, boxes, classes], feed_dict={yolo_model.input: image_data}) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("static/outimage", image_file.filename), quality=90) return image_file
def predict(sess, image_file): image, image_data = preprocess_image( "C:/Users/Vikarn Bhakri/Desktop/images/" + image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("C:/Users/Vikarn Bhakri/Desktop/out/", image_file), quality=90) output_image = scipy.misc.imread( os.path.join("C:/Users/Vikarn Bhakri/Desktop/out/", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes """ # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = None # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) ### START CODE HERE ### (≈ 1 line) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) ### END CODE HERE ### # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): image, image_data = preprocess_image( "E:/Spyder/神经网络与深度学习/吴恩达 深度学习 编程作业(4-3)(未完成)- Autonomous driving - Car detection/yad2k/images/" + image_file, model_image_size=(416, 416)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join( "E:/Spyder/神经网络与深度学习/吴恩达 深度学习 编程作业(4-3)(未完成)- Autonomous driving - Car detection/yad2k/images/out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread( os.path.join( "E:/Spyder/神经网络与深度学习/吴恩达 深度学习 编程作业(4-3)(未完成)- Autonomous driving - Car detection/yad2k/images/out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predictFromPath(sess, image_file, yolo_model, yolo_output, class_names): image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) scores, boxes, classes = prepare_yolo_eval(image.width, image.height, yolo_output) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("out", image_file), quality=90) output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Run the graph stored in "sess" to predict boxes for "image_file". Print and plot the preditions. Arguments: sess -- tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes """ # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) # Run the yolo model out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("out", image_file), quality=90) output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): # Pre_process image image, image_data = preprocess_image('images/' + image_file, model_image_size=(608, 608)) # Run the sess with the correct tensors and choose the correct placeholder in the feed_dict out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes colors = generate_colors(class_names) # Draw bounding boxes on image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join('output_image', image_file), quality=90) # Display the results in IDE output_image = imageio.imread(os.path.join('output_image', image_file)) imshow(output_image) plt.show() return out_scores, out_boxes, out_classes
def predict(sess, image_file, class_names, yolo_model, scores, boxes, classes): """ Aca hacemos el procesamiento de la imagen El valor del reshape es 608 x 608 porque con imagenes de ese tamaño fue entrenada la red """ # Seleccionamos la imagen a procesar y hacemos el reshape a 608,608 image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) # Iniciamos la sesion de tensorflow out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print de las predicciones realizadas print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Colores para determinar las clases colors = generate_colors(class_names) # Dibujamos las cajas en la imagen draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Podriamos guardar la imagen asi #image.save(os.path.join("out", image_file), quality=100) # Y mostrar los resultados asi #output_image = scipy.misc.imread(os.path.join("out", image_file)) #imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes """ image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("out", image_file), quality=90) output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): # Preprocess the image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) plt.show() return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the predictions. Arguments: sess -- tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # Preprocess the image image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. out_scores, out_boxes, out_classes = sess.run(fetches=[scores,boxes, classes],feed_dict={yolo_model.input: image_data ,K.learning_phase():0}) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file, is_show_info=True, is_plot=True): # 图像预处理 image, image_data = yolo_utils.preprocess_image("images/" + image_file, model_image_size=(608, 608)) # 运行并选择占位符 out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # 打印预测信息 if is_show_info: print("在" + str(image_file) + "中找到了" + str(len(out_boxes)) + "个锚框。") # 指定要绘制的边界框的颜色 colors = yolo_utils.generate_colors(class_names) # 在图中绘制边界框 yolo_utils.draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # 保存已经绘制了边界框的图 image.save(os.path.join("out", image_file), quality=100) # 打印出已经绘制了边界框的图 if is_plot: # output_image = scipy.misc.imread(os.path.join("out", image_file)) output_image = imageio.imread(os.path.join("out", image_file)) plt.imshow(output_image) return out_scores, out_boxes, out_classes
def predict(): sess = K.get_session() yolo = YOLO() image_file = 'test.jpg' image, image_data = preprocess_image("images/" + image_file, model_image_size=list( map(lambda x: int(x), IMG_SZ[:2]))) out_scores, out_boxes, out_classes = sess.run( [yolo.scores, yolo.boxes, yolo.classes], feed_dict={ yolo.model.input: image_data, K.learning_phase(): 0 }) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(yolo.class_names) draw_boxes(image, out_scores, out_boxes, out_classes, yolo.class_names, colors) image.save(os.path.join("out", image_file), quality=90) output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image)
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes """ # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608)) out_scores, out_boxes, out_classes = None # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. """ # Preprocess image image, image_data = preprocess_image(image_file, model_image_size=(608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook #t= plt.imshow(image) #print(image) #plt.show() return out_scores, out_boxes, out_classes
def predict(sess, image_file): # 对图片预处理, image_data 会增加一个维度, 变成 (1, 608, 608, 3), 这将作为CNN的输入 image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) # 喂入数据, 运行 session out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # 打印预测信息 print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) image = image.resize((1280, 720), PIL.Image.ANTIALIAS) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) image.save(os.path.join("out", image_file), quality=90) output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) plt.show() return out_scores, out_boxes, out_classes
def make_prediction(image_path, input_image_name, yolo_model): #Obtaining the dimensions of the input image input_image = Image.open(image_path + input_image_name) width, height = input_image.size width = np.array(width, dtype=float) height = np.array(height, dtype=float) #Assign the shape of the input image to image_shapr variable image_shape = (height, width) #Loading the classes and the anchor boxes that are provided in the model_data folder class_names = read_classes("model_data/yolo_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") #Print the summery of the model yolo_model.summary() #Convert final layer features to bounding box parameters yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) #Now yolo_eval function selects the best boxes using filtering and non-max suppression techniques. # If you want to dive in more to see how this works, refer keras_yolo.py file in yad2k/models boxes, scores, classes = yolo_eval(yolo_outputs, image_shape) # Initiate a session sess = K.get_session() #Preprocess the input image before feeding into the convolutional network image, image_data = preprocess_image(image_path + input_image_name, model_image_size = (608, 608)) #Run the session out_scores, out_boxes, out_classes = sess.run( [scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 } ) #Print the results print('Found {} boxes for {}'.format(len(out_boxes), input_image_name)) #Produce the colors for the bounding boxs colors = generate_colors(class_names) #Draw the bounding boxes draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) #Apply the predicted bounding boxes to the image and save it image.save("predictions/" + input_image_name, quality=90) if(len(out_classes) == 0): result = "No box found" elif (out_classes[0] == 0): result = "real" else: result = "fake" return input_image_name, out_scores, result
def predict(sess, image_file): image, image_data = preprocess_image(image_file, model_image_size = (608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict = {yolo_model.input:image_data})#, K.learning_phase():0}) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) return np.asarray(image)
def predict(sess, image_file): image, image_data = preprocess_image('input/' + image_file, model_image_size = (608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes],feed_dict = {yolo_model.input: image_data,K.learning_phase(): 0}) print('Found {} boxes for {}'.format(len(out_boxes), image_file)) colors = generate_colors(class_names) return out_scores, out_boxes, out_classes
def handler(event, context): from yolo_utils import draw_boxes, eval_image, generate_colors, preprocess_image, load_graph file_stream = io.BytesIO() bucket = s3.Bucket(DL_S3_BUCKET) model_obj = bucket.Object('yolo_tf.pb') model_obj.download_fileobj(file_stream) file_stream.seek(0) load_graph(file_stream) file_stream.close() bucket = s3.Bucket(S3_BUCKET) image_name = json.loads(event.get('body'))["image_name"] image_obj = bucket.Object(image_name) file_stream = io.BytesIO() out_image_name = "classified-{}".format(image_name) image_obj.download_fileobj(file_stream) image, image_data = preprocess_image(file_stream, model_image_size=(608, 608)) file_stream.close() scores, boxes, classes = eval_image(image_data, image.size) with open('./coco_classes.txt', 'r') as file: obj_classes = file.readlines() obj_classes = list(map(lambda cls: cls.strip(), obj_classes)) colors = generate_colors(obj_classes) draw_boxes(image, scores, boxes, classes, obj_classes, colors) file_stream = io.BytesIO() image.save(file_stream, format=image.format, quality=90) file_stream.seek(0) expiration = datetime.now() + timedelta(minutes=5) s3_client.put_object(Body=file_stream, Bucket=S3_BUCKET, Key=out_image_name, ACL='public-read', Expires=expiration) file_stream.close() return { 'statusCode': 200, 'headers': { "Access-Control-Allow-Headers": "Content-Type,Authorization,X-Amz-Date,X-Api-Key,X-Amz-Security-Token", "Access-Control-Allow-Methods": "DELETE,GET,HEAD,OPTIONS,PATCH,POST,PUT", "Access-Control-Allow-Origin": "*" }, 'body': json.dumps({ 'image_name': image_name, 'classified_image_name': out_image_name }) }
def predict(sess, image_file): start = time.time() # Preprocess your image image, image_data = preprocess_image(sys.argv[2] + image_file, model_image_size = (608, 608)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes] ,feed_dict={yolo_model.input:image_data, K.learning_phase(): 0}) end = time.time() # Print predictions info print(image_file) colors = generate_colors(class_names) draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors)
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # np.savetxt("output.txt", dept_array, fmt="%i") # print(dept_array) if image_file.__class__ == str: image, image_data = preprocess_image("images/" + image_file, model_image_size=(416, 416)) else: image = PIL.Image.fromarray(image_file) image, image_data = preprocess_image(image, model_image_size=(416, 416)) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) # print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file labels = draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) return out_scores, out_boxes, out_classes, image, labels
def load_model_and_infer(sess, image_file): """ Load the model as a graph in sesssion and Run it to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # Load and preprocess image for inference image, image_data, image_size = preprocess_image("images/" + image_file, model_image_size = (608, 608)) # Define classes, anchors and image shape. class_names = read_classes("model/coco_classes.txt") anchors = read_anchors("model/yolo_anchors.txt") image_shape = (float(image_size[1]),float(image_size[0])) # Load the pretrained model. yolo_model = load_model("model/yolo.h5") # print a summary of the layer's model. yolo_model.summary() # Convert output of the model to bounding box tensors. yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) # Filter boxes. scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) # Infer predictions on the image. out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input:image_data, K.learning_phase():0}) # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("output", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("output", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("model_data/yolo.h5") # yolo_model.summary() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) ### START CODE HERE ### (≈ 1 line) out_scores, out_boxes, out_classes = sess.run([scores,boxes,classes], feed_dict={yolo_model.input: image_data, K.learning_phase(): 0}) ### END CODE HERE ### # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes
def predict(sess, folderin, image_file, folderout, yolo_out): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the predictions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ (scores, boxes, classes) = yolo_out # folderpath = "images/" # Preprocess your image image, image_data = preprocess_image(os.path.join(folderin, image_file), model_image_size=(608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) ### START CODE HERE ### (≈ 1 line) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) ### END CODE HERE ### # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join(folderout, image_file), quality=90) # Display the results in the notebook # imread is deprecated in SciPy 1.0.0, and was removed in 1.2.0. Use imageio.imread instead. # output_image = scipy.misc.imread(os.path.join("out", image_file)) # errer! output_image = imageio.imread(os.path.join(folderout, image_file)) imshow(output_image) return out_scores, out_boxes, out_classes, output_image
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size=(608, 608)) print(image_data.shape) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) ### START CODE HERE ### (≈ 1 line) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={ yolo_model.input: image_data, K.learning_phase(): 0 }) ### END CODE HERE ### # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook #output_image = scipy.misc.imread(os.path.join("out", image_file)) #From PILLOW to Numpy matrix img = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR) cv2.imshow('windows', img) cv2.waitKey(0) return out_scores, out_boxes, out_classes
def predict(sess, image): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ class_names = read_classes("model_data/coco_classes.txt") anchors = read_anchors("model_data/yolo_anchors.txt") image_shape = (720., 1280.) yolo_model = load_model("model_data/yolo.h5") # yolo_model.summary() t = time.time() yolo_outputs = yolo_head(yolo_model.output, anchors, len(class_names)) print("t1:", time.time()-t) t = time.time() scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) print("t2:", time.time() - t) # Preprocess your image image_data = preprocess_image(image, model_image_size=(608, 608)) t = time.time() # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input: image_data, K.learning_phase(): 0}) print("t3:", time.time() - t) # Print predictions info #print('Found {} boxes'.format(len(out_boxes))) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image result = draw_boxes(image, out_boxes, out_classes, colors) return result
def predict(model, image_file, is_show_info=True, is_plot=True): """ 运行model并输出预测的图和信息 由于练习的使用的时tf2.3版本,代码和博客中的tf1有很大区别,所以和部分想了半天,也算是从头梳理了一遍 注意:博客中没有model这个参数,这里加上是因为模型在函数里加载的话,运行批量绘图时会由于多次加载模型导致显存报错。 Args: model: 用来预测锚框的模型 image_file: images文件夹的图片名称 Returns: out_scores: tensor,维度为(None,)锚框预测的可能值 out_boxes: tensor,维度为(None,4)预测的锚框的位置 out_classes: tensor, 维度为(None,)预测的锚框的分类索引 """ dir = './data/Yolo/yolo_model/model_data/' class_name = yolo_utils.read_classes(dir + 'coco_classes.txt') anchors = yolo_utils.read_anchors(dir + 'yolo_anchors.txt') image_shape = (720., 1280.) # 处理图像,image_data为图像转换为tensor后的数据 image, image_data = yolo_utils.preprocess_image( './data/Yolo/images/' + image_file, model_image_size=(608, 608)) # 预测图像,结果为(1,19,19,425)最后的维度为5个锚框x85个属性 yolo_model_output = model.predict(image_data) # yolo_head将yolo模型的输出进行转换为各个格子中每个锚框的 (坐标、宽高、预测值、分类值) # 原文中yolo_head的输出顺序有误,会导致yolo_eval函数报错,在此已经将yolo_head的输出顺序修改 yolo_outputs = yolo_utils.yolo_head(yolo_model_output, anchors, len(class_name)) scores, boxes, classes = yolo_eval(yolo_outputs, image_shape) # 打印预测信息 if is_show_info: print("在" + str(image_file) + "中找到了" + str(len(boxes)) + "个锚框。") # 指定绘制边框的颜色 colors = yolo_utils.generate_colors(class_name) # 绘制边界并保存图片 yolo_utils.draw_boxes(image, scores, boxes, classes, class_name, colors) image.save('./data/Yolo/out/' + image_file, quality=100) # 打印出已经绘制了边界框的图 if is_plot: output_image = plt.imread('./data/Yolo/out/' + image_file) plt.imshow(output_image) plt.show() return scores, boxes, classes
def predict(sess, image_file): """ Runs the graph stored in "sess" to predict boxes for "image_file". Prints and plots the preditions. Arguments: sess -- your tensorflow/Keras session containing the YOLO graph image_file -- name of an image stored in the "images" folder. Returns: out_scores -- tensor of shape (None, ), scores of the predicted boxes out_boxes -- tensor of shape (None, 4), coordinates of the predicted boxes out_classes -- tensor of shape (None, ), class index of the predicted boxes Note: "None" actually represents the number of predicted boxes, it varies between 0 and max_boxes. """ # Preprocess your image image, image_data = preprocess_image("images/" + image_file, model_image_size = (608, 608)) # Run the session with the correct tensors and choose the correct placeholders in the feed_dict. # You'll need to use feed_dict={yolo_model.input: ... , K.learning_phase(): 0}) ### START CODE HERE ### (≈ 1 line) out_scores, out_boxes, out_classes = sess.run([scores, boxes, classes], feed_dict={yolo_model.input: image_data, K.learning_phase(): 0}) ### END CODE HERE ### # Print predictions info print('Found {} boxes for {}'.format(len(out_boxes), image_file)) # Generate colors for drawing bounding boxes. colors = generate_colors(class_names) # Draw bounding boxes on the image file draw_boxes(image, out_scores, out_boxes, out_classes, class_names, colors) # Save the predicted bounding box on the image image.save(os.path.join("out", image_file), quality=90) # Display the results in the notebook output_image = scipy.misc.imread(os.path.join("out", image_file)) imshow(output_image) return out_scores, out_boxes, out_classes