2014-11-05 85 views
0

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

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.") 

回答

0

的原因可能不是很明显,你应该尝试使用“印刷技术”来定位问题。 的一点是,你应该把打印无处不在,像这样

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项目并查看最后一次打印,显然您的应用程序崩溃的原因将在终端最后一次打印后的下一行。 这种方法可能看起来很sl and,我的答案可能会被经验丰富的python guru降低,但它的工作原理非常好。试试吧,你会在几分钟内发现你的问题。