Created
          July 19, 2020 06:21 
        
      - 
      
 - 
        
Save neelriyer/ca7ef12a11406fa4b8d70a4ddbd9cade to your computer and use it in GitHub Desktop.  
    detectron2 web app get and post image in flask
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | import io | |
| from flask import Flask, render_template, request, send_from_directory, send_file | |
| from PIL import Image | |
| import requests | |
| import os | |
| # function to load img from url | |
| def load_image_url(url): | |
| response = requests.get(url) | |
| img = Image.open(io.BytesIO(response.content)) | |
| return img | |
| @app.route("/detect", methods=['POST', 'GET']) | |
| def upload(): | |
| if request.method == 'POST': | |
| try: | |
| # open image | |
| file = Image.open(request.files['file'].stream) | |
| # remove alpha channel | |
| rgb_im = file.convert('RGB') | |
| rgb_im.save('file.jpg') | |
| # failure | |
| except: | |
| return render_template("failure.html") | |
| elif request.method == 'GET': | |
| # get url | |
| url = request.args.get("url") | |
| # save | |
| try: | |
| # save image as jpg | |
| # urllib.request.urlretrieve(url, 'file.jpg') | |
| rgb_im = load_image_url(url) | |
| rgb_im = rgb_im.convert('RGB') | |
| rgb_im.save('file.jpg') | |
| # failure | |
| except: | |
| return render_template("failure.html") | |
| return send_file(rgb_im, mimetype='image/jpeg') | 
  
    Sign up for free
    to join this conversation on GitHub.
    Already have an account?
    Sign in to comment