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」的值作爲字符串。上面的代碼不適用於瀏覽器,但它在編譯器上正確編譯。下面的Python模塊會與django一起炒作嗎?
爲什麼不自己檢查一下? – BartoszKP 2014-10-05 12:55:52
我試過但失敗 – 2014-10-05 13:26:56
宣稱「我失敗了」真的不足以做出好的SO問題。請解釋你到底做了什麼,你是如何失敗的,你期望的是什麼。包括相關的代碼示例,以便其他人可以重現您的問題。否則這個問題可能會被關閉。 – BartoszKP 2014-10-05 13:33:15