Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import configparser
- import copy
- import json
- import os
- import shutil
- import sys
- import time
- import uuid
- import zipfile
- from pathlib import Path
- import requests
- import flask
- import cv2
- import numpy as np
- app = flask.Flask(__name__)
- # Implement API here
- #############################
- download_image_api_url_template = 'https://sandbox.toloka.yandex.ru/api/v1/attachments/{}/download'
- token = 'AQAAAAAmIyAzAAIbumyt8ZNFsETZl16aYI24LN4'
- @app.after_request
- def after_request(response):
- response.headers.add('Access-Control-Allow-Origin', '*')
- response.headers.add('Access-Control-Allow-Headers', '*')
- response.headers.add('Access-Control-Allow-Methods', '*')
- return response
- """
- Returns status of detection
- Request
- GET
- images: array of images
- Response
- GET
- result: 1 or 0
- error_message: if result is 0, message with description
- """
- @app.route('/api/0.1/check_images', methods = ['GET'])
- def check_image():
- js = flask.request.get_json()
- images_ids = js.get('images')
- print(images_ids)
- images = []
- for image in images_ids:
- url = download_image_api_url_template.format(image)
- headers = {'Authorization': 'OAuth {}'.format(token)}
- buf = requests.get(url, headers = headers).content
- encoded = np.frombuffer(buf, dtype = np.uint8)
- img = cv2.imdecode(encoded, cv2.IMREAD_UNCHANGED)
- images.append(img)
- ##################################
- def to_json(data):
- return json.dumps(data) + "\n"
- def resp(code, succeed, error_message, data = {}, mimetype = 'application/json'):
- if mimetype == 'application/json':
- body = to_json({'succeed' : succeed,
- 'error_message' : error_message,
- **data})
- else:
- body = data
- return flask.Response(
- status = code,
- mimetype = mimetype,
- response = body
- )
- # e.g. failed to parse json
- @app.errorhandler(400)
- def err(e):
- return resp(err_code_wrong_args, False, 'Failed to parse JSON')
- @app.errorhandler(404)
- def page_not_found(e):
- return resp(err_code_wrong_args, False, 'No such method on server')
- @app.errorhandler(405)
- def page_not_found(e):
- return resp(err_code_wrong_args, False, 'Methods is not allowed')
- if __name__ == '__main__':
- app.debug = True # enables auto reload during development
- app.run(host= '0.0.0.0', debug = True, port = 3180)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement