def scan(self, image): print "Saving image..." output = TemporaryFile.create('.tif') image.save(output, 'TIFF') print "Loading image with gamera..." img = load_image(output) TemporaryFile.remove(output) print "Converting to greyscale..." img = img.to_greyscale() print "Thresholding..." onebit = img.otsu_threshold() # Get connected components from the image print "Getting connected components" ccs = onebit.cc_analysis() # Classify #classifier = knn.kNNInteractive() #classifier.from_xml_filename('training.xml') #classifier.classify_list_automatic( css ) print "Initiating classifier" classifier = kNNNonInteractive(ccs) import ocr o = ocr.Ocr() print "Scanning with tesseract" o.scan(image) print "Teaching gamera" for c in ccs: print "Glyph: ", c text = o.textInRegion(c) classifier.classify_glyph_manual(c, text)
def ocr_process(): # 载入ocr模型 model = ocr.Ocr(text_process=False) print("load model done") while True: # 尝试从redis数据库取出一队图像数据 queue = db.lrange(settings.IMAGE_QUEUE, 0, settings.BATCH_SIZE - 1) # 用于放置每幅图像的id imageIDs = [] batch = None # 遍历图像数据队列 for q in queue: # 解码图像数据 q = json.loads(q.decode("utf-8")) image = helpers.base64_decode_image(q["image"], settings.IMAGE_DTYPE, q["shape"]) # 检查数据批是否为空 if batch is None: batch = image # 不为空则将数据堆叠起来,形成一批数据 else: batch = np.vstack([batch, image]) # 更新图像id列表 imageIDs.append(q["id"]) # 检查是否需要进行模型预测 if len(imageIDs) > 0: # 处理数据块 results = [] print("* Batch size: {}".format(batch.shape)) for img in batch: preds = model.predict(img) results.append(preds) for (imageID, resultSet) in zip(imageIDs, results): # 初始化预测结果 output = [] # 初始化文本检测结果 boxes = [] for box in resultSet[1]: boxes.append([box[xy] for xy in range(8)]) r = {"words": resultSet[0], "boxes": boxes} output.append(r) # 存储检测与识别结果 db.set(imageID, json.dumps(output, ensure_ascii=False)) db.ltrim(settings.IMAGE_QUEUE, len(imageIDs), -1) # 延时 time.sleep(settings.SERVER_SLEEP)
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with this program; if not, write to the # Free Software Foundation, Inc., # 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. import ocr import sys import codecs from PySide.QtGui import * ocr.initOcrSystem() c = ocr.Ocr() image = QImage() if not image.load( sys.argv[-1] ): print 'Error loading image' os.exit(1) c.scan( image ) print c.formatedText().encode('ascii','ignore') f=codecs.open( '/tmp/output.txt', 'w', 'utf-8' ) f.write( c.formatedText() ) f.close() print c.slope() c.deskewOnce() c.image.save( '/tmp/rotated.png', 'PNG' ) #print "Image stored in /tmp/rotated.png"
def POST(self): def get_id(img): _token = gt.get_token() _url = 'https://api.weixin.qq.com/cgi-bin/media/upload?access_token=' + _token + '&type=image' print('url=', _url) payload = {'file': ('upload.jpg', img, 'image/jpg')} m = MultipartEncoder(payload) headers = { 'Content-Type': m.content_type, 'other-keys': 'other-values' } _id = requests.post(_url, headers=headers, data=m).json() print('post request returns:', _id) _id = _id['media_id'] print(_id) return _id #try: webData = web.data() print("Handle Post webdata is ", webData) #后台打日志 recMsg = receive.parse_xml(webData) #print(recMsg.Content.decode('utf-8')) _data = '' if isinstance(recMsg, receive.Msg): #print(urls.triggers) toUser = recMsg.FromUserName fromUser = recMsg.ToUserName if recMsg.MsgType == 'text': for trigger in urls.triggers: if trigger in recMsg.Content.decode('utf-8'): print('got the keyword:', trigger) for key, item in urls.URL.items(): if item['trigger'] == trigger: _item = item break if _item['type'] == 'txt_event': print('now making poem...') _data = _item['keyword']() print(_data) replyMsg = reply.TextMsg(toUser, fromUser, _data) return replyMsg.send() else: _data = gf.getData(_item['url'], data_type=_item['type']) if _data: if _item['type'] == 'txt' or _item[ 'type'] == 'json': if _item['type'] == 'json': _data = _data[_item['keyword']] replyMsg = reply.TextMsg( toUser, fromUser, _data) elif _item['type'] == 'img': _id = get_id(_data) replyMsg = reply.ImageMsg( toUser, fromUser, _id) return replyMsg.send() else: print('request data failed') _data = '' break else: _data = urls.DEFAULT_MSG replyMsg = reply.TextMsg(toUser, fromUser, _data) return replyMsg.send() elif recMsg.MsgType == 'image': _url = recMsg.PicUrl _data = gf.getData(_url, data_type='img') _data = ocr.Ocr(_data) _id = get_id(_data) replyMsg = reply.ImageMsg(toUser, fromUser, _id) return replyMsg.send() if _data == '': print("暂且不处理") return "success"
def start(self,inputFile, outputFile): _jsonLib = jsonLib.JsonLib(self._constants) variables = self._constants._PATH_VARIABLES textType = _jsonLib.getValue(variables,self._constants._FIELD_TEXT_TYPE) textTypeCodecs = _jsonLib.getValue(variables,self._constants._FIELD_TEXT_TYPE_CODECS) languageType = _jsonLib.getValue(variables,self._constants._FIELD_LANGUAGE_CURRENT) languagePath = _jsonLib.getValue(variables,self._constants._PATH_LANGUAGE)+languageType+self._constants._EXTENSION_JSON extension = os.path.splitext(inputFile)[self._constants._INDEX_POSITION_EXTENSION][self._constants._INDEX_POSITION_SPLIT_ORDER:] if (inputFile == self._constants._FIELD_EMPTY_STRING or outputFile == self._constants._FIELD_EMPTY_STRING): print(_jsonLib.getValue(languagePath,self._constants._FIELD_ERROR_PARAMETERS)) return self._constants._INDEX_RETURN_ERROR if not (os.path.isfile(inputFile)): print(_jsonLib.getValue(languagePath,self._constants._FIELD_ERROR_INPUT)) return self._constants._INDEX_RETURN_ERROR if (not (os.path.dirname(outputFile))) and (os.path.dirname(outputFile) != self._constants._FIELD_EMPTY_STRING): print(_jsonLib.getValue(languagePath,self._constants._FIELD_ERROR_OUTPUT)) return self._constants._INDEX_RETURN_ERROR if not (extension in _jsonLib.getValue(variables,self._constants._FIELD_IMAGE_EXTENSION)): print(_jsonLib.getValue(languagePath,self._constants._FIELD_ERROR_FILE_TYPE)) return self._constants._INDEX_RETURN_ERROR print(_jsonLib.getValue(languagePath,self._constants._FIELD_MESSAGE_WELCOME)) internalImage = _jsonLib.getValue(variables,self._constants._FIELD_IMAGE_FILENAME) internalText = _jsonLib.getValue(variables,self._constants._FIELD_TEXT_FILENAME) _image = image.Image(self._constants) _image.fixImage(inputFile,internalImage) if (_image): del _image _ocr = ocr.Ocr(self._constants) _ocr.readPhoto(internalImage,internalText,textTypeCodecs) if (_ocr): del _ocr _spellcheck = spellcheck.Spellcheck(self._constants) _spellcheck.verifyFile(internalText,languageType,textType,textTypeCodecs) if (_spellcheck): del _spellcheck ttsLanguage = _jsonLib.getValue(languagePath,self._constants._FIELD_GTTS_CODE) _tts = tts.Tts(self._constants) audioExists = _tts.convertText(internalText,outputFile,ttsLanguage) if (_tts): del _tts if (audioExists == self._constants._INDEX_RETURN_OK): print(_jsonLib.getValue(languagePath,self._constants._FIELD_MESSAGE_FINISH)) if (_jsonLib): del _jsonLib _functions = functions.Functions(self._constants) _functions.clearAll(internalImage,internalText) if (_functions): del _functions