3720| 4
|
[项目] 可达鸭助力智能春联 |
一、项目介绍: 对联又称对偶、门对、春贴、春联、桃符等,是一种对偶文学,起源于桃符。是写在纸、布上或刻在竹子、木头、柱子上的对偶语句。言简意深,对仗工整,平仄协调,字数相同,结构相同,是中文语言的独特的艺术形式,是中国传统文化的瑰宝。 有历史记载的最早对联出现在三国时代。明洪武年间(1368-1399年),在江西庐陵(今江西省吉安市)地方,出土一尊特大铁十字架,上铸有三国时代孙权赤乌年号(238-250年)。在铁十字架上又铸有艺术精美的对联云:“四海庆安澜,铁柱宝光留十字;万民怀大泽,金炉香篆蔼千秋。” 随着人工智能技术普及,机器人玩对联不再是难事;本文带大家一起来揭秘AI智能春联的制作过程。 二、项目功能及运行流程 三、硬件清单 1.可达鸭玩具一个(固定手写纸卡,可用其他代替) 2.树莓派 *1 https://www.dfrobot.com.cn/goods-2617.html 3.树莓派摄像头*1 https://www.dfrobot.com.cn/goods-1023.html 4.OLED屏 *1 https://www.dfrobot.com.cn/goods-2688.html 5.碰撞传感器 (左右皆可)*1 https://www.dfrobot.com.cn/goods-638.html 硬件全家福 四、硬件连接 连接摄像头 连接屏幕 连接开关 五、程序编写 1.申请百度AI开放平台API接口 有百度账号可以直接登录,具体用法可参考网站介绍https://ai.baidu.com/?track=cp:aipinzhuan|pf:pc|pp:AIpingtai|pu:title|ci:|kw:10005792 2.代码介绍main.py 引用库 [mw_shl_code=python,false]# -*- coding: utf-8 -*- from RPi import GPIO import time import os import json import urllib.request import requests import base64 import Adafruit_SSD1306 #import subprocess from PIL import Image from PIL import ImageDraw from PIL import ImageFont[/mw_shl_code] 屏幕显示文字函数 [mw_shl_code=python,false]'''屏幕提示文字显示''' def shape(): RST = 24 # Note the following are only used with SPI: DC = 23 SPI_PORT = 0 SPI_DEVICE = 0 #128x64 display with hardware I2C: disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) disp.begin() disp.clear() disp.display() width = disp.width height = disp.height image = Image.new('1', (width, height)) draw = ImageDraw.Draw(image) draw.rectangle((0,0,width,height), outline=0, fill=0) font = ImageFont.load_default() font = ImageFont.truetype('simkai.ttf', 20) draw.text((20, 20), '智能春联', font=font, fill=255) disp.image(image) disp.display()[/mw_shl_code] 显示春联结果函数 [mw_shl_code=python,false]'''屏幕提示文字显示''' def shape(): RST = 24 # Note the following are only used with SPI: DC = 23 SPI_PORT = 0 SPI_DEVICE = 0 #128x64 display with hardware I2C: disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST) disp.begin() disp.clear() disp.display() width = disp.width height = disp.height image = Image.new('1', (width, height)) draw = ImageDraw.Draw(image) draw.rectangle((0,0,width,height), outline=0, fill=0) font = ImageFont.load_default() font = ImageFont.truetype('simkai.ttf', 20) draw.text((20, 20), '智能春联', font=font, fill=255) disp.image(image) disp.display()[/mw_shl_code] 手写文字识别函数 [mw_shl_code=python,false]''' 手写文字识别''' def get_file_content(filePath): """ 读取图片base64 """ with open(filePath, 'rb') as fp: return base64.b64encode(fp.read()) def pic_ocr(access_token,image_path): image = get_file_content(image_path) r = requests.post( url = 'https://aip.baidubce.com/rest/2.0/ocr/v1/handwriting?access_token='+access_token, headers={"Content-Type":"application/x-www-form-urlencoded"}, data = {'image':image}) j = json.loads(r.text) words_result = j.get('words_result') ls_dict = words_result ls_dict3 =ls_dict[0]['words'] return ls_dict3[/mw_shl_code] 智能春联程序函数 [mw_shl_code=python,false]''' 春联程序 ''' def get_couplets(content): #content 字符串(限5字符数以内)即春联的主题 token2=此处请用自己申请的APP ID ,API Key ,SecretKey 创建 token,具体见百度API说明 url = 'https://aip.baidubce.com/rpc/2.0/creation/v1/couplets' params = dict() params['index'] = 0 #index整数 默认为数值为0,即第一幅春联。每换一次,数值加1即可,一定数量后会返回之前的春联结果。 params['text'] = content params = json.dumps(params).encode('utf-8') access_token = token2 url = url + "?access_token=" + access_token request = urllib.request.Request(url=url, data=params) request.add_header('Content-Type', 'application/json') response = urllib.request.urlopen(request) content = response.read() if content: content=content.decode('utf-8') print (content) data = json.loads(content) print(data) data=data['couplets'] #print('横批:', data['center']) #print ('上联:',data['first']) #print ('下联:',data['second']) return data[/mw_shl_code] 主程序 [mw_shl_code=python,false]# 采用BCM引脚编号 GPIO.setmode(GPIO.BCM) # 关闭警告 GPIO.setwarnings(False) # 设置GPIO输入模式, 使用GPIO内置的下拉电阻, 即开关断开情况下输入为LOW GPIO.setup(22, GPIO.IN) shape() try: while True: if (GPIO.input(22)==GPIO.LOW): print('系统启动中') os.system("raspistill -n -t 200 -w 1024 -h 768 -o image.jpg") time.sleep(0.5) Photo1='image.jpg' access_token=此处请用自己申请的APP ID ,API Key ,SecretKey 创建 token,具体见百度API说明 text1=pic_ocr(access_token,Photo1) print(text1) text2=get_couplets(text1) a=text2['center'] b=text2['first'] c=text2['second'] print("",a,b,c) ocr_pt(a,b,c) time.sleep(60) else : print('请启动程序') shape() time.sleep(0.5) except Exception : print('erorr') GPIO.cleanup()[/mw_shl_code] 六、上传代码测试程序: |
© 2013-2024 Comsenz Inc. Powered by Discuz! X3.4 Licensed