1、PIL介紹以及圖片分割
Python 3 安裝: pip3 install Pillow
1.1 image 模塊
Image模塊是在Python PIL圖像處理中常見的模塊,主要是用于對這個圖像的基本處理,它配合open、save、convert、show…等功能使用。
1
2
3
4
5
|
from PIL import Image #打開文件代表打開pycharm中的文件 im = Image. open ( '1.jpg' ) #展示圖片 im.show() |
1、Crop類
拷貝這個圖像。如果用戶想粘貼一些數據到這張圖,可以使用這個方法,但是原始圖像不會受到影響。
im.crop(box) ⇒ image
從當前的圖像中返回一個矩形區域的拷貝。變量box是一個四元組,定義了左、上、右和下的像素坐標。用來表示在原始圖像中截取的位置坐標,如box(100,100,200,200)就表示在原始圖像中以左上角為坐標原點,截取一個100*100(像素為單位)的圖像。
1
2
3
4
5
6
7
|
from PIL import Image im = Image. open ( "pic1.jpg" ) ##確定拷貝區域大小 box = ( 5 , 41 , 72 , 108 ) ##將im表示的圖片對象拷貝到region中,大小為box region = im.crop(box) region.show() |
實戰一:12306圖像分割并保存
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
|
from PIL import Image #切割圖像,由于下載的圖片都是有固定的位置,所以直接控制像素進行切割就行了 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 im.crop((left, top, right, bottom)) if __name__ = = '__main__' : im = Image. open ( "./pic1.jpg" ) #控制y軸 for y in range ( 2 ): #控制x軸 for x in range ( 4 ): im2 = cut_img(im, x, y) im2.save( './images/%s_%s.png' % (y,x)) |
2、百度平臺接口實現
2.1.平臺接入:
1.打開https://ai.baidu.com/進入控制臺,選擇文字識別服務。
2.創建應用,如圖示:
3.輸入應用名稱、描述,并選擇應用類型,之后點擊“立即創建”按鈕。
4.創建完畢,點擊“返回應用列表”。
5.此處顯示AK,SK,后面程序中會用到
3. 官方文檔的讀取
1.打開https://ai.baidu.com/docs#/OCR-API/top 文檔說明
需要用到的信息有:
(1)圖像識別URL: https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general
(2)Header格式:Content-Type:application/x-www-form-urlencoded
(3) 請求參數:image和multi_detect兩個參數,image為圖像數據,base64編碼后進行urlencode,要求base64編碼和urlencode后大小不超過4M。
(4)返回參數:車牌顏色Color、車牌號碼number等。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
#!/usr/bin/python # -*- coding: utf-8 -*- import base64 import requests import os import time #todo:獲取百度權限驗證碼access_token def get_token(): get_token_url = "https://aip.baidubce.com/oauth/2.0/token" params = { "grant_type" : "client_credentials" , "client_id" : "7ax98QuWU5l2zTbaOkzvKgxE" , "client_secret" : "INugQTM2DAfNFgfxtvgR7eF8AHPFGP5t" , } res = requests.get(get_token_url, params).json() return res[ "access_token" ] #todo:通過權限驗證碼和圖片進行識別物品 def get_result(access_token,image): url = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advanced_general" #打開文件并進行編碼 with open (image, 'rb' )as f: image = base64.b64encode(f.read()) # image = #頭部信息 headers = { 'Content-Type' : 'application/x-www-form-urlencoded' } #發送數據 data = { "access_token" : access_token, "image" : image } #發送請求,并返回識別數據 res = requests.post(url, headers = headers, data = data).json() if res: result = res[ 'result' ] return result #todo:獲取圖片關鍵物品 def get_keywords(result): #按照最大匹配率進行排序,并獲取左最后一個 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, 'pic1.jpg' ) datas = [] for root, dir ,files in os.walk( 'images' ): for file in files: image = os.path.join(root, file ) result = get_result(access_token,image) keyword = get_keywords(result) print (keyword) time.sleep( 1 ) datas.append(keyword) print (datas) |
總結:
- PIL介紹以及圖片分割
- 百度AI圖像識別實例搭建
- 識別12306類別碼
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/Smile_Mr/article/details/104020465