視訊理解或視訊洞察由於其多方面的優勢而在各個行業和應用中至關重要。它們透過自動產生元資料、對內容進行分類並使影片更易於搜尋來增強內容分析和管理。此外,視訊洞察提供了推動決策、增強用戶體驗並提高不同行業營運效率的關鍵數據。
Google 的 Gemini 1.5 模型為該領域帶來了重大進步。除了在語言處理方面令人印象深刻的改進之外,該模型還可以處理多達 100 萬個標記的巨大輸入上下文。為了進一步增強其功能,Gemini 1.5 被訓練為多模式模型,可以本地處理文字、圖像、音訊和視訊。各種輸入類型和廣泛的上下文大小的強大組合為有效處理長視訊開闢了新的可能性。
在本文中,我們將深入探討如何利用 Gemini 1.5 產生有價值的影片見解,改變我們跨不同領域理解和利用影片內容的方式。
入門
目錄
- 什麼是 Gemini 1.5
- 先決條件
- 安裝依賴項
- 設定 Gemini API 金鑰
- 設定環境變數
- 導入庫
- 初始化項目
- 儲存上傳的檔案
- 從影片產生見解
- 將影片上傳到檔案 API
- 取得文件
- 回應產生
- 刪除檔案
- 組合各階段
- 建立介面
- 建立 Streamlit 應用程式
什麼是雙子座1.5
Google 的 Gemini 1.5 代表了人工智慧效能和效率的重大飛躍。該模型建立在廣泛的研究和工程創新的基礎上,採用新的專家混合 (MoE) 架構,提高了培訓和服務效率。 Gemini 1.5 Pro 和 1.5 Flash 現已推出公開預覽版,透過 Google AI Studio 和 Vertex AI 提供了令人印象深刻的 100 萬個代幣上下文視窗。
Google Gemini 更新:Flash 1.5、Gemma 2 與 Project Astra (blog.google)
1.5 Flash 型號是 Gemini 系列的最新成員,對於大容量、高頻任務來說速度最快且最佳化。它專為實現成本效益而設計,在摘要、聊天、圖像和視訊字幕以及從大量文件和表格中提取資料等應用程式中表現出色。憑藉這些進步,Gemini 1.5 為 AI 車型的性能和多功能性樹立了新標準。
先決條件
- Python 3.9 (https://www.python.org/downloads)
- Google生成ai
- 流線型
安裝依賴項
- 透過執行以下命令來建立並啟動虛擬環境。
python -m venv venv source venv/bin/activate #for ubuntu venv/Scripts/activate #for windows
- 使用 pip 安裝 google-generativeai、streamlit、python-dotenv 函式庫。請注意,generativeai 需要 python 3.9 版本才能運作。
pip install google-generativeai streamlit python-dotenv
設定 Gemini API 金鑰
要存取 Gemini API 並開始使用其功能,您可以透過註冊 Google AI Studio 來取得免費的 Google API 金鑰。 Google AI Studio 由 Google 提供,提供了一個用戶友好的、基於視覺的介面,用於與 Gemini API 進行互動。在 Google AI Studio 中,您可以透過其直覺的 UI 無縫地與生成模型交互,如果需要,還可以產生 API 令牌以增強控制和自訂。
依照下列步驟產生 Gemini API 金鑰:
- 要啟動此過程,您可以點擊連結 (https://aistudio.google.com/app) 以重新導向至 Google AI Studio,或在 Google 上執行快速搜尋以找到它。
- 接受服務條款並按一下繼續。
- 點擊側邊欄的取得 API 金鑰連結和在新專案中建立 API 金鑰按鈕來產生金鑰。
- 複製產生的 API 金鑰。
設定環境變數
首先為您的專案建立一個新資料夾。選擇一個能夠反映您專案目的的名稱。
在新專案資料夾中,建立一個名為 .env 的檔案。該文件將儲存您的環境變量,包括您的 Gemini API 金鑰。
開啟 .env 檔案並新增以下程式碼來指定您的 Gemini API 金鑰:
GOOGLE_API_KEY=AIzaSy......
導入庫
要開始您的專案並確保您擁有所有必要的工具,您需要匯入幾個關鍵庫,如下所示。
import os import time import google.generativeai as genai import streamlit as st from dotenv import load_dotenv
- google.generativeai as genai:導入 Google Generative AI 庫以與 Gemini API 互動。
- Streamlit as st:導入 Streamlit 用於建立 Web 應用程式。
- from dotenv import load_dotenv:從 .env 檔案載入環境變數。
初始化專案
要設定您的項目,您需要設定 API 金鑰並為上傳的檔案建立臨時檔案儲存目錄。
透過初始化必要的設定來定義媒體資料夾並配置 Gemini API 金鑰。將以下程式碼加入您的腳本:
python -m venv venv source venv/bin/activate #for ubuntu venv/Scripts/activate #for windows
儲存上傳的文件
要將上傳的檔案儲存在媒體資料夾中並返回其路徑,請定義一個名為 save_uploaded_file 的方法並在其中新增以下程式碼。
pip install google-generativeai streamlit python-dotenv
從影片中產生見解
從影片中產生見解涉及幾個關鍵階段,包括上傳、處理和產生回應。
1. 將影片上傳到Files API
Gemini API 直接接受影片檔案格式。文件 API 支援最大 2GB 的文件,並允許每個專案最大儲存 20GB。上傳的檔案保留 2 天,無法從 API 下載。
GOOGLE_API_KEY=AIzaSy......
2. 取得文件
上傳檔案後,您可以使用files.get方法驗證API是否已成功接收檔案。此方法可讓您查看上傳到文件 API 的文件,這些文件與連結到您的 API 金鑰的雲端項目關聯。只有檔案名稱和 URI 是唯一識別碼。
import os import time import google.generativeai as genai import streamlit as st from dotenv import load_dotenv
3. 響應生成
影片上傳後,您可以發出引用檔案 API URI 的GenerateContent 請求。
MEDIA_FOLDER = 'medias' def __init__(): # Create the media directory if it doesn't exist if not os.path.exists(MEDIA_FOLDER): os.makedirs(MEDIA_FOLDER) # Load environment variables from the .env file load_dotenv() # Retrieve the API key from the environment variables api_key = os.getenv("GEMINI_API_KEY") # Configure the Gemini API with your API key genai.configure(api_key=api_key)
4. 刪除文件
檔案會在 2 天後自動刪除,或者您可以使用 files.delete() 手動刪除它們。
def save_uploaded_file(uploaded_file): """Save the uploaded file to the media folder and return the file path.""" file_path = os.path.join(MEDIA_FOLDER, uploaded_file.name) with open(file_path, 'wb') as f: f.write(uploaded_file.read()) return file_path
5. 結合各個階段
建立一個名為 get_insights 的方法並在其中加入以下程式碼。使用 Streamlit write() 方法取代 print() 來查看網站上的消息。
video_file = genai.upload_file(path=video_path)
建立介面
要簡化在 Streamlit 應用程式中上傳影片和產生見解的過程,您可以建立一個名為 app 的方法。此方法將提供一個上傳按鈕,顯示上傳的視頻,並從中產生見解。
import time while video_file.state.name == "PROCESSING": print('Waiting for video to be processed.') time.sleep(10) video_file = genai.get_file(video_file.name) if video_file.state.name == "FAILED": raise ValueError(video_file.state.name)
創建 Streamlit 應用程式
要建立一個完整且功能齊全的 Streamlit 應用程序,允許使用者使用 Gemini 1.5 Flash 模型上傳影片並產生見解,請將所有元件組合到一個名為 app.py 的檔案中。
這是最終程式碼:
# Create the prompt. prompt = "Describe the video. Provides the insights from the video." # Set the model to Gemini 1.5 Flash. model = genai.GenerativeModel(model_name="models/gemini-1.5-flash") # Make the LLM request. print("Making LLM inference request...") response = model.generate_content([prompt, video_file], request_options={"timeout": 600}) print(response.text)
運行應用程式
執行以下程式碼來執行應用程式。
genai.delete_file(video_file.name)
您可以開啟控制台中提供的連結來查看輸出。
感謝您閱讀這篇文章! !
如果您喜歡這篇文章,請點擊心形按鈕♥並分享以幫助其他人找到它!
本教學的完整原始碼可以在這裡找到,
GitHub - codemaker2015/video-insights-generator
以上是使用 Gemini Flash 建構視訊洞察生成器的詳細內容。更多資訊請關注PHP中文網其他相關文章!

toAppendElementStoApythonList,usetheappend()方法forsingleements,Extend()formultiplelements,andinsert()forspecificpositions.1)useeAppend()foraddingoneOnelementAttheend.2)useextendTheEnd.2)useextendexendExendEnd(

TocreateaPythonlist,usesquarebrackets[]andseparateitemswithcommas.1)Listsaredynamicandcanholdmixeddatatypes.2)Useappend(),remove(),andslicingformanipulation.3)Listcomprehensionsareefficientforcreatinglists.4)Becautiouswithlistreferences;usecopy()orsl

金融、科研、医疗和AI等领域中,高效存储和处理数值数据至关重要。1)在金融中,使用内存映射文件和NumPy库可显著提升数据处理速度。2)科研领域,HDF5文件优化数据存储和检索。3)医疗中,数据库优化技术如索引和分区提高数据查询性能。4)AI中,数据分片和分布式训练加速模型训练。通过选择适当的工具和技术,并权衡存储与处理速度之间的trade-off,可以显著提升系统性能和可扩展性。

pythonarraysarecreatedusiseThearrayModule,notbuilt-Inlikelists.1)importThearrayModule.2)指定tefifythetypecode,例如,'i'forineizewithvalues.arreaysofferbettermemoremorefferbettermemoryfforhomogeNogeNogeNogeNogeNogeNogeNATATABUTESFELLESSFRESSIFERSTEMIFICETISTHANANLISTS。

除了shebang線,還有多種方法可以指定Python解釋器:1.直接使用命令行中的python命令;2.使用批處理文件或shell腳本;3.使用構建工具如Make或CMake;4.使用任務運行器如Invoke。每個方法都有其優缺點,選擇適合項目需求的方法很重要。

ForhandlinglargedatasetsinPython,useNumPyarraysforbetterperformance.1)NumPyarraysarememory-efficientandfasterfornumericaloperations.2)Avoidunnecessarytypeconversions.3)Leveragevectorizationforreducedtimecomplexity.4)Managememoryusagewithefficientdata

Inpython,ListSusedynamicMemoryAllocationWithOver-Asalose,而alenumpyArraySallaySallocateFixedMemory.1)listssallocatemoremoremoremorythanneededinentientary上,respizeTized.2)numpyarsallaysallaysallocateAllocateAllocateAlcocateExactMemoryForements,OfferingPrediCtableSageButlessemageButlesseflextlessibility。

Inpython,YouCansspecthedatatAtatatPeyFelemereModeRernSpant.1)Usenpynernrump.1)Usenpynyp.dloatp.dloatp.ploatm64,formor professisconsiscontrolatatypes。


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

WebStorm Mac版
好用的JavaScript開發工具

SublimeText3漢化版
中文版,非常好用

Dreamweaver CS6
視覺化網頁開發工具

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。

MantisBT
Mantis是一個易於部署的基於Web的缺陷追蹤工具,用於幫助產品缺陷追蹤。它需要PHP、MySQL和一個Web伺服器。請查看我們的演示和託管服務。