我在Google App Engine中有以下代码,它接受来自用户的图像,然后对该图像执行OCR。
索引.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()
任何帮助将不胜感激。谢谢!
哦,
等等,我通过这样做自己解决了它:
encoded_string = base64.b64encode(image).strip('n')