Verification code recognition is an unavoidable problem for crawler to realize automatic script. Typically captcha identifiers are deployed either locally or on the server side. If deployed on the server side, it is necessary to build and configure the network environment and write the call interface, which is an extremely tedious and time-consuming process.
This article was contributed by Serverless community users as “just as anything”
But now we can quickly release the local verification code recognition program online through Tencent Yunyun function SCF, which greatly improves the development efficiency.
Results show
As you can see, the recognition is pretty good, even better than the visual recognition rate.
steps
The traditional verification code identification process is
- Image preprocessing (ashing, denoising, cutting, binarization, interference line removal, etc.)
- Verification code character Feature Extraction (SVM, CNN, etc.)
- Verification code recognition
Let me take you through the process of creating, writing, and publishing an authentication cloud function
Step 1: Create a New Python cloud function
See the series of articles: Fast Development of Full-stack Applications using SCF+COS for Everything Serverless
Step 2: Write the validation to identify the cloud function
Life is short, show me the code.
Here I will take a most simple verification code recognition program as an example, directly on the code
Chars = {'1': [1, 1, 1, 0, 1... , '2': [1, 0, 0, 1, 0...] , '3': [0, 1, 0, 0, 1...] , # Other character characteristics... Def clearedge(img): for y in range(img. Size [1]): img.putpixel((0, y), 255) img.putpixel((1, y), 255) img.putpixel((2, y), 255) img.putpixel((img.size[0]-1, y), 255) img.putpixel((img.size[0]-2, y), 255) img.putpixel((img.size[0]-3, y), 255) for x in range(img.size[0]): img.putpixel((x, 0), 255) img.putpixel((x, 1), 255) img.putpixel((x, 2), 255) img.putpixel((x, img.size[1]-1), 255) img.putpixel((x, img.size[1]-2), 255) img.putpixel((x, img.size[1]-3), Def clearline(img): for y in range(img.size[1]): for x in range(img.size[0]): if int(img.getpixel((x, y))) >= 110: img.putpixel((x, y), 0xff) else: Img. putpixel((x, y), 0x0) return img # def del_noise(im, pnum=3): w, h = im.size white = 255 black = 0 for i in range(0, w): im.putpixel((i, 0), white) im.putpixel((i, h - 1), white) for i in range(0, h): im.putpixel((0, i), white) im.putpixel((w - 1, i), white) for i in range(1, w - 1): for j in range(1, h - 1): val = im.getpixel((i, j)) if val == black: cnt = 0 for ii in range(-1, 2): for jj in range(-1, 2): if im.getpixel((i + ii, j + jj)) == black: cnt += 1 if cnt < pnum: im.putpixel((i, j), white) else: cnt = 0 for ii in range(-1, 2): for jj in range(-1, 2): if im.getpixel((i + ii, j + jj)) == black: CNT += 1 if CNT >= 7: im.putPixel ((I, j), black) return def two_value(code_data): Table = [serverless] for I in code_data: if I < 140: # binary line 140 table.append(0) else: Table.append (1) return table # def pre_img(img): Img = clearline(img) # img = del_noise(img) # return img # Def data_img(img): code_data = [serverless] # code_data for I in range(4): Code_data.append (img.crop((x, 9, x + 18, Code_data [I] = two_value(code_data[I]) # return code_data # def identify(data): Code = ["]*4 # Diff_min = [432]*4 # For char in chars: Diff = [0]*4 # For I in range(4): # for j in range(432): If data[I][J]! = chars[char][j]: diff[I] += 1 # for range(4): if diff[I] < diff_min[I]: Def predict(imgs): diff_min[I] = diff_min[I] = diff_min[I] = char [I] Img = image.open(IO.BytesIO(img)) img = pre_img(img) data = data_img(img Code = identify(data) # return code def apiReply(reply, code=200): return {"isBase64Encoded": False, "statusCode": code, "headers": {'Content-Type': 'application/json', "Access-Control-Allow-Origin": "*"}, "body": json.dumps(reply, ensure_ascii=False) } def main_handler(event, context): main_start = time.time() flag = True if 'image' in event['queryString'] else False code = Predict (event['queryString']['image']) if 'image' in event['queryString'] else 'invalid' return apiReply({'ok': flag, 'code': code, 'spendTime': str(time.time()-main_start) })Copy the code
As usual, let’s go through the flow of the cloud function.
def main_handler(event, context):
main_start = time.time()
flag = True if 'image' in event['queryString'] else False
code = predict(event['queryString'] ['image']) if 'image' in event['queryString'] else 'Invalid request'
return apiReply({
'ok': flag,
'code': code,
'spendTime': str(time.time()-main_start)
})
Copy the code
First of all, we get the image data of the verification code requested by API through the event event, and then determine whether the image parameter exists. If not, we will return a message indicating that the request is invalid
def predict(imgs): Img = image.open(IO.BytesIO(img)) img = pre_img(img) data = data_img(img Code = identify(data) # Return codeCopy the code
If the image request parameter exists, the predict function is called to parse the identification verification code, and the process is as follows:
- Read the captcha image
- Verification code image preprocessing
- Identify the verification code after processing
Def pre_img(img): Img = clearline(img) # del_noise(img) # return imgCopy the code
Let’s look at image preprocessing
- Remove the color of the verification code and turn it into a grayscale image
- Remove captcha black border
- Remove verification code interference lines
- Remove noise of verification code
Chars = {'1': [1, 1, 1, 0, 1... , '2': [1, 0, 0, 1, 0...] , '3': [0, 1, 0, 0, 1...] , # Other character characteristics... } # def identify(data): Code = ["]*4 # Diff_min = [432]*4 # For char in chars: Diff = [0]*4 # For I in range(4): # for j in range(432): If data[I][J]! = chars[char][j]: diff[I] += 1 # for range(4): if diff[I] < diff_min[I]: # diff_min[I] = diff[I] # code[I] = char # return ". Join (code) #Copy the code
PS: The character features in this article, Chars, are not complete and you may need to extract all the features yourself.
Finally look at some of the verification code identification process: here we are simple and crude to directly take after processing the image data of all pixels as the characteristics of the characters (the so-called avenue to Jane), will then stay each character processing image data after compared with all the characteristics of the characters one by one, the most similar to the character as a recognition result.
Well, if there’s nothing wrong, you’ll get the correct result.
Step 3: Publish cloud functions online, add API gateway triggers, and enable response integration
See the series of articles: Fast Development of Full-stack Applications using SCF+COS for Everything Serverless
Write in the last
Of course, the above just takes a simple verification code recognition as an example. For some complicated verification codes, you can also use deep learning computing frameworks such as Tensorflow and Pytorch to build and train models, and then quickly publish the trained models online with the help of serverless cloud functions.
Serverless Framework 30-day trial plan
We invite you to experience the most convenient way to develop and deploy Serverless. During the trial period, all related products and services provide free resources and professional technical support to help your business quickly and easily achieve Serverless!
For details, see: Serverless Framework Trial Plan
One More Thing
What can you do in 3 seconds? Take a sip of water, read an email, or — deploy a full Serverless application?
Copy the link to PC browser to access: serverless.cloud.tencent.com/deploy/expr…
Fast deployment in 3 seconds, experience the fastest Serverless HTTP development in history immediately!
Portal:
- GitHub: github.com/serverless
- Website: serverless.com
Welcome to Serverless Chinese, you can experience more about Serverless application development in the best practices!
Recommended reading: Serverless Architecture: From Principle, Design to Project Practice