import gradio as gr from transformers import pipeline # 创建 pipeline,使用指定模型进行图像到文本的转换 pipe = pipeline("image-to-text", model="microsoft/trocr-base-handwritten") def recognize_text_from_image(input_image): # 使用 pipeline 处理上传的图片 output = pipe(input_image) # 返回识别出的文本 return output[0]['generated_text'] # 创建 Gradio 接口 iface = gr.Interface( fn=recognize_text_from_image, # 指定处理函数 inputs=gr.Image(tool="editor", type="pil", label="Upload your image here or use your camera"), # 设置输入为图片 outputs="text", # 输出类型为文本 title="Image to Text Recognition", # 接口标题 description="Upload an image and the text in the image will be recognized." # 接口描述 ) # 启动 Gradio 应用 iface.launch()