手把手教你在本地Ollama上部署Qwen3 VL,实现图片理解与OCR
Qwen3 VL能够将静态可视化数据转化为动态智能,帮助企业在医疗、零售、制造和金融等行业提升运营效率。本文将详细介绍如何在本地PC上通过Ollama部署Qwen3 VL模型,并通过具体案例展示其在图片描述、OCR文字识别及图片对比方面的应用。即使是2B版本的小型模型,也能满足多种用例需求。
01环境准备与模型下载
在开始部署前,需先根据操作系统安装Ollama运行环境,并下载相应的模型文件。以下是针对不同操作系统的安装命令及下载方式。
- Linux系统:执行命令
curl -fsSL https://ollama.com/install.sh | sh - Windows系统:下载并运行
OllamaSetup.exe - macOS系统:下载并解压
Ollama-darwin.zip - Docker环境:拉取镜像
ollama/ollama
完成Ollama安装后,使用以下命令下载Qwen3 VL模型。用户可根据硬件配置选择2B或8B版本。
- 下载2B模型:
ollama run qwen3-vl:2b - 下载8B模型:
ollama run qwen3-vl:8b
此外,还需配置Python环境(建议使用Python 3.12),并创建Conda环境或虚拟环境,安装必要的依赖库:
- 安装Streamlit:
pip install streamlit - 安装Ollama Python库:
pip install ollama
02案例一:图片描述
本案例演示如何使用Streamlit构建简单的Web应用,上传图片并利用Qwen3 VL模型生成详细描述。代码实现了图片上传、Base64编码以及模型调用功能。
import streamlit as st
import base64
from ollama import Client
# Initialize Local Ollama Client
client = Client(host='http://localhost:11434')
st.title("Image Description with Qwen")
st.write("Upload an image and describe it using the Qwen model")
# Image Uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
# Optional Prompt Input
prompt = st.text_input("Enter custom prompt (optional):",
value="Describe this image in detail")
# Describe Button
if st.button("Describe Image"):
if uploaded_file is not None:
encoded_image = base64.b64encode(uploaded_file.getvalue()).decode('utf-8')
# Prepare message for Ollama
messages = [{
'role': 'user',
'content': prompt,
'images': [encoded_image]
}]
# Get response from Qwen3 VL model
try:
response = client.chat(model='qwen3-vl:2b', messages=messages)
st.subheader("Description:")
st.write(response['message']['content'])
except Exception as e:
st.error(f"Error: {str(e)}")
else:
st.warning("Please upload an image first")
# Display the Uploaded Image
if uploaded_file is not None:
st.subheader("Uploaded Image")
st.image(uploaded_file, use_column_width=True)
import base64
from ollama import Client
# Initialize Local Ollama Client
client = Client(host='http://localhost:11434')
st.title("Image Description with Qwen")
st.write("Upload an image and describe it using the Qwen model")
# Image Uploader
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
# Optional Prompt Input
prompt = st.text_input("Enter custom prompt (optional):",
value="Describe this image in detail")
# Describe Button
if st.button("Describe Image"):
if uploaded_file is not None:
encoded_image = base64.b64encode(uploaded_file.getvalue()).decode('utf-8')
# Prepare message for Ollama
messages = [{
'role': 'user',
'content': prompt,
'images': [encoded_image]
}]
# Get response from Qwen3 VL model
try:
response = client.chat(model='qwen3-vl:2b', messages=messages)
st.subheader("Description:")
st.write(response['message']['content'])
except Exception as e:
st.error(f"Error: {str(e)}")
else:
st.warning("Please upload an image first")
# Display the Uploaded Image
if uploaded_file is not None:
st.subheader("Uploaded Image")
st.image(uploaded_file, use_column_width=True)
运行结果:模型能够成功识别图片中的对象(如描述为“三个戴着帽子和衬衫的孩子站在一起”),但部分用户反馈在特定场景下描述可能不够稳定。
03案例二:OCR文字识别
利用与图片描述相同的代码结构,只需修改提示词(Prompt),即可将Qwen3 VL转换为OCR工具,用于提取图片中的文字信息。
实际测试表明,该模型在常规文档和场景文字识别中表现良好,未出现明显的数据质量问题,能够准确转写图片中的文本内容。
04案例三:图片对比
该案例展示了如何同时上传两张图片,并要求模型分析它们的相似性或差异性。此功能适用于寻找图片细节变化或场景对比。
import streamlit as st
import base64
from ollama import Client
# Initialize Ollama Client
ollama_client = Client(host='http://localhost:11434')
def encode_image(image):
return base64.b64encode(image.getvalue()).decode('utf-8')
def get_model_response(prompt, image1_data, image2_data):
try:
response = ollama_client.generate(
model='qwen3-vl:2b',
prompt=prompt,
images=[image1_data, image2_data]
)
return response['response']
except Exception as e:
return f"Error: {str(e)}"
st.set_page_config(page_title="Image Comparison with Qwen3 VL", layout="wide")
st.title("🔍 Image Comparison using Ollama Qwen")
st.subheader("Upload two images and analyze their differences/similarities")
with st.sidebar:
st.header("Prompt Options")
st.markdown("Enter a prompt to compare images:")
default_prompts = [
"Compare these images. What are the main differences?",
"How are these images similar? Point out key similarities.",
"Describe what's different between these two images.",
"Are these images depicting the same scene? Explain."
]
selected_prompt = st.selectbox("Choose a default prompt:", default_prompts)
user_prompt = st.text_area(
"Or enter your own prompt:",
value="",
height=100
)
col1, col2 = st.columns(2)
with col1:
image1 = st.file_uploader("Upload First Image", type=["jpg", "jpeg", "png"], key="img1")
if image1:
st.image(image1, caption="First Image", use_column_width=True)
image1_encoded = encode_image(image1)
with col2:
image2 = st.file_uploader("Upload Second Image", type=["jpg", "jpeg", "png"], key="img2")
if image2:
st.image(image2, caption="Second Image", use_column_width=True)
image2_encoded = encode_image(image2)
st.divider()
st.header("Comparison Results")
if 'image1_encoded' in locals() and 'image2_encoded' in locals():
prompt = user_prompt if user_prompt else selected_prompt
with st.spinner("Analyzing images with Qwen..."):
response = get_model_response(prompt, image1_encoded, image2_encoded)
st.subheader("Analysis Results")
st.write(response)
st.markdown(f"**Prompt used:** {prompt}")
else:
st.info("Please upload both images to start comparison")
st.divider()
st.caption("Powered by Local Ollama Qwen3 VL Model")
import base64
from ollama import Client
# Initialize Ollama Client
ollama_client = Client(host='http://localhost:11434')
def encode_image(image):
return base64.b64encode(image.getvalue()).decode('utf-8')
def get_model_response(prompt, image1_data, image2_data):
try:
response = ollama_client.generate(
model='qwen3-vl:2b',
prompt=prompt,
images=[image1_data, image2_data]
)
return response['response']
except Exception as e:
return f"Error: {str(e)}"
st.set_page_config(page_title="Image Comparison with Qwen3 VL", layout="wide")
st.title("🔍 Image Comparison using Ollama Qwen")
st.subheader("Upload two images and analyze their differences/similarities")
with st.sidebar:
st.header("Prompt Options")
st.markdown("Enter a prompt to compare images:")
default_prompts = [
"Compare these images. What are the main differences?",
"How are these images similar? Point out key similarities.",
"Describe what's different between these two images.",
"Are these images depicting the same scene? Explain."
]
selected_prompt = st.selectbox("Choose a default prompt:", default_prompts)
user_prompt = st.text_area(
"Or enter your own prompt:",
value="",
height=100
)
col1, col2 = st.columns(2)
with col1:
image1 = st.file_uploader("Upload First Image", type=["jpg", "jpeg", "png"], key="img1")
if image1:
st.image(image1, caption="First Image", use_column_width=True)
image1_encoded = encode_image(image1)
with col2:
image2 = st.file_uploader("Upload Second Image", type=["jpg", "jpeg", "png"], key="img2")
if image2:
st.image(image2, caption="Second Image", use_column_width=True)
image2_encoded = encode_image(image2)
st.divider()
st.header("Comparison Results")
if 'image1_encoded' in locals() and 'image2_encoded' in locals():
prompt = user_prompt if user_prompt else selected_prompt
with st.spinner("Analyzing images with Qwen..."):
response = get_model_response(prompt, image1_encoded, image2_encoded)
st.subheader("Analysis Results")
st.write(response)
st.markdown(f"**Prompt used:** {prompt}")
else:
st.info("Please upload both images to start comparison")
st.divider()
st.caption("Powered by Local Ollama Qwen3 VL Model")
运行结果:模型能够正常生成对比分析,但需注意,这属于语义层面的对比,而非像素级的精确比对,因此在准确性上可能存在一定偏差,主要提供宏观的差异概述。
当前文章无评论,是时候发表评论了