【问题标题】:Django page stuck on loading when click on reload page.单击重新加载页面时,Django 页面卡在加载中。
【发布时间】:2014-11-05 09:22:16
【问题描述】:

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

【问题讨论】:

    标签: python django python-2.7


    【解决方案1】:

    您的问题原因可能并不明显,您应该尝试使用“打印技术”来定位问题。 关键是你应该把 print 放在任何地方,像这样

    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 大师放弃,但它很有效,而且效果很好。只需尝试一下,您就会在几分钟内找出问题所在。

    【讨论】:

      猜你喜欢
      • 2021-07-18
      • 1970-01-01
      • 2022-12-15
      • 1970-01-01
      • 2019-06-29
      • 2014-02-11
      • 2023-03-13
      • 2017-04-30
      相关资源
      最近更新 更多