问题遇到的现象和发生背景
将yolov3的coco识别分类嫁接到django框架下出现问题,第一次上传图片识别,没有问题,第二次上传图片识别,就报错了
将yolov3的coco识别分类嫁接到django框架下出现问题,第一次上传图片识别,没有问题,第二次上传图片识别,就报错了
以下是我预测用的代码
def input_model(input_size=416):
input_layer = tf.keras.layers.Input([input_size, input_size, 3])
feature_maps = YOLOv3(input_layer)
bbox_tensors = []
for i, fm in enumerate(feature_maps):
bbox_tensor = decode(fm, i)
bbox_tensors.append(bbox_tensor)
model = tf.keras.Model(input_layer, bbox_tensors)
utils.load_weights(model, "ICAR\\yolov3.weights")
# model.summary()
return model
# model.summary()
def image_label(original_image, model):
input_size = 416
original_image = np.array(original_image) # 将图片转为 numpy array 格式
original_image_size = original_image.shape[:2]
image_data = utils.image_preporcess(np.copy(original_image), [input_size, input_size])
image_data = image_data[np.newaxis, ...].astype(np.float32)
# models = input_model(input_size=416)
pred_bbox = model.predict(image_data)
pred_bbox = [tf.reshape(x, (-1, tf.shape(x)[-1])) for x in pred_bbox]
pred_bbox = tf.concat(pred_bbox, axis=0)
bboxes = utils.postprocess_boxes(pred_bbox, original_image_size, input_size, 0.3)
bboxes = utils.nms(bboxes, 0.45, method='nms')
_, label = utils.draw_bbox(original_image, bboxes)
return label
# 方法二:把预测的结果直接返回给前端的文本框中
@csrf_exempt
def facedetectDemo2(request):
result = {"code": None}
if request.method == 'POST':
imag = request.FILES.get('image')
# 3、载入自己写的数字图片并设置大小
original_image = Image.open(imag)
model = input_model(416)
label = image_label(original_image, model)
print(label)
result.update({"output": label})
return JsonResponse(result)