PHP 使用微軟Azure AI 認知服務電腦視覺做處方箋辨識

最近接到了一個任務,是透過影像辨識的模式去取得處方箋內容。然後抓到病患的一些個資以及病名、藥名等資料。於是就想到用azure 的ai服務裡有電腦視覺可以做ocr,然後再透過open ai去抓出內容試試
簡單的寫了段程式,主要是把上傳到images裡的處方箋資料,透過azure vision 去做辨識,接著把辨識出的文字存到 檔名-result.txt裡面

https://portal.vision.cognitive.azure.com/demo/extract-text-from-images

https://azure.microsoft.com/zh-tw/products/cognitive-services/vision-services/

<?php
// 透過azure vision ai 取得images下的資料,然後將ocr結果另存新檔
$base_url = ‘https://eastasia.api.cognitive.microsoft.com/computervision/imageanalysis:analyze?features=caption%2Cread&model-version=latest&language=en&api-version=2023-02-01-preview’;

$api_key = ‘yourkey’;

$directory = ‘./images’; // 修改成目錄的路徑

$files = scandir($directory);

foreach ($files as $file) {
$extension = strtolower(pathinfo($file, PATHINFO_EXTENSION));
if (in_array($extension, [‘jpg’, ‘jpeg’])) {
$file_url = ‘https://yoursite/images/’ . $file;

$curl = curl_init();

curl_setopt_array($curl, array(
CURLOPT_URL => $base_url,
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => ”,
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => ‘POST’,
CURLOPT_POSTFIELDS => ‘{“url”:”‘ . $file_url . ‘”}’,
CURLOPT_HTTPHEADER => array(
‘Ocp-Apim-Subscription-Key: ‘ . $api_key,
‘Content-Type: application/json’
),
));

$response = curl_exec($curl);

curl_close($curl);
echo $response;
$file_name = pathinfo($file, PATHINFO_FILENAME) . ‘.txt’;
file_put_contents($file_name, $response);
$json_response = json_decode($response, true);

// Check if the response contains “readResult” and “content” fields
if (isset($json_response[‘readResult’]) && isset($json_response[‘readResult’][‘content’])) {
$content = $json_response[‘readResult’][‘content’];
$file_name = pathinfo($file, PATHINFO_FILENAME) . ‘-result.txt’;

// Save the content to a file
file_put_contents($file_name, $content);
}
}
}
?>

接著再透過open ai的服務,去把相關資料抓出來,再透過您是一位專業的藥師,擁有中西醫藥品的知識,請幫我從以下的文字找出醫事機構代碼、門診類別、姓名、年齡(如無訊息可以由就診日期的年減掉出生日期的年)、出生日期、就診日期、性別(如無訊息可以從身份證字號第二碼數字,數字1為男性,數字2為女性)、天數、用法、藥品健保碼並以條列的方式回應:,然後抓到我自己想要的資料。

 

<?php
// 設定 OpenAI API 金鑰
$api_key = ‘yourkey’;

// 獲取現有目錄下檔名符合 “-result.txt” 的檔案
$directory = ‘./’; // 修改成目錄的路徑
$files = scandir($directory);

foreach ($files as $file) {
if (strpos($file, ‘-result.txt’) !== false) {
echo $file.”\n\r”;
// 讀取檔案內容
$content = file_get_contents($file);
echo $content.”\n\r”;;
echo ‘您是一位專業的藥師,擁有中西醫藥品的知識,請幫我從以下的文字找出醫事機構代碼、門診類別、姓名、年齡(如無訊息可以由就診日期的年減掉出生日期的年)、出生日期、就診日期、性別(如無訊息可以從身份證字號第二碼數字,數字1為男性,數字2為女性)、天數、用法、藥品健保碼並以條列的方式回應:’.$conten.”\n\r”;;
// 設定 API 請求的 URL 和 headers
$url = ‘https://api.openai.com/v1/engines/text-davinci-002/completions’;
$headers = array(
‘Authorization: Bearer ‘ . $api_key,
‘Content-Type: application/json’,
);

// 設定 API 請求的 payload,這裡使用 GPT-3.5 模型
$data = array(
‘prompt’ => ‘您是一位專業的藥師,擁有中西醫藥品的知識,請幫我從以下的文字找出醫事機構代碼、門診類別、姓名、年齡(如無訊息可以由就診日期的年減掉出生日期的年)、出生日期、就診日期、性別(如無訊息可以從身份證字號第二碼數字,數字1為男性,數字2為女性)、天數、用法、藥品健保碼並以條列的方式回應:’.$content,
‘temperature’=> 0.7,
‘max_tokens’ => 2000,
);
$payload = json_encode($data);

// 初始化 curl
$ch = curl_init();

// 設定 curl 選項
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $payload);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);

// 執行 curl 並取得回傳結果
$response = curl_exec($ch);
echo $response.”\n\r”;;
// 關閉 curl
curl_close($ch);

// 處理 API 回傳的結果,你可以根據需要進行處理
$openai_result = json_decode($response, true);

// 將 API 回傳的結果存成新的檔案
$new_file_name = str_replace(‘-result.txt’, ‘-gpt.txt’, $file);
file_put_contents($new_file_name, $openai_result[‘choices’][0][‘text’]);
}
}
?>

 

運用chatGPT結合canva協助社群經營生成字卡

生成式AI chatGPT可以協助生成許多不錯的文案

再結合免費且強大可以製作出漂亮美觀的字卡的canva

就能夠讓社群經營每天有源源不絕的內容了

首先,在chatGPT上詢問你想要他幫你產生的20條內容

接著請chatGPT以csv的格式產出,然後複製下來到記事本另存為csv檔案

然後來到canva,找到您要的畫面後,按左邊的大量建立

接著會問要輸入或匯入CSV,我們選匯入csv

在要大量生成的文字框按右鍵,連接資料選擇要塞入的資料

最後再選擇其他19則也是如此,就可以完成了!

人工智慧相關資料整理(長期)

微軟 Azure OpenAI Service Partner Workshop 筆記 – AI 時代的 iPhone 時刻 | 活動

非常厲害的語音轉文字(語音識別, STT)服務

非常擬真的文字轉聲音服務(TTS)

語音服務的語言和語音支援

Line + AI 機器人自動繪圖還能P圖外加智能助理功能

分享 Call Language Understanding (LUIS) predictions api 的 3 種方式

線上客服 + Chatbot = 智慧客服

建立Microsoft LUIS的App服務,進行語意識別的訓練並整合Bot Framework

LUIS 自然語言語意分析服務實戰

C# 微軟語音辨識/語音合成結合OpenAI ChatGPT 變成ai人工智慧管家

透過微軟azure上的語音服務,辨識麥克風傳入的聲音後將語音轉成文字
接著再把文字送到open ai api中的chatgpt 模型中取得回應內容
最後再將回應內容透過語音合成送出

參考:
Introducing ChatGPT and Whisper APIs
https://openai.com/blog/introducing-chatgpt-and-whisper-apis
openai api
https://platform.openai.com/docs/guides/chat

Betalgo.OpenAI.GPT3
https://www.nuget.org/packages/Betalgo.OpenAI.GPT3/6.7.0
https://github.com/betalgo/openai

chatGPT與line官方帳號結合 = line ai 小助理

OpenAI設計的ChatGPT問世後,很快的ai的使用人數破億。且chatgpt讓ai整個流行了起來~ 想說台灣最流行的通訊工具line,有沒有機會跟chatgpt串到一起,於是很快的用php以及.net core都串了一次。簡單明瞭~很易懂

分享關鍵的php,有問題歡迎line: superlevin 林壽山


$api_key = 'xxxxxxxxxx';
$curl = curl_init();
curl_setopt_array($curl, [
CURLOPT_URL => 'https://api.openai.com/v1/completions',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_POST => true,
CURLOPT_HTTPHEADER => [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
],
CURLOPT_POSTFIELDS => json_encode([
'prompt' => $ask_question,
'model' => 'text-davinci-003',
'max_tokens' => 1000,
'temperature' => 0.3
])
]);
$response = curl_exec($curl);

$json = json_decode($response, true);

 

 

C# ChatGPT取回後轉語音合成 將base64轉音檔

從ChatGPT的api,取得文件後,再透過語音合成轉為base64音檔念出。

 

台灣雅婷 https://www.yating.tw/zh/api-text-to-speech-zh/

雅婷文字轉語音 https://tts.yating.tw/

文件  https://developer.yating.tw/doc/tts-%E8%AA%9E%E9%9F%B3%E5%90%88%E6%88%90#%E7%AF%84%E4%BE%8B

後台: https://developer.yating.tw/zh-TW/dashboard

其他參考

https://ai.baidu.com/tech/speech/tts

 

https://aws.amazon.com/tw/polly/

https://tw.piliapp.com/text-to-speech/

https://azure.microsoft.com/zh-tw/products/cognitive-services/text-to-speech/#overview

https://cloud.google.com/text-to-speech?hl=zh-tw

https://azure.microsoft.com/zh-tw/products/cognitive-services/text-to-speech/

https://www.xfyun.cn/services/online_tts

https://everest-ai.ximalaya.com/charm-studio/


static void DecodeBase64ToFile(string Base64String, string filenames)
{
try
{

byte[] bytes = Convert.FromBase64String(Base64String);
using (var fs = new FileStream(filenames, FileMode.Create, FileAccess.Write))
{
fs.Write(bytes, 0, bytes.Length);
fs.Flush();
}
}
catch (Exception e)
{

}
}