1, PIL introduction and picture division
Python 3 installation: pip3 install Pillow
1.1 image module
Image module is a common module in Python PIL image processing, mainly used for the basic processing of this image, which is used in conjunction with open, save, convert, show ... and other functions.
from PIL import Image #open file means open a file in pycharm im = ('') #Show Pictures ()
1. Crop class
Copy this image. If the user wants to paste some data to this image, this method can be used, but the original image will not be affected.
(box) ⇒ image
Returns a copy of a rectangular region from the current image. The variable box is a quaternion that defines the coordinates of the left, top, right and bottom pixels. It is used to represent the coordinates of the position intercepted in the original image, e.g. box(100,100,200,200) means that a 100*100 (in pixels) image is intercepted in the original image with the upper left corner as the coordinate origin.
from PIL import Image im = ("") ## Determine the size of the copy area box = (5, 41, 72, 108) ## Copy the image object represented by im into region, size box region = (box) ()
Practice 1:12306 image segmentation and save
from PIL import Image #Cut the image, since the downloaded images are in a fixed position, just control the pixels to cut them directly def cut_img(im, x, y): assert 0 <= x <= 3 assert 0 <= y <= 2 left = 5 + (67 + 5) * x top = 41 + (67 + 5) * y right = left + 67 bottom = top + 67 return ((left, top, right, bottom)) if __name__ == '__main__': im = ("./") # Controls the y-axis for y in range(2): # Controls the x-axis for x in range(4): im2 = cut_img(im, x, y) ('./images/%s_%'%(y,x))
2、Baidu platform interface implementation
2.1. platform access:
1. Open/Go to the console and select Text Recognition Service.
2. Create the application as shown:
3. Enter the application name and description, and select the application type, then click the "Create Now" button.
4. When you are done, click "Back to Application List".
5. AK, SK are shown here and will be used in the program later.
3. Access to official documents
1. Open/docs#/OCR-API/top documentation
The information that needs to be used is:
(1) Image Recognition URL: /rest/2.0/image-classify/v2/advanced_general
(2) Header format: Content-Type: application/x-www-form-urlencoded
(3) request parameters: image and multi_detect two parameters, image for the image data, base64 encoding urlencode, base64 encoding and urlencode size does not exceed 4M.
(4) return parameters: license plate color Color, license plate number number and so on.
#!/usr/bin/python # -*- coding: utf-8 -*- import base64 import requests import os import time #todo:Get Baidu Permission Verification Code access_token def get_token(): get_token_url = "/oauth/2.0/token" params = { "grant_type": "client_credentials", "client_id": "7ax98QuWU5l2zTbaOkzvKgxE", "client_secret": "INugQTM2DAfNFgfxtvgR7eF8AHPFGP5t", } res = (get_token_url, params).json() return res["access_token"] #todo: Identify items by permission CAPTCHA and image def get_result(access_token,image): url = "/rest/2.0/image-classify/v2/advanced_general" # Open the file and encode it with open(image, 'rb')as f: image = base64.b64encode(()) # image = # Header information headers = { 'Content-Type': 'application/x-www-form-urlencoded' } #Send data data = { "access_token": access_token, "image": image } # Send a request and return identification data res = (url, headers=headers, data=data).json() if res: result = res['result'] return result #todo:Get image key items def get_keywords(result): # Sort by maximum match rate and get the last left max_score = sorted(result,key=lambda x:x['score'])[-1] # print(max_score['keyword']) keyword = max_score['keyword'] return keyword if __name__ == '__main__': access_token = get_token() get_result(access_token,'') datas = [] for root,dir,files in ('images'): for file in files: image = (root,file) result = get_result(access_token,image) keyword = get_keywords(result) print(keyword) (1) (keyword) print(datas)
Summary:
- Introduction to PIL and image segmentation
- Baidu AI Image Recognition Example Building
- Identify 12306 Category Code
This is the whole content of this article.