我正在处理一个项目,在该项目中我上载图像,对其进行一些处理,然后在显示某些统计信息的同时显示两者。
我已经设置了2个Django应用程序:
问题:
当我runserver
时,第二个应用程序的views.py会运行,而无需等待第一个应用程序中的图像!
有帮助吗?
app1 :: views.py
def index(request):
if request.method == 'POST':
uploaded_file=request.FILES['document']
fs=FileSystemStorage()
fs.save(uploaded_file.name, uploaded_file)
print(uploaded_file.name)
img=io.imread(BASE_DIR+'/media/'+uploaded_file.name)
io.imsave(BASE_DIR+'/detect/static/original.jpg',img)
return render(request, 'index.html')
app2 :: views.py 实例是在同一文件中生成的
from django.shortcuts import render
from skimage import color
import skimage.io as io
from django.core.files.storage import FileSystemStorage
import os
import sys
import random
import math
import numpy as np
import skimage.io
import matplotlib
import matplotlib.pyplot as plt
from mrcnn import utils
import mrcnn.model as modellib
from mrcnn import visualize
from keras.backend import clear_session
import coco
ROOT_DIR = os.path.abspath("../")
def result(request):
return render(request, 'detect.html', {'instances':instances})
MODEL_DIR = os.path.join(ROOT_DIR, "logs")
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
class InferenceConfig(coco.CocoConfig):
GPU_COUNT = 1
IMAGES_PER_GPU = 1
config = InferenceConfig()
clear_session()
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
model.load_weights(COCO_MODEL_PATH, by_name=True)
class_names = ['BG', 'person', 'bicycle', 'car', 'motorcycle', 'airplane',
'bus']
image = skimage.io.imread('./detect/static/original.jpg')
# Run detection
results = model.detect([image], verbose=1)
# Visualize results
r = results[0]
visualize.display_instances(image, r['rois'], r['masks'], r['class_ids'], class_names, r['scores'])
masks=r['masks']
ci=r['class_ids']
classnm=[]
for i in ci:
classnm.append(class_names[i])
l=[]
y=0
for i in range(len(ci)):
for j in masks:
for x in j:
if x[i] == True:
y=y+1
l.append(y)
y=0
scores=r['scores']*100
scores_percent=[round(x,2) for x in scores ]
li=[round(x*0.729,2) for x in l]
class Instance:
def __init__(self,class_name,surface,accuracy):
self.class_name=class_name
self.surface=surface
self.accuracy=accuracy
instances = [ Instance(classnm[i],li[i],scores_percent[i]) for i in range(len(classnm))]