0
我在Google App Engine中有以下代碼,它接受來自用戶的圖像,並隨後在該圖像上執行OCR。Google App Engine:image to base64 OCR(Python)的字符串
的index.html:
<form action="/submit" method="post" enctype="multipart/form-data">
<input type="file" name="newImage" capture="camera">
<input type="submit" value="Submit">
</form>
main.py:
import requests
def image_to_text(encoded_string, content_type="jpeg"):
api_key = "API_KEY"
overlay = False
language = 'eng'
payload = {'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
'base64Image': "data:image/{};base64,{}".format(content_type,
encoded_string)
}
r = requests.post('https://api.ocr.space/parse/image',
data=payload)
return r.content.decode()
class Submit(webapp2.RequestHandler):
def post(self):
new_image = self.request.get("newImage")
if new_image is not '': # ie user uploads an image
IMG = UploadImage()
IMG.img = new_image # ndb.BlobProperty()
img_key = IMG.put() # stores it in datastore
img_key_url = img_key.urlsafe()
base64_string = new_image.encode('base64') # this is the step that I may be doing wrongly
text= image_to_text(base64_string)
但是,我得到了一個錯誤,指出它是不是有效的base64圖像。
以下代碼(用於讀取圖像並將其轉換爲base64字符串)在我從本地磁盤讀取文件時起作用。 (我使用的OCR API可以在這裏找到:https://ocr.space/ocrapi#python
OCR.py:!
import requests
import base64
def image_to_text(base64_encoded_string=None,content_type="jpeg"):
filename = 'image.jpg'
with open(filename, 'rb') as f:
encoded_string = base64.b64encode(f.read()).strip('\n')
api_key = "API_KEY"
overlay = False # Boolean value indicating if the overlay is required along with the image/pdf parsed result
language = 'eng'
payload = {'isOverlayRequired': overlay,
'apikey': api_key,
'language': language,
'base64Image':"data:image/{};base64,{}".format(content_type,
encoded_string)
}
r = requests.post('https://api.ocr.space/parse/image',
data=payload)
return r.content.decode()
任何幫助將不勝感激感謝