Update app.py
Browse files
app.py
CHANGED
|
@@ -1,70 +1,27 @@
|
|
| 1 |
import streamlit as st
|
| 2 |
-
import cv2
|
| 3 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 4 |
from PIL import Image
|
| 5 |
-
import numpy as np
|
| 6 |
|
| 7 |
# 加载BLIP模型和处理器
|
| 8 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 9 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 10 |
|
| 11 |
-
def capture_image():
|
| 12 |
-
# 打开摄像头
|
| 13 |
-
cap = cv2.VideoCapture(0)
|
| 14 |
-
|
| 15 |
-
st.info("按下空格键拍照,按下'q'键退出摄像头")
|
| 16 |
-
|
| 17 |
-
img_name = None # 初始化 img_name
|
| 18 |
-
|
| 19 |
-
while True:
|
| 20 |
-
# 读取摄像头帧
|
| 21 |
-
ret, frame = cap.read()
|
| 22 |
-
if not ret:
|
| 23 |
-
st.error("无法从摄像头读取帧")
|
| 24 |
-
break
|
| 25 |
-
|
| 26 |
-
# 显示摄像头的内容
|
| 27 |
-
cv2.imshow('摄像头', frame)
|
| 28 |
-
|
| 29 |
-
# 等待键盘输入
|
| 30 |
-
key = cv2.waitKey(1)
|
| 31 |
-
if key % 256 == 32: # 空格键拍照
|
| 32 |
-
img_name = "captured_image.png"
|
| 33 |
-
cv2.imwrite(img_name, frame)
|
| 34 |
-
st.success(f"照片已保存为 {img_name}")
|
| 35 |
-
break
|
| 36 |
-
elif key % 256 == ord('q'): # 按下 'q' 键退出
|
| 37 |
-
st.info("退出摄像头")
|
| 38 |
-
break
|
| 39 |
-
|
| 40 |
-
# 释放摄像头并关闭窗口
|
| 41 |
-
cap.release()
|
| 42 |
-
cv2.destroyAllWindows()
|
| 43 |
-
|
| 44 |
-
if img_name:
|
| 45 |
-
return img_name
|
| 46 |
-
else:
|
| 47 |
-
return None # 如果没有拍照,返回 None
|
| 48 |
-
|
| 49 |
-
|
| 50 |
-
def generate_caption(image_path):
|
| 51 |
-
image = Image.open(image_path).convert('RGB')
|
| 52 |
-
inputs = processor(image, return_tensors="pt")
|
| 53 |
-
out = model.generate(**inputs)
|
| 54 |
-
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 55 |
-
return caption
|
| 56 |
-
|
| 57 |
st.title("图像描述生成器")
|
| 58 |
st.write("使用摄像头拍照并生成图像的描述。")
|
| 59 |
|
| 60 |
-
#
|
| 61 |
-
|
| 62 |
-
image_path = capture_image()
|
| 63 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
# 显示拍摄的图像
|
| 65 |
-
|
| 66 |
-
st.image(image_path, caption="拍摄的图像")
|
| 67 |
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import streamlit as st
|
|
|
|
| 2 |
from transformers import BlipProcessor, BlipForConditionalGeneration
|
| 3 |
from PIL import Image
|
|
|
|
| 4 |
|
| 5 |
# 加载BLIP模型和处理器
|
| 6 |
processor = BlipProcessor.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 7 |
model = BlipForConditionalGeneration.from_pretrained("Salesforce/blip-image-captioning-large")
|
| 8 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 9 |
st.title("图像描述生成器")
|
| 10 |
st.write("使用摄像头拍照并生成图像的描述。")
|
| 11 |
|
| 12 |
+
# 使用Streamlit的camera_input来获取用户摄像头输入
|
| 13 |
+
image_data = st.camera_input("请使用摄像头拍照")
|
|
|
|
| 14 |
|
| 15 |
+
if image_data is not None:
|
| 16 |
+
# 将图像数据转换为PIL图像
|
| 17 |
+
image = Image.open(image_data)
|
| 18 |
+
|
| 19 |
# 显示拍摄的图像
|
| 20 |
+
st.image(image, caption="拍摄的图像", use_column_width=True)
|
|
|
|
| 21 |
|
| 22 |
+
# 生成图像描述
|
| 23 |
+
inputs = processor(image, return_tensors="pt")
|
| 24 |
+
out = model.generate(**inputs)
|
| 25 |
+
caption = processor.decode(out[0], skip_special_tokens=True)
|
| 26 |
+
|
| 27 |
+
st.write(f"图像描述: {caption}")
|