【使用攻略】【图像识别】红酒识别
wangwei8638 发布于2019-05 浏览:5596 回复:6
2
收藏
最后编辑于2022-04

很多人不知道怎么辨别红酒,往往买到假酒、劣质酒吃亏也吃亏在这个地方。其实红酒标签里,其实隐藏了很多的信息,比如:国家,产区,采摘葡萄年份,酒庄,酒精度,生产商等,通过AI图像识别即可得到这些信息。

一.平台接入

此步骤比较简单,不多阐述。可参照之前文档:

https://ai.baidu.com/forum/topic/show/943028

二.分析接口文档

1. https://ai.baidu.com/docs#/ImageClassify-API/5e3dd57a

   (1)接口描述

该服务用于识别红酒标签,即对于输入的一张图片(可正常解码,长宽比适宜,且酒标清晰可见),输出图片中的红酒名称、国家、产区、酒庄、类型、糖分、葡萄品种、酒品描述等信息。可识别数十万中外常见红酒。

注意:图片中酒标要清晰可见,示例图片如下

 (2)请求说明

需要用到的信息有:

请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine

Header格式:Content-Type:application/x-www-form-urlencoded

请求参数:image, 图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式 。注意:图片需要base64编码、去掉编码头后再进行urlencode。

(3)返回示例

   仅识别出红酒名称,hasdetail = 0:

{

   "log_id": 3450013152046070669,

   "result":

   {

      "wineNameCn": "银色高地阙歌干红",

      "hasdetail":0

   }

}

识别出详细信息,hasdetail = 1:

{

   "log_id": 2495538539661269738,

   "result": {

      "classifyByColor": "红葡萄酒/Red Wine",

      "subRegionCn": "梅多克",

      "wineNameCn": "拉图嘉利庄园红葡萄酒(正牌)",

      "subRegionEn": "Medoc",

      "regionEn": "Bordeaux",

      "color": "深紫红色/Dark Violet",

      "wineNameEn": "Chateau La Tour Carnet",

      "hasdetail": 1,

      "wineryCn": "拉图嘉利庄园",

      "classifyBySugar": "干型/Dry",

      "tasteTemperature": "16-18℃",

      "regionCn": "波尔多",

      "wineryEn": "Chateau La Tour-Carnet",

      "grapeCn": "",

      "grapeEn": "",

      "countryCn": "法国",

      "countryEn": "France",

      "description": "此酒充满红果和黑果味道,并带有矿物质和花香(紫罗兰,玫瑰),混合些许香草气息,单宁柔软,余香悠长。在口中留下清新的味道,香料和香草味道萦绕口中。"

   }

}

2.获取access_token

#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】

#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key


三.识别结果

 1.单张图片

识别结果:{"log_id": 8563171063159062561,

 "result": {"classifyByColor": "红葡萄酒/Red Wine",

 "subRegionCn": "夜丘",

 "wineNameCn": "罗曼尼·康帝酒庄罗曼尼-康帝特级园红葡萄酒",

"subRegionEn": "Cote de Nuits",

"regionEn": "Bourgogne(Burgundy)",

 "color":"宝石红色/Ruby",

 "wineNameEn": "Domaine De La Romanee-Conti Romanee-Conti",

 "hasdetail": 1,

"wineryCn": "罗曼尼·康帝酒庄",

"classifyBySugar": "干型/Dry", "tasteTemperature": "12-18°C",

"regionCn": "勃艮第",

"wineryEn": "Domaine De La Romanee-Conti",

"grapeCn": "",

"grapeEn": "",

 "countryCn": "法国",

 "countryEn": "France",

"description": "此酒呈宝石红色,带有水果味、香料味、焦油、巧克力香气,口感和谐,饱满,细腻的单宁。"}}

2.多张图片

采用10张图片,进行批量处理

识别结果:

C:\Users\Administrator>python hongjiu10.py

F:/paddle/redwine/laf1.jpg

{'log_id': 3218894919047488129, 'result': {'hasdetail': 0, 'wineNameCn': ''}}

用时  :1.10 s

F:/paddle/redwine/laf2.jpg

{'log_id': 6432701731714996385, 'result': {'hasdetail': 0, 'wineNameCn': ''}}

用时  :1.21 s

F:/paddle/redwine/laf3.jpg

{'log_id': 5982108611892492321, 'result': {'hasdetail': 0, 'wineNameCn': ''}}

用时  :1.45 s

F:/paddle/redwine/laf4.jpg

{'log_id': 8285815439575054753, 'result': {'tasteTemperature': '7-10°C', 'subRe

gionEn': '', 'wineryCn': '拉菲罗斯柴尔德集团', 'wineryEn': 'Domaines Barons De R

othschild Lafite', 'color': '浅柠檬色/Light Lemon', 'regionCn': '波尔多', 'count

ryEn': 'France', 'grapeCn': '', 'regionEn': 'Bordeaux', 'classifyByColor': '白葡

萄酒/White Wine', 'hasdetail': 1, 'grapeEn': '', 'subRegionCn': '', 'classifyByS

ugar': '干型/Dry', 'wineNameCn': '拉菲罗斯柴尔德集团传奇波尔多白葡萄酒', 'descri

ption': '此酒呈浅柠檬色,散发出成熟水果的芳香,口感馥郁、柔软,余味充满诱人的果

味。', 'countryCn': '法国', 'wineNameEn': 'Domaines Barons De Rothschild Lafite

Legende Bordeaux'}}

用时  :1.50 s

F:/paddle/redwine/laf5.jpg

{'log_id': 724288871593192801, 'result': {'hasdetail': 0, 'wineNameCn': '智利 拉

菲旗下巴斯克华诗歌赤霞珠干红葡萄酒'}}

用时  :1.57 s

F:/paddle/redwine/lmn1.jpg

{'log_id': 1448942443258803649, 'result': {'hasdetail': 0, 'wineNameCn': '罗曼尼

·康帝酒庄罗曼尼-康帝特级园红葡萄酒'}}

用时  :1.36 s

F:/paddle/redwine/lmn2.jpg

{'log_id': 6125082451123591713, 'result': {'hasdetail': 0, 'wineNameCn': '罗曼尼

·康帝酒庄罗曼尼-康帝特级园红葡萄酒'}}

用时  :1.23 s

F:/paddle/redwine/lmn3.jpg

{'log_id': 8218462884308559937, 'result': {'hasdetail': 0, 'wineNameCn': '罗曼尼

·康帝酒庄拉塔希特级园红葡萄酒'}}

用时  :1.62 s

F:/paddle/redwine/lmn4.jpg

{'log_id': 5786004320273430337, 'result': {'hasdetail': 0, 'wineNameCn': '罗曼尼

·康帝酒庄罗曼尼-康帝特级园红葡萄酒'}}

用时  :1.74 s

F:/paddle/redwine/lmn5.jpg

{'log_id': 129239502241767745, 'result': {'tasteTemperature': '12-18°C', 'subRe

gionEn': 'Cote de Nuits', 'wineryCn': '罗曼尼·康帝酒庄', 'wineryEn': 'Domaine D

e La Romanee-Conti', 'color': '宝石红色/Ruby', 'regionCn': '勃艮第', 'countryEn'

: 'France', 'grapeCn': '', 'regionEn': 'Bourgogne(Burgundy)', 'classifyByColor':

 '红葡萄酒/Red Wine', 'hasdetail': 1, 'grapeEn': '', 'subRegionCn': '夜丘', 'cla

ssifyBySugar': '干型/Dry', 'wineNameCn': '罗曼尼·康帝酒庄罗曼尼-康帝特级园红葡

萄酒', 'description': '此酒呈宝石红色,带有水果味、香料味、焦油、巧克力香气,口

感和谐,饱满,细腻的单宁。', 'countryCn': '法国', 'wineNameEn': 'Domaine De La R

omanee-Conti Romanee-Conti'}}

用时  :1.59 s

准确率方面:10张图有7张返回识别结果,其中5张只返回名称,2张返回详细信息。

处理速度方面:10张图片,平均处理用时1.437s,还不错的响应结果。

四.代码共享

# -*- coding: utf-8 -*-
#!/usr/bin/env python

import os
import requests
import base64
import json
from pprint import pprint
import time
import io
from PIL import Image
#api_key 为官网获取的AK, secret_key 为官网获取的SK
api_key = '***********'
secret_key = '**************'

class RedwineRecognizer(object):
    def __init__(self, api_key, secret_key):
        self.access_token = self._get_access_token(api_key=api_key, secret_key=secret_key)
        self.API_URL = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/redwine' + '?access_token=' \
                      + self.access_token
    #获取token
    @staticmethod
    def _get_access_token(api_key, secret_key):
        api = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \
            '&client_id={}&client_secret={}'.format(api_key, secret_key)
        rp = requests.post(api)
        if rp.ok:
            rp_json = rp.json()
#            print(rp_json['access_token'])
            return rp_json['access_token']
        else:
            print('=> Error in get access token!')
    def get_result(self, params):
        rp = requests.post(self.API_URL, data=params)
        if rp.ok:
#            print('=> Success! got result: ')
            rp_json = rp.json()
#            pprint(rp_json)
            return rp_json
        else:
            print('=> Error! token invalid or network error!')
#            print(rp.content)
            return None
    #返回红酒信息
    def detect(self, img_path):

        f = open(img_path, 'rb')
        img_str = base64.b64encode(f.read())
        params = {'image': img_str}
        tic = time.clock()
        rp_json = self.get_result(params)
        print(rp_json)
        toc = time.clock()
        print('用时  :'+'%.2f'  %(toc - tic) +' s')

if __name__ == '__main__':
    recognizer = RedwineRecognizer(api_key, secret_key)
    in_path = 'F:/paddle/redwine'  # 目标文件夹位置
    in_path_list = []
    in_content_name = os.listdir(in_path)  # 返回目录下的所有文件名
    for in_name in in_content_name:
        in_content_path = in_path + '/'+in_name
        in_path_list.append(in_content_path)

#批量处理图片
    n = 0
    for path in in_path_list[0:]:
        if os.path.isdir(path) is True:
            out_folder_name = path.split('/')[-1]
          # print(out_folder_name)
        elif n <= len(in_path_list):
            img = path
            print(img)
            recognizer.detect(img)
            n += 1
收藏
点赞
2
个赞
共6条回复 最后由用户已被禁言回复于2022-04
#7worddict回复于2020-07

感觉很方便

0
#6wangwei8638回复于2020-07
#2 百分一bb回复
怎样把执行的结果保存为xlsx(代码怎样写),如果要加上登陆系统,要用什么连接起来  
展开

百度一下,应该挺多的

0
#5wangwei8638回复于2020-07
#3 白羊刘建豪回复
可以可以,挺强的。有考虑过做成APP直接用吗?

0
#4wangwei8638回复于2020-07
#3 白羊刘建豪回复
可以可以,挺强的。有考虑过做成APP直接用吗?

集成到了小程序

0
#3白羊刘建豪回复于2020-07

可以可以,挺强的。有考虑过做成APP直接用吗?

0
#2百分一bb回复于2020-06

怎样把执行的结果保存为xlsx(代码怎样写),如果要加上登陆系统,要用什么连接起来

 

0
TOP
切换版块