Пример #1
0
def process_xml_posted(xml):
    """
    Based on posted xml, construct the corresponding response
    :param xml: posted xml from WeChat server
    :return: respond body
    """

    wechatdata = WechatXml(xml)
    wechatdata.determine_media_type()
    # TODO: to extend to other types of media type
    if wechatdata.is_image:
        logging.info("\tReceived xml contains image")
        wechatdata.parse_image_xml()
        # store image content and info into storage and db
        wechatdb = Wechat_Database(wechatdata)
        wechatdb.store_media_info_to_db(xml)
        msg_info = wechatdata.xlm_content_dict
    elif wechatdata.is_msg:
        logging.info("\tReceived xml contains msg")
        wechatdata.parse_msg_xml()
        msg_info = wechatdata.xlm_content_dict
        if is_request_image_back(msg_info):
        # send image back. right now just a message
            logging.info("\tRecieved xml request: asking for photos")
            wechatdata.is_request = True
            downloader = RequestProcessor(msg_info)
            callback_msg_content = downloader.uploadAllPhotosOfUser()
            msg_info['content'] = callback_msg_content
    elif wechatdata.is_video:
        logging.info("\tReceived xml contains video")
        # store video content and info into storage and db
        wechatdata.parse_video_xml()
        wechatdb = Wechat_Database(wechatdata)
        wechatdb.store_media_info_to_db(xml)
        msg_info = wechatdata.xlm_content_dict
    else:
        logging.info("\tReceived other types of xml")
    # form response
    response = construct_response_msg(msg_info, wechatdata)
    return response
    return flask.jsonify(data)


@app.route("/segment_board", methods=["POST"])
def segment_board():
    data = {"success": False}

    # ensure an image was properly uploaded to our endpoint
    if flask.request.method == "POST":
        if flask.request.files.get("image"):
            # read the image in PIL format
            image = flask.request.files["image"].read()
            image = Image.open(io.BytesIO(image))

            if image:
                image = np.array(image)
                print(image.shape)
                request_processor.segment_chess_board(image)
                data["success"] = True

    return flask.jsonify(data)


# if this is the main thread of execution first load the model and
# then start the server
if __name__ == "__main__":
    request_processor = RequestProcessor()
    redis_provider = RedisProvider()
    app.run(debug=True, use_reloader=False)