所以我有一个python文件,运行时需要给它一些用户输入,例如:python image_initial.py abc.jpg abc
我想在运行此文件时删除这些参数,因为我有一个可以运行的烧瓶,并且在单击提交按钮时,文件image_initial.py
就会运行。
我已经在image_initial.py
中创建了一个类,并在我的flask文件中调用了该类,但是我需要按如下方式进行争论:python main.py abc.jpg abc
;如果我不提供参数,则会出现错误,提示ListIndex out of range
。
如何删除这些争论以及如何接受用户输入?
这是我的image_initial.py
的代码:
import numpy as np
import os
import sys
import tensorflow as tf
import json
from PIL import Image
sys.path.append("..")
from object_detection.utils import ops as utils_ops
from utils import label_map_util
from utils import visualization_utils as vis_util
class Image_tensorflow():
PATH_TO_FROZEN_GRAPH = 'frozen_inference_graph.pb'
PATH_TO_LABELS = 'object-detection.pbtxt'
NUM_CLASSES = 4
detection_graph = tf.Graph()
with detection_graph.as_default():
od_graph_def = tf.GraphDef()
with tf.gfile.GFile(PATH_TO_FROZEN_GRAPH, 'rb') as fid:
serialized_graph = fid.read()
od_graph_def.ParseFromString(serialized_graph)
tf.import_graph_def(od_graph_def, name='')
label_map = label_map_util.load_labelmap(PATH_TO_LABELS)
categories = label_map_util.convert_label_map_to_categories(label_map, max_num_classes=NUM_CLASSES,
use_display_name=True)
category_index = label_map_util.create_category_index(categories)
def load_image_into_numpy_array(image):
(im_width, im_height) = image.size
return np.array(image.getdata()).reshape(
(im_height, im_width, 3)).astype(np.uint8)
def image_url(imagepath, filename):
file_path = 'images/'
file_name = filename
image = imagepath
f = open((file_path + file_name + ".json"), "w")
f.close
return_dict = {'image': image, 'file': f};
return return_dict
get_image_data = image_url(sys.argv[1],sys.argv[2])
image_path= get_image_data['image']
IMAGE_SIZE = (12, 8)
def run_inference_for_single_image(image, graph):
with graph.as_default():
with tf.Session() as sess:
# Get handles to input and output tensors
ops = tf.get_default_graph().get_operations()
all_tensor_names = {output.name for op in ops for output in op.outputs}
tensor_dict = {}
for key in [
'num_detections', 'detection_boxes', 'detection_scores',
'detection_classes', 'detection_masks'
]:
tensor_name = key + ':0'
if tensor_name in all_tensor_names:
tensor_dict[key] = tf.get_default_graph().get_tensor_by_name(
tensor_name)
if 'detection_masks' in tensor_dict:
# The following processing is only for single image
detection_boxes = tf.squeeze(tensor_dict['detection_boxes'], [0])
detection_masks = tf.squeeze(tensor_dict['detection_masks'], [0])
# Reframe is required to translate mask from box coordinates to image coordinates and fit the image size.
real_num_detection = tf.cast(tensor_dict['num_detections'][0], tf.int32)
detection_boxes = tf.slice(detection_boxes, [0, 0], [real_num_detection, -1])
detection_masks = tf.slice(detection_masks, [0, 0, 0], [real_num_detection, -1, -1])
detection_masks_reframed = utils_ops.reframe_box_masks_to_image_masks(
detection_masks, detection_boxes, image.shape[0], image.shape[1])
detection_masks_reframed = tf.cast(
tf.greater(detection_masks_reframed, 0.5), tf.uint8)
tensor_dict['detection_masks'] = tf.expand_dims(
detection_masks_reframed, 0)
image_tensor = tf.get_default_graph().get_tensor_by_name('image_tensor:0')
output_dict = sess.run(tensor_dict,
feed_dict={image_tensor: np.expand_dims(image, 0)})
output_dict['num_detections'] = int(output_dict['num_detections'][0])
output_dict['detection_classes'] = output_dict[
'detection_classes'][0].astype(np.uint8)
output_dict['detection_boxes'] = output_dict['detection_boxes'][0]
output_dict['detection_scores'] = output_dict['detection_scores'][0]
if 'detection_masks' in output_dict:
output_dict['detection_masks'] = output_dict['detection_masks'][0]
return output_dict
image = Image.open(image_path)
image_np = load_image_into_numpy_array(image)
image_np_expanded = np.expand_dims(image_np, axis=0)
# Actual detection.
output_dict = run_inference_for_single_image(image_np, detection_graph)
# Visualization of the results of a detection.
vis_util.visualize_boxes_and_labels_on_image_array(
image_np,
output_dict['detection_boxes'],
output_dict['detection_classes'],
output_dict['detection_scores'],
category_index,
instance_masks=output_dict.get('detection_masks'),
use_normalized_coordinates=True,
line_thickness=8)
# get_image_data = image_url(sys.argv[1],sys.argv[2])
# image_file = get_image_data['image']
# pass values
import cv2 as cv
image_file = image_path
img = cv.imread(image_file)
i = 0
j = 0
limiter = 0.3
while (i < 100):
if (output_dict['detection_scores'][i] > limiter):
j = j + 1
i = i + 1
# In[17]:
# store the pass values in lists
i = 0
detection_classes = []
detection_boxes = [[]] * j
detection_scores = []
while (i < j):
detection_classes.append(output_dict['detection_classes'][i])
detection_scores.append(output_dict['detection_scores'][i])
detection_boxes[i].append(output_dict['detection_boxes'][i])
i = i + 1
list1 = []
for items in detection_classes:
if items == 1:
list1.append("Angry")
elif items == 2:
list1.append("Sad")
elif items == 3:
list1.append("Neutral")
elif items == 4:
list1.append("Happy")
final_dict = {'DETECTION': list1}
file_to_write_to = get_image_data['file'].name
file_to_write_to = str(file_to_write_to)
text_file = open(file_to_write_to, "w")
text_file.write(json.dumps(final_dict))
text_file.close()
final_path = "images/" + sys.argv[2] + "_annotated" + ".jpg"
# draw bounding boxes
img = cv.imread(image_path)
i = 0
for item in detection_classes:
width, height = image.size
ymin = int(detection_boxes[0][i][0] * height)
xmin = int(detection_boxes[0][i][1] * width)
ymax = int(detection_boxes[0][i][2] * height)
xmax = int(detection_boxes[0][i][3] * width)
font = cv.FONT_HERSHEY_SIMPLEX
panel_colour = (182, 182, 42)
bumper_colour = (241, 239, 236)
damage_colour = (0, 255, 0)
text_colour = (255, 255, 255)
bumper_text = (0, 0, 0)
buffer = int(5 * width / 1000)
if (detection_classes[i] == 1):
img = cv.rectangle(img, (xmin, ymin), (xmax, ymax), panel_colour, int(2 * (height / 600)))
cv.rectangle(img, (xmin, (ymin + (buffer * 8))), (xmax, ymin), panel_colour, -1)
cv.putText(img, 'angry', (xmin, (ymin + (buffer * 6))), font, 0.8 * (height / 500), text_colour,
int(2 * (height / 400)), cv.LINE_AA)
elif (detection_classes[i] == 2):
img = cv.rectangle(img, (xmin, ymin), (xmax, ymax), panel_colour, int(2 * (height / 600)))
cv.rectangle(img, (xmin, (ymin + (buffer * 8))), (xmax, ymin), panel_colour, -1)
cv.putText(img, 'sad', (xmin, (ymin + (buffer * 6))), font, 0.8 * (height / 500), text_colour,
int(2 * (height / 400)), cv.LINE_AA)
elif (detection_classes[i] == 3):
img = cv.rectangle(img, (xmin, ymin), (xmax, ymax), bumper_colour, int(2 * (height / 600)))
cv.rectangle(img, (xmin, (ymin + (buffer * 8))), (xmax, ymin), bumper_colour, -1)
cv.putText(img, 'neutral', (xmin, (ymin + (buffer * 6))), font, 0.8 * (height / 500), bumper_text,
int(2 * (height / 400)), cv.LINE_AA)
elif (detection_classes[i] == 4):
img = cv.rectangle(img, (xmin, ymin), (xmax, ymax), panel_colour, int(2 * (height / 600)))
cv.rectangle(img, (xmin, (ymin + (buffer * 8))), (xmax, ymin), panel_colour, -1)
cv.putText(img, 'happy', (xmin, (ymin + (buffer * 6))), font, 0.8 * (height / 500), text_colour,
int(2 * (height / 400)), cv.LINE_AA)
i = i + 1
final_path = "/home/mayureshk/PycharmProjects/ImageDetection/venv/models/research/object_detection/images/" + sys.argv[2] + "_annotated" + ".jpg"
cv.imwrite(final_path, img)
这是我的main.py
flask文件的代码:
import os
import urllib.request
from app import app
from flask import Flask,flash,redirect,render_template,request
from werkzeug.utils import secure_filename
from image_initial import Image_tensorflow
ALLOWED_EXTENSIONS = set(['png','jpg','jpeg','gif'])
def allowed_file(filename):
return '.' in filename and filename.rsplit('.',1)[1].lower() in ALLOWED_EXTENSIONS
@app.route('/')
def upload_form():
return render_template('upload.html')
@app.route('/',methods=['GET','POST'])
def upload_file():
if request.method == 'POST':
#check if the post request has the file part
if 'file' not in request.files:
flash('No file is available')
return redirect(request.url)
file = request.files['file']
if file.filename == '':
flash('No file is selected for uploading')
return redirect(request.url)
if file and allowed_file(file.filename):
filename = secure_filename(file.filename)
Image_tensorflow()
file.save(os.path.join(app.config['UPLOAD_FOLDER'],filename))
flash('File successfully uploaded')
return redirect('/')
else:
flash('Allowed types are png,jpg,jpeg,gif')
return redirect(request.url)
if __name__ == "__main__":
app.run(debug=True)
而且我也希望我的image_initial.py文件仅在用户按下Submit按钮时才能工作,但在运行我的flask文件main.py之后不久它就会运行
这是我在不传递参数的情况下运行的回溯信息
Traceback (most recent call last):
File "main.py", line 6, in <module>
from image_initial import Image_tensorflow
File "/home/mayureshk/PycharmProjects/ImageDetection/venv/models/research/object_detection/image_initial.py", line 14, in <module>
class Image_tensorflow():
File "/home/mayureshk/PycharmProjects/ImageDetection/venv/models/research/object_detection/image_initial.py", line 49, in Image_tensorflow
get_image_data = image_url(sys.argv[1],sys.argv[2])
IndexError: list index out of range
您的脚本image_initial.py
在计算功能内使用了sys.argv
,这是一个坏习惯,因为它构成了在较深位置对命令行参数的硬连线。另外,在Image_tensorflow
类中进行代码调用是一个坏习惯:这应该在方法中。