简体   繁体   English

使用 Google Cloud Vision 进行图像分类 API

[英]Image Classification Using Google Cloud Vision API

I have a Flask app in which a user uploads a file and then I make a request to google cloud vision API to classify it.我有一个 Flask 应用程序,用户在其中上传文件,然后我向 google cloud vision API 发出请求以对其进行分类。

I'm doing the following:我正在做以下事情:

@app.route('/cloud_vision', methods=['POST'])
def cloud_vision():
    files = flask.request.files.getlist('files')

    client = vision.ImageAnnotatorClient()

    if len(files) > 1 or files[0].filename != '':
        for file in files:
            with io.open(file, 'rb') as image_file:
                content = image_file.read()
            image = vision.Image(content=content)
            response = client.label_detection(image=image)
            labels = response.label_annotations
            (...)

But im getting this error TypeError: expected str, bytes or os.PathLike object, not FileStorage when opening the file.但我收到此错误TypeError: expected str, bytes or os.PathLike object, not FileStorage打开文件时。 How can I correct this?我该如何纠正这个问题?

Please note that I'm not familiar with flask but to resolve your issue you should use file.filename in io.open() .请注意,我不熟悉 flask但要解决您的问题,您应该在file.filename io.open()中使用 file.filename。 I used the sample code in this article to be able to use getlist() .我使用本文中的示例代码来使用getlist() See code below:请参阅下面的代码:

app.py应用程序.py

from flask import Flask, render_template, request, redirect, url_for
from google.cloud import vision
import io

app = Flask(__name__)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/', methods=['POST'])
def upload_file():
    client = vision.ImageAnnotatorClient()

    files = request.files.getlist('file')

    if len(files) > 1 or files[0].filename != '':
        for file in files:
            file.save(file.filename) # I add saving for testing

            with io.open(file.filename, 'rb') as image_file: # use file.filename
                content = image_file.read() 

            image = vision.Image(content=content)
            response = client.label_detection(image=image)
            labels = response.label_annotations

    return str(labels)

if __name__==('__main__'):
    app.run(debug=True)

index.html index.html

<!doctype html>
<html>
  <head>
    <title>File Upload</title>
  </head>
  <body>
    <h1>File Upload</h1>
    <form method="POST" action="" enctype="multipart/form-data">
      <p><input type="file" name="file"></p>
      <p><input type="submit" value="Submit"></p>
    </form>
  </body>
</html>

Testing (image used is seen here ):测试(使用的图像见此处):

在此处输入图像描述

After submitting the image, I returned the response on the webpage.提交图片后,我在网页上返回了响应。

Output after submitting the image: Output 提交图片后:

在此处输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 节点JS | 谷歌视觉 API | 将 base64 发送到 Google Cloud Vision 时出现“错误的图像数据” - Node JS | Google Vision API | "Bad image data" when sending base64 to Google Cloud Vision KeyError: 'textAnnotations' while doing OCR using Google Cloud Vision API - KeyError: 'textAnnotations' while doing OCR using Google Cloud Vision API Google Cloud Vision API PDF 文本提取 - Google Cloud Vision API PDF text extraction OCR PDF 文件使用 Google Cloud Vision? - OCR PDF Files Using Google Cloud Vision? Google Cloud AutoML Vision 支持哪些图像位类型? - What image bit types are supported by Google Cloud AutoML Vision? Google Cloud Vision API 将所有图像检测为不当 - Google Cloud Vision API detects all images as inappropriate 谷歌云愿景与谷歌存储 - Google cloud vision with Google storage 谷歌视觉 API 可以接受外部图像 URL 吗? - Can google vision API accept external image URL? 使用 python 在 Google Cloud Vision 中逐行检测文本 - Line by Line Text Detection in Google Cloud Vision using python 是否可以在 Xamarin 中使用 Google Cloud Vision Nuget 突出显示检测到的文本? - Is it possible to highlight detected text using the Google Cloud Vision Nuget in Xamarin?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM