这些小活动你都参加了吗?快来围观一下吧!>>
电子产品世界 » 论坛首页 » 活动中心 » 板卡试用 » 【PocketBeagle2】⑦部署百度智能云进行机器翻译

共1条 1/1 1 跳转至

【PocketBeagle2】⑦部署百度智能云进行机器翻译

菜鸟
2025-10-16 16:42:33     打赏


在前面的几个帖子中,我们已经完成了PocketBeagle2的外网连接、python安装、大模型初试。

本篇我们部署下百度智能云,以进行基于云端的人工智能应用

比如:语音识别、文本识别、人脸识别、自然语言处理(机器翻译)等。


https://cloud.baidu.com/products/index.html

目前大部分应用都可以免费试用(有限额)

本例开发基于百度智能云的机器翻译。

baidu.PNG


在百度智能云平台注册账户及产品:

这里就不展开细说了。


image.png


image.png



我们需要得到API_KEY和SECRET_KEY 

API_KEY = "XXXXXX"

SECRET_KEY = "XXXXXX"


回到PocketBeagle2

我们在电脑端   此电脑\BeagleBoard.org\Boot Partition

新建一个目录(命名为python),将python程序丢进去


image.png



然后在SSH客户端进去/boot/firmware/python/

就对应上述目录

image.png


p4.py

我们在程序中给定一段中文:“记者15日从清华大学获悉,该校电子工程系方璐教授团队在智能光子领域取得重大突破……”

让程序连接百度智能云进行机器翻译,并将结果打印出来。

import requests
import json

API_KEY = "XXXXXX" 
SECRET_KEY = "XXXXXX"

def main():
        
    url = "https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + get_access_token()
    
    payload = json.dumps({
        "from": "zh",
        "to": "en",
        "q": "记者15日从清华大学获悉,该校电子工程系方璐教授团队在智能光子领域取得重大突破,成功研制出全球首款亚埃米级快照光谱成像芯片“玉衡”,标志着我国智能光子技术在高精度成像测量领域迈上新台阶。相关研究成果在线发表于学术期刊《自然》。科研团队基于智能光子原理,创新提出可重构计算光学成像架构,将传统物理分光限制转化为光子调制与计算重建过程。通过挖掘随机干涉掩膜与铌酸锂材料的电光重构特性,团队实现高维光谱调制与高通量解调的协同计算,最终研制出“玉衡”芯片。"
    }, ensure_ascii=False)
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload.encode("utf-8"))
    
    print(response.json()['result']['trans_result'][0]['src'])
    print(response.json()['result']['trans_result'][0]['dst'])
    

def get_access_token():
    """
    使用 AK,SK 生成鉴权签名(Access Token)
    :return: access_token,或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))

if __name__ == '__main__':
    main()


运行结果如下:

image.png

记者15日从清华大学获悉,该校电子工程系方璐教授团队在智能光子领域取得重大突破,成功研制出全球首款亚埃米级快照光谱成像芯片“玉衡”,标志着我国智能光子技术在高精度成像测量领域迈上新台阶。相关研究成果在线发表于学术期刊《自然》。科研团队基于智能光子原理,创新提出可重构计算光学成像架构,将传统物理分光限制转化为光子调制与计算重建过程。通过挖掘随机干涉掩膜与铌酸锂材料的电光重构特性,团队实现高维光谱调制与高通量解调的协同计算,最终研制出“玉衡”芯片。

On the 15th, it was learned from Tsinghua University that Professor Fang Lu's team in the Department of Electronic Engineering has made significant breakthroughs in the field of intelligent photonics, successfully developing the world's first sub Emmy level snapshot spectral imaging chip "Yuheng", marking a new step for China's intelligent photonics technology in the field of high-precision imaging measurement. The relevant research results were published online in the academic journal Nature. The research team has innovatively proposed a reconfigurable computational optical imaging architecture based on the principle of intelligent photons, which transforms traditional physical spectral confinement into photon modulation and computational reconstruction processes. By exploring the electro-optic reconstruction characteristics of random interference masks and lithium niobate materials, the team achieved collaborative computation of high-dimensional spectral modulation and high-throughput demodulation, ultimately developing the "Yuheng" chip.




p5.py

这个程序,我们不预设待翻译的内容,需要手动输入中文,然后进行翻译


import requests
import json

API_KEY = "XXXXXX"
SECRET_KEY = "XXXXXX"

def main():

    string = input("请输入需要翻译的原文(中文):")
        
    url = "https://aip.baidubce.com/rpc/2.0/mt/texttrans/v1?access_token=" + get_access_token()
    
    payload = json.dumps({
        "from": "zh",
        "to": "en",
        "q": string
    }, ensure_ascii=False)
    headers = {
        'Content-Type': 'application/json',
        'Accept': 'application/json'
    }
    
    response = requests.request("POST", url, headers=headers, data=payload.encode("utf-8"))
    
    #print(response.json()['result']['trans_result'][0]['src'])
    print(response.json()['result']['trans_result'][0]['dst'])
    

def get_access_token():
    """
    使用 AK,SK 生成鉴权签名(Access Token)
    :return: access_token,或是None(如果错误)
    """
    url = "https://aip.baidubce.com/oauth/2.0/token"
    params = {"grant_type": "client_credentials", "client_id": API_KEY, "client_secret": SECRET_KEY}
    return str(requests.post(url, params=params).json().get("access_token"))

if __name__ == '__main__':
    main()


运行结果如下:

image.png

来个难度更大的:

image.png


岗日嘎布山脉: 温柔的雪山,优美的崇高。究竟是一种怎样的美一下子触动了我。虽然就熟悉程度而言,相对于中国西部其他雪山,我对岗日嘎布雪山(应该称之为山脉)算是熟悉的了。我曾两次去过来古冰川和来古村;两次去过米堆村和米堆冰川;两次从波密出发翻越嘎隆拉岭,一直驾车到达墨脱;两次去过下察隅,接近过阿扎冰川;还深入过波密附近的岗乡云杉林,接着去墨脱,去寻找过中国最高的树……有时只是遥望,驾车经318国道从成都去西藏,翻过安久拉山垭口,一到然乌湖,公路之南隔着帕隆藏布,对面绵延的山脉就是岗日嘎布。说这些,丝毫没有“凡尔赛”的意思,而是想获取一种权利:谈论岗日嘎布的权利。如果我没有到过这里,我觉得我就没有用现在这种角度谈论它的权利。首先让我感到困难的是:一开始我就被这条山脉的界线和范围纠缠住了,原来这个问题并不是清晰的,而是疑难很多,这个我想放到文章的后面再说,否则这篇文章就会像是一篇论文,很枯燥;二是我想概括一下这条山脉的特点,尽管我多次去过那里,但是让我一下子说出这条山脉的特点,还是有些难度。总是感觉自己已经体验到了它的魅力,感受到了它的独特,已经能够欣赏它独特的美了,但是若用语言准确地说出我的感觉来,竟找不出合适的词汇。最后我决定还是先尝试着说出它的特点,因为这样文章会更贴近读者。

Gangri Gabu Mountain Range: Gentle snow capped mountains, beautiful and sublime. What kind of beauty suddenly touched me. Although in terms of familiarity, compared to other snow capped mountains in western China, I am relatively familiar with Gangrigab Snow Mountain (which should be called a mountain range). I have visited Gu Glacier and Gu Village twice; Twice visited Midui Village and Midui Glacier; Starting from Bomi twice, crossing the Galongla Mountains and driving all the way to Motuo; Twice visited Xiachayu and approached Aza Glacier; I also went deep into the spruce forest in Gangxiang near Bomi, and then went to Motuo to look for the highest tree in China... Sometimes I just looked from afar, driving through National Highway 318 from Chengdu to Xizang, crossing the Anjiula Pass, and arriving at Ranwu Lake. To the south of the road is Palong Zangbu, and the opposite mountain is Gangri Gabu. Saying these things does not mean 'Versailles' at all, but rather seeks to gain a right: to discuss the rights of Gangrigab. If I hadn't been here before, I feel like I wouldn't have the right to discuss it from the current perspective. The first thing that makes me feel difficult is: from the beginning, I was entangled by the boundaries and scope of this mountain range. It turns out that this problem is not clear, but much more difficult. I want to put this at the end of the article, otherwise it will be like a paper, very boring; Secondly, I would like to summarize the characteristics of this mountain range. Although I have been there many times, it is still difficult for me to describe the features of this mountain range at once. I always feel like I have experienced its charm, felt its uniqueness, and can already appreciate its unique beauty, but if I were to express my feelings accurately in language, I couldn't find the appropriate vocabulary. In the end, I decided to try to describe its characteristics first, as this would make the article more relatable to the readers.



翻译效果如何,大家各自评判吧。









关键词: PocketBeagle2    

共1条 1/1 1 跳转至

回复

匿名不能发帖!请先 [ 登陆 注册 ]