2014-11-05 109 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降低,但它的工作原理非常好。試試吧,你會在幾分鐘內發現你的問題。