Skip to content

Commit

Permalink
feat: Added adaptation for batch input.
Browse files Browse the repository at this point in the history
  • Loading branch information
kerlomz committed Oct 18, 2018
1 parent 25180e2 commit 3e15e71
Show file tree
Hide file tree
Showing 11 changed files with 203 additions and 164 deletions.
5 changes: 5 additions & 0 deletions config.py
Expand Up @@ -64,6 +64,8 @@ def __init__(self, model_conf='model.yaml', model_path='model', print_info=True)
self.system = None
self.device = None
self.charset = None
self.split_flag = b'\x00\xff\xff\xff\x00'
self.split_char = None
self.gen_charset = None
self.char_exclude = None
self.charset_len = None
Expand Down Expand Up @@ -106,6 +108,9 @@ def assignment(self):

self.target_model = self.cf_model['Model'].get('ModelName')

self.split_char = self.cf_model['Model'].get('SplitChar')
self.split_char = '' if not self.split_char else self.split_char

self.binaryzation = self.cf_model['Pretreatment'].get('Binaryzation')
self.smooth = self.cf_model['Pretreatment'].get('Smoothing')
self.blur = self.cf_model['Pretreatment'].get('Blur')
Expand Down
35 changes: 19 additions & 16 deletions demo.py
Expand Up @@ -73,7 +73,7 @@ def local_iter(self, image_list: dict):
if _true:
self.true_count += 1
self.total_count += 1
print('result: {}, label: {}, flag: {}, acc_rate: {}'.format(code, k, _true, self.true_count/self.total_count))
print('result: {}, label: {}, flag: n{}, acc_rate: {}'.format(code, k, _true, self.true_count/self.total_count))


class NoAuth(object):
Expand Down Expand Up @@ -108,7 +108,7 @@ def request(self, image):
import grpc_pb2_grpc
channel = grpc.insecure_channel(self._url)
stub = grpc_pb2_grpc.PredictStub(channel)
response = stub.predict(grpc_pb2.PredictRequest(captcha_img=image))
response = stub.predict(grpc_pb2.PredictRequest(captcha_img=image, split_char=','))
return {"message": {"result": response.result}, "code": response.code, "success": response.success}

def local_iter(self, image_list: dict):
Expand All @@ -124,8 +124,8 @@ def local_iter(self, image_list: dict):
if __name__ == '__main__':

# Here you can replace it with a web request to get images in real time.
# with open(r"E:\***\***\***.jpg", "rb") as f:
# img_bytes = f.read()
with open(r"D:\***\***\***.jpg", "rb") as f:
img_bytes = f.read()

# # Here is the code for the network request.
# # Replace your own captcha url for testing.
Expand All @@ -140,11 +140,10 @@ def local_iter(self, image_list: dict):
# data_stream = io.BytesIO(img_bytes)
# pil_image = PilImage.open(data_stream)
# pil_image.show()

# api_params = {
# 'image': base64.b64encode(img_bytes).decode(),
# }

api_params = {
'image': base64.b64encode(img_bytes).decode(),
}
print(api_params)
for i in range(1):
# Tornado API with authentication
# resp = Auth(DEFAULT_HOST, ServerType.TORNADO).request(api_params)
Expand All @@ -163,13 +162,17 @@ def local_iter(self, image_list: dict):
# print(resp)

# API by gRPC - The fastest way.
# resp = GoogleRPC(DEFAULT_HOST).request(base64.b64encode(img_bytes).decode())
# print(resp)

# If you want to identify multiple verification codes continuously, please do like this:
# resp = GoogleRPC(DEFAULT_HOST).request(base64.b64encode(img_bytes+b'\x00\xff\xff\xff\x00'+img_bytes).decode())
# b'\x00\xff\xff\xff\x00' is the split_flag defined in config.py
resp = GoogleRPC(DEFAULT_HOST).request(base64.b64encode(img_bytes).decode())
print(resp)
pass

# API by gRPC - The fastest way, Local batch version, only for self testing.
path = r"D:\***\***\***"
path_list = os.listdir(path)
batch = {i.split('_')[0].lower(): _image(os.path.join(path, i)) for i in path_list}
GoogleRPC(DEFAULT_HOST).local_iter(batch)
# path = r"D:\***\***\***"
# path_list = os.listdir(path)
# print(path_list)
# batch = {i.split('_')[0].lower(): _image(os.path.join(path, i)) for i in path_list}
# print(batch)
# GoogleRPC(DEFAULT_HOST).local_iter(batch)
1 change: 1 addition & 0 deletions grpc.proto
Expand Up @@ -6,6 +6,7 @@ service Predict {

message PredictRequest {
string captcha_img = 1;
string split_char = 2;
}

message PredictResult {
Expand Down
216 changes: 115 additions & 101 deletions grpc_pb2.py

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 3e15e71

Please sign in to comment.