Closed AhmadShaik closed 4 years ago
I am trying to do the same, but no luck. Any help here would be much appreciated. Thank you.
Modify line 263 in main.py file. Hope it helps.
Execute with this command:
python main.py predict (Two under score before main and two underscore after main)
Thanks a lot, Piyush. Big help. The code could not have been more elegant.
@githubpiyush @githubpiyush I have updated the code in main.py. From where do I need to run main.py file. Could anyone of you can explain in more detail.
@AhmadShaik You just need to go inside aocr folder open terminal over there and execute under score main.py file with predict as argument (Enter the directory path in your code where i mentioned enter directory absolute path in the code snippet )
@githubpiyush i have modified the code in the main.py and ran the command python main.py predict i didnt get any result.
Try :python main.py --predict
Note:there is two underscores before and after main of course. It is not appearing for some reason.
@sushanth-d Thanks man. That works. But im getting a new error. Do i need do any installation after cloning the repository. I just cloned the repo and modified the main.py and ran the above code. Now im getting this error. Can you let me know if there is any installation steps. I did pip installation aocr before. Is there anything else i should do before i run the code.
Traceback (most recent call last):
File "main.py", line 14, in
It is importing some module and it is not present there which is causing this error.
If you use this repo in ubuntu then you just need to use this code in your main.py file: import sys sys.path.insert(0, "/home/Desktop/aocr ")
Note: Give your aocr folder directory path in above code. You need to add this code above the module imports. Please find the windows equivalent of this. Plus you need to remove the proceeding '.' From all the imports. For example: from .model.model import Model Becomes this from model.model import Model
Thanks @githubpiyush. I changed that to from model.model import Model. Now im facing this error.
Traceback (most recent call last):
File "main.py", line 24, in
Remove those '..' too from model.py file or where is that error is coming from. from ..util.data_gen import DataGen become this: from util.data_gen import DataGen If you find any preceding '.' in import please remove them.
Any success ?
@githubpiyush I have cleared the import error.Thanks
th1_1.jpg LLLLLZZZZZ 1.3522936940656732e-16 th1_10.jpg L66ZZZZZZZ 1.3564668442314004e-16 th1_100.jpg L66ZZZZZZZ 1.3502966319449094e-16 th1_101.jpg LLL6ZZZZZZ 1.3328914537112895e-16
i got the above output after passing the cropped text areas of my image. None of my predictions was right. As of now im using tesseract to extract the text which gives decent output after some preprocessing. I have no idea how to use aocr. Should i train my own model to extract the text. can someone please share their knowledge on how to extract text using aocr?
What checkpoints you are using for this prediction?
You can train your own model on your dataset for best results. Training process is already mention in readme file of this repository.
@githubpiyush Thats what im confused. Is there any pre trained models on which i can test my images. As of now i just clone the repositry and ran the test for my images. I dont see any checkpoints in the aocr module.
You have to train it on your data and around 500-1000 epochs.
I am trying to perform the prediction on a single image using the following script. import argparse import tensorflow as tf import numpy as np
def load_graph(frozen_graph_filename): # We load the protobuf file from the disk and parse it to retrieve the # unserialized graph_def with tf.gfile.GFile(frozen_graph_filename, "rb") as f: graph_def = tf.GraphDef() graph_def.ParseFromString(f.read()) # Then, we can use again a convenient built-in function to import a graph_def into the # current default Graph with tf.Graph().as_default() as graph: tf.import_graph_def( graph_def, input_map=None, return_elements=None, name="prefix", op_dict=None, producer_op_list=None ) return graph def getImage(path): with open(path, 'rb') as img_file: img = img_file.read() print(img) return img frozen_model_filename = "exported-model/frozen_graph.pb" graph = load_graph(frozen_model_filename) def ocrImage(image): x = graph.get_tensor_by_name('prefix/input_image_as_bytes:0') y = graph.get_tensor_by_name('prefix/prediction:0') allProbs = graph.get_tensor_by_name('prefix/probability:0') img = getImage(image) with tf.Session(graph=graph) as sess: (y_out, probs_output) = sess.run([y,allProbs], feed_dict={ x: [img] }) # print(y_out) # print(allProbsToScore(probs_output)) return { "predictions": [{ "ocr": str(y_out), "confidence": probs_output }] }; if __name__ == '__main__': # Let's allow the user to pass the filename as an argument parser = argparse.ArgumentParser() # parser.add_argument("--frozen_model_filename", default="checkpoints_pruned/frozen_model.pb", type=str, help="Frozen model file to import") parser.add_argument("--image", default="0_15.png", type=str, help="Path to image") args = parser.parse_args() predictions = ocrImage(args.image) print(str(predictions))
When I run the script I am getting below error:
(/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env) ahmad@irs-aeye-ll-014:/data/work/tvs-part-rec/loc-aocr/aocr_50k$ python predict.py WARNING:tensorflow:From predict.py:21: calling import_graph_def (from tensorflow.python.framework.importer) with op_dict is deprecated and will be removed in a future version. Instructions for updating: Please file an issue at https://github.com/tensorflow/tensorflow/issues if you depend on this feature. 2019-08-06 14:47:13.028800: I tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2 FMA 2019-08-06 14:47:13.090489: I tensorflow/stream_executor/cuda/cuda_gpu_executor.cc:964] successful NUMA node read from SysFS had negative value (-1), but there must be at least one NUMA node, so returning NUMA node zero 2019-08-06 14:47:13.090737: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1432] Found device 0 with properties: name: GeForce GTX 1060 major: 6 minor: 1 memoryClockRate(GHz): 1.6705 pciBusID: 0000:01:00.0 totalMemory: 5.94GiB freeMemory: 5.72GiB 2019-08-06 14:47:13.090754: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1511] Adding visible gpu devices: 0 2019-08-06 14:47:13.408287: I tensorflow/core/common_runtime/gpu/gpu_device.cc:982] Device interconnect StreamExecutor with strength 1 edge matrix: 2019-08-06 14:47:13.408323: I tensorflow/core/common_runtime/gpu/gpu_device.cc:988] 0 2019-08-06 14:47:13.408330: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1001] 0: N 2019-08-06 14:47:13.408416: I tensorflow/core/common_runtime/gpu/gpu_device.cc:1115] Created TensorFlow device (/job:localhost/replica:0/task:0/device:GPU:0 with 5494 MB memory) -> physical GPU (device: 0, name: GeForce GTX 1060, pci bus id: 0000:01:00.0, compute capability: 6.1) Traceback (most recent call last): File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1334, in _do_call return fn(*args) File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1317, in _run_fn self._extend_graph() File "/data/work/tvs-part-rec/CRNN_Tensorflow/crnntf-env/lib/python3.5/site-packages/tensorflow/python/client/session.py", line 1352, in _extend_graph tf_session.ExtendSession(self._session) tensorflow.python.framework.errors_impl.InvalidArgumentError: Cannot assign a device for operation prefix/Rank: Could not satisfy explicit device specification '/device:GPU:0' because no supported kernel for GPU devices is available. Registered kernels: device='XLA_CPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, DT_COMPLEX64, DT_INT64, DT_BOOL, DT_QINT8, DT_QUINT8, DT_QINT32, DT_HALF, DT_UINT32, DT_UINT64] device='XLA_GPU_JIT'; T in [DT_FLOAT, DT_DOUBLE, DT_INT32, DT_UINT8, DT_INT8, ..., DT_QINT32, DT_BFLOAT16, DT_HALF, DT_UINT32, DT_UINT64] device='XLA_CPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL] device='XLA_GPU'; T in [DT_UINT8, DT_QUINT8, DT_INT8, DT_QINT8, DT_INT32, DT_QINT32, DT_INT64, DT_HALF, DT_FLOAT, DT_DOUBLE, DT_COMPLEX64, DT_BOOL, DT_BFLOAT16] device='CPU' device='GPU'; T in [DT_BOOL] device='GPU'; T in [DT_INT32] device='GPU'; T in [DT_VARIANT] device='GPU'; T in [DT_COMPLEX128] device='GPU'; T in [DT_COMPLEX64] device='GPU'; T in [DT_INT8] device='GPU'; T in [DT_UINT8] device='GPU'; T in [DT_INT16] device='GPU'; T in [DT_UINT16] device='GPU'; T in [DT_INT64] device='GPU'; T in [DT_DOUBLE] device='GPU'; T in [DT_FLOAT] device='GPU'; T in [DT_BFLOAT16] device='GPU'; T in [DT_HALF] [[{{node prefix/Rank}} = Rank[T=DT_STRING, _device="/device:GPU:0"](prefix/input_image_as_bytes)]]
I got the same error when I try to run your code. Here is a working version I modified base on your code.
import sys import argparse import os import tensorflow as tf import numpy as np def load_graph(frozen_graph_filename): detection_graph = tf.Graph() with detection_graph.as_default(): od_graph_def = tf.GraphDef() with tf.gfile.GFile(frozen_graph_filename, "rb") as fid: serialized_graph = fid.read() od_graph_def.ParseFromString(serialized_graph) tf.import_graph_def(od_graph_def,name='') return detection_graph
def getImage(path): with open(path, 'rb') as img_file: img = img_file.read()
return img
frozen_model_filename = "exported-model/frozen_graph.pb" detection_graph = load_graph(frozen_model_filename)
def ocrImage(image): img = getImage(image) tf_config = tf.ConfigProto() tf_config.gpu_options.allow_growth = True tf_config.allow_soft_placement=True with detection_graph.as_default(): with tf.Session(graph=detection_graph,config=tf_config) as sess: image_tensor = detection_graph.get_tensor_by_name('input_image_as_bytes:0') prediction_result = detection_graph.get_tensor_by_name('prediction:0') allProbs = detection_graph.get_tensor_by_name('probability:0')
(y_out, probs_output) = sess.run([prediction_result,allProbs],
feed_dict={image_tensor: [img]})
return {
"predictions": [{
"ocr": str(y_out),
"confidence": probs_output
}]
}
if name == 'main':
parser = argparse.ArgumentParser()
parser.add_argument("--image", default="0_15.png", type=str, help="Path to image")
args = parser.parse_args()
predictions = ocrImage(args.image)
print(str(predictions))
There is some problem with your GPU config or kernel is not registered in tensorflow for some operation for example data types in static hash tables.
I am trying to perform the prediction on a single image using the following script. import argparse import tensorflow as tf import numpy as np
When I run the script I am getting below error:
Can someone explain the process to perform recognition on a single image.