File size: 2,065 Bytes
2af3c11
 
 
 
 
ebea9f6
2af3c11
 
 
 
1f6803b
2af3c11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7819b33
 
 
2fc8dce
741e3fe
ce64143
741e3fe
 
 
 
 
2af3c11
 
741e3fe
2af3c11
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import streamlit as st
from PIL import Image
import numpy as np
import io
import openai
from gTTs import gTTS
import tempfile
import os

# 请将你的 OpenAI API 密钥放在这里
# openai.api_key = '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'

# 功能一:拍照功能
st.title("拍照并处理图片")
picture = st.camera_input("拍照")

if picture:
    # 功能二:将拍照后的照片分辨率设置成300x300像素
    img = Image.open(picture)
    img = img.resize((300, 300))
    st.image(img, caption="Resized Image", use_column_width=True)
    
    # 将图片保存到内存中
    img_byte_arr = io.BytesIO()
    img.save(img_byte_arr, format='PNG')
    img_byte_arr = img_byte_arr.getvalue()
    
    # 功能三:上传照片并生成文字结果
    st.write("正在上传照片并生成文字结果...")
    
    # 生成文字的提示词
    prompt = "请从左到右描述图片中的物体。"
    
    # 调用 OpenAI API 生成文字描述
    response = openai.ChatCompletion.create(
        model="gpt-4",
        messages=[
            {"role": "system", "content": "你是一位图像描述专家。"},
            {"role": "user", "content": f"{prompt} 图像数据将作为背景知识提供。"},
            {"role": "user", "content": "图像数据:图像数据以字节流表示,这里无法显示具体内容。"}
        ]
    )
    
    generated_text = response.choices[0].message['content'].strip()
    st.write("生成的文字结果:")
    st.write(generated_text)
    
    # 功能四:将文字转换为语音并播放
    st.write("将文字转换为语音并播放")
    
    tts = gTTS(text=generated_text, lang='zh')
    temp_file = tempfile.NamedTemporaryFile(delete=False, suffix=".mp3")
    tts.save(temp_file.name)
    
    audio_file = open(temp_file.name, "rb")
    audio_bytes = audio_file.read()
    
    st.audio(audio_bytes, format='audio/mp3')
    
    # 删除临时文件
    os.remove(temp_file.name)