可达鸭助力智能春联
https://v.youku.com/v_show/id_XNDc4OTc3MDMzMg==.html一、项目介绍:
对联又称对偶、门对、春贴、春联、桃符等,是一种对偶文学,起源于桃符。是写在纸、布上或刻在竹子、木头、柱子上的对偶语句。言简意深,对仗工整,平仄协调,字数相同,结构相同,是中文语言的独特的艺术形式,是中国传统文化的瑰宝。
有历史记载的最早对联出现在三国时代。明洪武年间(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
硬件全家福
https://mc.dfrobot.com.cn/data/attachment/album/202008/10/192338lzg1kad69ggz0cql.jpg
四、硬件连接
连接摄像头
https://mc.dfrobot.com.cn/data/attachment/album/202007/25/165556o0qf4gee9k9z4ce9.jpg
连接屏幕
https://mc.dfrobot.com.cn/data/attachment/album/202007/25/165559laveavdauhssy8uj.jpg
连接开关
https://mc.dfrobot.com.cn/data/attachment/album/202007/25/165614agxji7b0w7wj1d7w.jpg
五、程序编写
1.申请百度AI开放平台API接口
有百度账号可以直接登录,具体用法可参考网站介绍https://ai.baidu.com/?track=cp:aipinzhuan|pf:pc|pp:AIpingtai|pu:title|ci:|kw:10005792https://mc.dfrobot.com.cn/data/attachment/album/202008/10/195808rdkcdjp92ppjjcrx.png
https://mc.dfrobot.com.cn/data/attachment/album/202008/10/200417vsljn36l4isggtji.png
https://mc.dfrobot.com.cn/data/attachment/album/202008/10/200417fr9ry832drz0d3pz.png
https://mc.dfrobot.com.cn/data/attachment/album/202008/10/200417vvqdlfldcdr8flqv.png
https://mc.dfrobot.com.cn/data/attachment/album/202008/10/200417t0id069zy55h90hx.png
2.代码介绍main.py
引用库
# -*- 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
屏幕显示文字函数
'''屏幕提示文字显示'''
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()
显示春联结果函数
'''屏幕提示文字显示'''
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()
手写文字识别函数
''' 手写文字识别'''
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['words']
return ls_dict3
智能春联程序函数
'''
春联程序
'''
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
主程序
# 采用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()
六、上传代码测试程序:
https://v.youku.com/v_show/id_XNDc4OTcxOTg1Ng==.html
很有趣啊 树莓派人工的新方向,漂亮 gray6666 发表于 2020-8-10 20:40
一、项目介绍:
对联又称对偶、门对、春贴、春联、桃符等,是一种对偶文学,起源于桃符 ...
人工智能三大新宠:树莓派,Linux,Python interesting
页:
[1]