2017-08-17 50 views
-2
127.0.0.1 - - [17/Aug/2017 17:39:07] "GET /hello?url=http://i.imgur.com/HYMQOl7.jpg HTTP/1.1" 500 781 
https://lh4.ggpht.com/mJDgTDUOtIyHcrb69WM0cpaxFwCNW6f0VQ2ExA7dMKpMDrZ0A6ta64OCX3H-NMdRd20=w300 
Traceback (most recent call last): 
    File "/Users/SR/anaconda2/lib/python2.7/site-packages/bottle.py", line 862, in _handle 
    return route.call(**args) 
    File "/Users/SR/anaconda2/lib/python2.7/site-packages/bottle.py", line 1740, in wrapper 
    rv = callback(*a, **ka) 
    File "helloworld.py", line 29, in expandit 
    txt = pytesseract.image_to_string('temp.png') 
    File "/Users/SR/anaconda2/lib/python2.7/site-packages/pytesseract/pytesseract.py", line 117, in image_to_string 
    image.save(input_file_name) 
AttributeError: 'str' object has no attribute 'save' 

我真的不知道問題出在哪裏。 imread()的功能應該和我一樣,至少這就是我的想法。圖像未被讀取,'NoneType沒有屬性讀取'

這裏是我的Python代碼:

import urllib2 
import numpy as np 
import pytesseract 

from skimage.color import rgb2gray 
from skimage import data 
from skimage import io 
from PIL import Image, ImageEnhance, ImageFilter 
from bottle import route, run, request, get 



@get('/hello') 
def expandit(): 
    url = request.get('url') 
    if url == '': 
     return {"success": "false"} 
    else: 
     image_original = io.imread(url) #creates a temp file 
     image = rgb2gray(image_original) 

     io.imsave(temp.png, image) 

     txt = pytesseract.image_to_string(temp.png) 

     return {"text": txt, "success": "true"} 

run(host='localhost', port=8080, reloader=True) 

這將是真棒,如果我能在正確的方向推。 非常感謝。

+0

'如果URL == '':'什麼也不做趕上'None'。一種方法是將其更改爲「if not url:',這可以阻止您的錯誤。 'url = request.get('url')'''url = None'不是''''如果找不到密鑰。 – roganjosh

+0

@roganjosh - 感謝您的回覆。我只是試圖做到這一點,但同樣的錯誤 – SR1

+0

所以'image_original = io.imread(url)'之前,你從'print(url)'得到什麼? – roganjosh

回答

1

您收到此錯誤,因爲urlNone

試試這個代碼:

image_original = io.imread(None) 

可以得到同樣的錯誤:

AttributeError: 'NoneType' object has no attribute 'read' 

如果條件改變你的代碼

# to test override url 
url = 'https://lh4.ggpht.com/mJDgTDUOtIyHcrb69WM0cpaxFwCNW6f0VQ2ExA7dMKpMDrZ0A6ta64OCX3H-NMdRd20=w300' 
if url: 
    image_original = io.imread(url) 

    # read image and convert it. 
    image = rgb2gray(image_original) 
    io.imsave('temp.png', image) 
    im = Image.open('temp.png') 
    bg = Image.new("RGB", im.size, (255,255,255)) 
    bg.paste(im) 
    txt = pytesseract.image_to_string(bg) 

    return {"text": txt, "success": "true"} 

else: 
    return {"success": "false"} 

確保已正方體-OCR安裝

sudo apt-get install tesseract-ocr 
+0

雖然這是幾乎肯定的答案,OP在評論認爲,這並不解決他們的問題,聲稱。 – roganjosh

+0

@roganjosh剛剛閱讀評論:更新建議。感謝您指出。 –

+0

感謝您的回覆。這是一個獨立的解決方案 - 但BottlePy代碼仍然沒有響應。 – SR1