单击重新加载页面时,Django页面卡在加载上。

时间:2014-11-05 09:22:17

标签: python django python-2.7

我用django构建一个web应用程序,在背景上显示什么过程图像。当我启动服务器第一次加载页面时,它完美地工作并将图像保存在文件夹中。但问题是当我点击浏览器的刷新按钮时它会加载。我尝试关闭服务器我看到服务器(ctrl + x)它关闭但我看到它在后台运行。重新启动服务器后,应用程序第一次工作,但如果我单击刷新按钮再次停止加载。我希望如果我点击浏览器的刷新按钮,应用程序将从头开始运行。我不知道代码有什么问题。我是django的新手,请给我一些暗示该做什么。我的操作系统是linux并使用python 2.7。

下面给出了views.py的代码: -

from django.http import HttpResponse
import numpy as np
import cv2
import Image
from PIL import Image
import ctypes
import os
import ImageDraw

#
#
#
#
#

def index(request):
    im_gray = cv2.imread('Rimage.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
    (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY |  cv2.THRESH_OTSU)
    thresh = 100
    im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
    cv2.imwrite('bw_image.png', im_bw)
    im = cv2.imread('bw_image.png')
    gray = cv2.cvtColor(im,cv2.COLOR_BGR2GRAY)
    blur = cv2.GaussianBlur(gray,(5,5),0)
    thresh = cv2.adaptiveThreshold(blur,255,1,1,19,4)
    contours,hierarchy = cv2.findContours(thresh,cv2.RETR_LIST,cv2.CHAIN_APPROX_SIMPLE)
    h_list=[]
    for cnt in contours:
        [x,y,w,h] = cv2.boundingRect(cnt)
        if w*h>250:
            h_list.append([x,y,w,h])
    ziped_list=zip(*h_list)
    x_list=list(ziped_list[0])
    dic=dict(zip(x_list,h_list))
    x_list.sort()
    i=0
    for x in x_list:
        [x,y,w,h]=dic[x]
        im3=im[y:y+h,x:x+w]
        cv2.imwrite('objects/pix%i.png'%i,im3)
        i+=1
    cv2.imwrite('objects/shhh.jpg',im)
    key = cv2.waitKey(0)
    im0 = cv2.imread('objects/pix0.png',0)
    im1 = cv2.imread('objects/pix1.png',0)
    im2 = cv2.imread('objects/pix2.png',0)
    im3 = cv2.imread('objects/pix3.png',0)
    im4 = cv2.imread('objects/pix4.png',0)
    im5 = cv2.imread('objects/pix5.png',0)

    h0, w0 = im0.shape[:2]
    h1, w1 = im1.shape[:2]
    h2, w2 = im2.shape[:2]
    h3, w3 = im3.shape[:2]
    h4, w4 = im4.shape[:2]
    h5, w5 = im5.shape[:2]
    maxh=max(h0,h1,h2,h3,h4,h5)

    new = np.zeros((maxh,w0+w1+w2+w3+w4+w5+50),np.uint8)
    new=(255-new)
    new[maxh-h0:, :w0] = im0
    new[maxh-h1:, w0+10:w0+w1+10] = im1
    new[maxh-h2:, w0+w1+20:w0+w1+w2+20] = im2
    new[maxh-h3:, w0+w1+w2+30:w0+w1+w2+w3+30] = im3
    new[maxh-h4:, w0+w1+w2+w3+40:w0+w1+w2+w3+w4+40] = im4
    new[maxh-h5:, w0+w1+w2+w3+w4+50:] = im5
    gray = cv2.cvtColor(new, cv2.COLOR_GRAY2BGR)
    cv2.imwrite('new_image.jpg',gray)
    key = cv2.waitKey(0)

#resize image..........................................
    im22 = Image.open("new_image.jpg")
    img22 = im22.resize( (157,57), Image.ANTIALIAS)
    img22.save("new_image1.jpg", dpi=(1200,1200) )
#image with white background........................... 
    img1 = Image.open("new_image1.jpg")
    img2 = img1.crop( (0,-34,600,566) )
    draw = ImageDraw.Draw(img2)
    draw.rectangle( (0,0,600,34), fill="white" )
    draw.rectangle( (0,90,600,600), fill="white" )
    draw.rectangle( (157,0,600,600), fill="white" )
    img2.save("img2.jpg","JPEG", quality=90)
#remove files.....................................................
    os.remove('bw_image.png')
    os.remove('img2.jpg')
    os.remove('new_image.jpg')
    os.remove('new_image1.jpg')
    os.remove('objects/pix0.png')
    os.remove('objects/pix1.png')
    os.remove('objects/pix2.png')
    os.remove('objects/pix3.png')
    os.remove('objects/pix4.png')
    os.remove('objects/pix5.png')
    os.remove('objects/shhh.jpg')
#end..........................................................

    return HttpResponse("Hello, world. You're at the polls index.")

1 个答案:

答案 0 :(得分:0)

您的问题的原因可能并不明显,您应该尝试使用'print-technique'来查找问题。 关键是你应该把打印放在任何地方,比如这个

def index(request):
    im_gray = cv2.imread('Rimage.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)
    print 'after im_gray'
    (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY |     cv2.THRESH_OTSU)
    print 'after (thresh, im_bw)'
    thresh = 100
    print 'after thresh'
    im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
    print 'after im_bw'

等等。然后你只需打开终端,运行你的django项目并查看最后的打印件,显然你的应用程序崩溃的原因将是在终端上次打印后的下一行。 这种方法可能看起来很草率,可能我的答案将由经验丰富的python大师放下,但它的工作原理非常好。试试吧,你会在几分钟内找出问题所在。