以下Python模块是否可以与django一起使用?

时间:2014-10-05 12:50:02

标签: python django python-2.7

   import Image,tesseract,cv2
    import numpy as np
    import cgi, os, sys
    import sqlite3 as db
    import sqlite3 as db

    def codingd(num):
         ##Thre hold..................................................................
         im_gray = cv2.imread('image.jpg', cv2.CV_LOAD_IMAGE_GRAYSCALE)

         (thresh, im_bw) = cv2.threshold(im_gray, 128, 255, cv2.THRESH_BINARY | cv2.THRESH_OTSU)
         thresh = 90
         im_bw = cv2.threshold(im_gray, thresh, 255, cv2.THRESH_BINARY)[1]
         cv2.imwrite('bw_image1.jpg', im_bw)
         key = cv2.waitKey(0)

         #improve image..........................................................
         im = cv2.imread('bw_image1.jpg') 
         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_TREE,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])
         #print h_list          
         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]
               #cv2.rectangle(im,(x,y),(x+w,y+h),(0,0,255),1)
               im3=im[y:y+h,x:x+w]
               cv2.imwrite('objects/pix%i.png'%i,im3)
               i+=1

               #cv2.imshow('bw_image1',im)
         cv2.imwrite('bw_image1.jpg',im)
         key = cv2.waitKey(0)

         #adding object............
         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)

         #add 50 for space between the objects

         new = np.zeros((maxh, w0+w1+w2+w3+w4+w5+25),np.uint8)
         new=(255-new)
         new[maxh-h0:, :w0] = im0
         new[maxh-h1:, w0+5:w0+w1+5] = im1
         new[maxh-h2:, w0+w1+10:w0+w1+w2+10] = im2
         new[maxh-h3:, w0+w1+w2+15:w0+w1+w2+w3+15] = im3
         new[maxh-h4:, w0+w1+w2+w3+20:w0+w1+w2+w3+w4+20] = im4
         new[maxh-h5:, w0+w1+w2+w3+w4+25:] = im5
         gray = cv2.cvtColor(new, cv2.COLOR_GRAY2BGR)


         #cv2.imshow('norm',gray)
         cv2.imwrite('new_image1.jpg',gray)
         key = cv2.waitKey(0)

         # joom image 
         im = Image.open('new_image1.jpg')
         im2 = im.resize((70,35), Image.NEAREST)
         im2.save('new_image1.jpg')


         api = tesseract.TessBaseAPI()
         api.SetOutputName("outputName");
         #api.Init(".","eng")
         api.Init(".","eng",tesseract.OEM_DEFAULT)
         api.SetPageSegMode(tesseract.PSM_AUTO)
         mImgFile = "bw_image1.jpg"

         result = tesseract.ProcessPagesWrapper(mImgFile,api)
         return result
    mmmmmm= codingd(1)

sys.stdout.write("Content-type: text/html\r\n\r\n")
sys.stdout.write("")
sys.stdout.write("<html><body>")
sys.stdout.write("<h2>Fahrenheit converted to Celsius</h2>")
sys.stdout.write("</body></html>")

我创建了一个简单的函数,可以在python 2.7中将图像转换为文本。现在我想建立一个网站,帮助人们将图像转换为字符串对象。任何人都可以告诉我这些模块是否可以与django一起使用,或者我将如何构建该Web应用程序?由于我是python的新手,我将非常感谢您的详细信息。我希望如果我运行一个网页,那么它将在浏览器上返回“mmmmmm”的值作为字符串。上面的代码不能在浏览器上运行,但它在编译器上正确编译。

1 个答案:

答案 0 :(得分:0)

我重用Django的所有python代码都可以。麻烦的是在HTTP请求/响应之间插入它。例如:

  • 您的任务计算时间超过2秒。一个网站不会用来处理这段时间。

在这种情况下,我想您使用Celery或者Web用户必须等待任务结束才能得到回复。