# @Author  : lightXu
# @File    : sheet_views.py
# @Time    : 2018/12/19 0019 下午 14:28
import json
import os
import shutil
import time
import traceback

import cv2
from django.conf import settings
from django.http import HttpResponse
from django.shortcuts import render
from django.views.decorators.csrf import csrf_exempt

import segment.logging_config as logging
from segment.form import SubmitSeriesNumberForm, UploadFileForm
from segment.form import UploadImageForm, UploadImageWithPaperIdForm, DownloadImage
from segment.image_operation.utils import resize_by_percent, write_single_img
from segment.models import SheetBigBoxes, SheetBoxes

from segment.sheet_resolve.tools.tf_sess import TfSess
from segment.sheet_resolve.tools.utils import read_xml_to_json, read_single_img, NpEncoder
from segment.sheet_server import handle_uploaded_xml_file, generate_serial_number
from segment.sheet_server import save_raw_image_without_segment, save_raw_image_with_paper_id
from segment.sheet_server import sheet_big_boxes_resolve, sheet_small_boxes_resolve, gen_xml, decide_blank_sheet
from segment.sheet_server import sheet_row_col_resolve, sheet_detail_resolve, sheet_format_output, sheet_points, \
    sheet_anchor
from segment.sheet_resolve.analysis.sheet.sheet_infer import subfield_answer_sheet
from segment.sheet_resolve.tools.brain_api import get_ocr_text_and_coordinate

logger = logging.getLogger(settings.LOGGING_TYPE)

subject_id_dict = {0: 'unknown_subject',
                   3: 'math',
                   6: 'math_zxhx',
                   8: 'english',
                   9: 'chinese',
                   12: 'physics',
                   13: 'chemistry',
                   14: 'biology',
                   15: 'politics',
                   16: 'history',
                   17: 'geography',
                   18: 'science_comprehensive',
                   19: 'arts_comprehensive'
                   }

tf_sess_dict = {
    # 'choice_m': TfSess('choice_m'),
    # 'cloze': TfSess('cloze'),
    # 'math_zxhx': TfSess('math_zxhx'),
    # 'math_zxhx_detail': TfSess('math_zxhx_detail'),

    # 'math': TfSess('math'),
    # 'english': TfSess('english'),
    # 'chinese': TfSess('chinese'),
    # 'physics': TfSess('physics'),
    # 'chemistry': TfSess('chemistry'),
    # 'biology': TfSess('biology'),
    # 'politics': TfSess('politics'),
    # 'history': TfSess('history'),
    # 'geography': TfSess('geography'),
    # 'science_comprehensive': TfSess('science_comprehensive'),
    # 'arts_comprehensive': TfSess('arts_comprehensive'),
    # 'chinese_blank': TfSess('chinese_blank'),
    # 'science_comprehensive_blank': TfSess('science_comprehensive_blank'),
    # 'arts_comprehensive_blank': TfSess('arts_comprehensive_blank'),
}


# Create your views here.
def index(request):
    return render(request, 'sheet.html')


@csrf_exempt
def analysis_big_box(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img_list = request.FILES.getlist('img_data')

            res_info_list = []
            error_info = ''
            is_success = 1
            try:
                save_path = ''
                image = ''
                series_number = ''
                for img in upload_img_list:
                    start_time = time.time()
                    raw_name = img.name
                    try:
                        save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, subject)
                        series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                        img_instance = SheetBigBoxes(series_number=series_number,
                                                     raw_name=raw_name, save_path=save_path,
                                                     subject_id=subject_id, subject=subject)
                        img_instance.save()
                        logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                    except Exception as e:
                        traceback.print_exc()
                        logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                    sheet_sess = tf_sess_dict[subject]
                    status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
                                                                              save_path, subject, sheet_sess)
                    bbox_info['series_number'] = series_number
                    bbox_info["img_name"] = raw_name
                    SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                    is_success = status
                    end_time = time.time()
                    cost_time = '{:.2f}s'.format(float(end_time - start_time))
                    bbox_info.update({'cost_time': cost_time})
                    ocr_res = get_ocr_text_and_coordinate(image)
                    bbox_info.update({'ocr': ocr_res})
                    res_info_list.append(bbox_info)
            except Exception as e:
                traceback.print_exc()
                logger.info('big box resolve error: ', e)
                is_success = 0
                error_info = 'big box resolve error'

            res = {'is_success': is_success, 'imgs_info': res_info_list}
            if error_info:
                res['error'] = error_info
            res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
            logger.info('segment_info: {}\n'.format(res_json))

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_small_box(request):
    if request.method == 'POST':
        form = UploadFileForm(request.POST, request.FILES)
        if form.is_valid():
            upload_xml_list = request.FILES.getlist('xml_file')

            res_info_list = []
            error_info = ''
            is_success = 1
            try:
                for xml in upload_xml_list:
                    raw_name = xml.name

                    reviewed_xml_save_path = os.path.join(settings.XML_ROOT, raw_name.replace('.xml', '_review.xml'))
                    handle_uploaded_xml_file(xml, reviewed_xml_save_path)
                    sheet_dict = read_xml_to_json(reviewed_xml_save_path)
                    start_time = time.time()
                    series_number = sheet_dict['series_number']
                    raw_image_path = SheetBigBoxes.objects.get(series_number=series_number).save_path
                    final_xml_path = raw_image_path.replace('.jpg', '.xml')
                    shutil.move(reviewed_xml_save_path, final_xml_path)

                    raw_img = read_single_img(raw_image_path)
                    small_box_sheet_dict = sheet_small_boxes_resolve(raw_img, sheet_dict,
                                                                     tf_sess_dict['choice'], tf_sess_dict['cloze'],
                                                                     final_xml_path)

                    is_success = 1
                    end_time = time.time()
                    cost_time = '{:.2f}s'.format(float(end_time - start_time))
                    small_box_sheet_dict.update({'cost_time': cost_time})
                    res_info_list.append(small_box_sheet_dict)
            except Exception as e:
                logger.info('small box resolve error: {}'.format(e))
                is_success = 0
                error_info = 'small box resolve error'

            res = {'is_success': is_success, 'imgs_info': res_info_list}
            if error_info:
                res['error'] = error_info
            res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
            # logger.info('resolve: {}\n'.format(res_json))

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_sucecss = 99
            res = {'is_success': is_sucecss, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def upload_exam(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img_list = request.FILES.getlist('img_data')

            res_info_list = []
            error_info = ''
            is_success = 1

            for img in upload_img_list:
                raw_name = img.name
                try:
                    save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
                    series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                    img_instance = SheetBigBoxes(series_number=series_number,
                                                 raw_name=raw_name, save_path=save_path,
                                                 subject_id=subject_id, subject=subject)
                    img_instance.save()
                    res_info_list.append({'raw_name': raw_name,
                                          'series_number': series_number,
                                          raw_name: series_number})
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    # traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

            res = {'is_success': is_success, 'imgs_info': res_info_list}
            if error_info:
                res['error'] = error_info
            res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_box(request):
    if request.method == 'POST':
        form = SubmitSeriesNumberForm(request.POST)
        if form.is_valid():
            series_number = form.cleaned_data['series_number']
            small_box_path = SheetBigBoxes.objects.get(series_number=series_number).small_box_path
            if not small_box_path:
                res = {'series_number': series_number, 'info': '尚未处理完成', 'status': 100}
                return HttpResponse('{}'.format(res))

            else:
                res = json.load(small_box_path.replace('.xml', '.json'))
                res['series_number'] = series_number
                res['status'] = 1000
                return HttpResponse('{}'.format(res))

        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_box_once(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'math'  # default
            upload_img_list = request.FILES.getlist('img_data')

            res_info_list = []
            error_info = ''
            is_success = 1
            try:
                save_path = ''
                image = ''
                series_number = ''
                for img in upload_img_list:
                    start_time = time.time()
                    raw_name = img.name
                    try:
                        save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
                        series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                        img_instance = SheetBigBoxes(series_number=series_number,
                                                     raw_name=raw_name, save_path=save_path,
                                                     subject_id=subject_id, subject=subject)
                        img_instance.save()
                        logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                    except Exception as e:
                        # traceback.print_exc()
                        logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                    status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
                                                                              save_path, 'sheet',
                                                                              tf_sess_dict['math_zxhx'])
                    bbox_info['series_number'] = series_number
                    bbox_info["img_name"] = raw_name
                    SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                    small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
                    shutil.copy(raw_xml_path, small_box_xml_path)
                    sheet_dict = bbox_info
                    small_box_sheet_dict = sheet_small_boxes_resolve(image, sheet_dict,
                                                                     tf_sess_dict['choice'], tf_sess_dict['cloze'],
                                                                     small_box_xml_path)
                    SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=small_box_xml_path)

                    is_success = status
                    end_time = time.time()
                    cost_time = '{:.2f}s'.format(float(end_time - start_time))
                    small_box_sheet_dict.update({'cost_time': cost_time})
                    res_info_list.append(small_box_sheet_dict)
            except Exception as e:
                logger.info('box resolve error: {}'.format(e))
                is_success = 0
                error_info = 'box resolve error'

            res = {'is_success': is_success, 'imgs_info': res_info_list}
            if error_info:
                res['error'] = error_info
            res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
            # logger.info('segment_info: {}\n'.format(res_json))

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_box_once_with_multiple_img(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject_init = subject_id_dict.get(subject_id)
            if not subject_init:
                subject_init = 'math'
            upload_img_list = request.FILES.getlist('img_data')

            res_info_list = []
            image_list = []
            detail_xml = []
            ocr_list = []
            error_info = ''
            is_success = 1

            save_path = ''
            image = ''
            series_number = ''
            for img_index, img in enumerate(upload_img_list):
                subject = subject_init
                start_time = time.time()
                raw_name = img.name
                try:
                    save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
                    series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                    img_instance = SheetBigBoxes(series_number=series_number,
                                                 raw_name=raw_name, save_path=save_path,
                                                 subject_id=subject_id, subject=subject)
                    img_instance.save()
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                try:

                    try:
                        ocr_res = get_ocr_text_and_coordinate(image)
                    except Exception as e:
                        traceback.print_exc()
                        error_info = error_info + 'ocr error;'
                        ocr_res = ''

                    ocr_list.append(ocr_res)

                    if subject not in ["chinese", "science_comprehensive", "arts_comprehensive"]:
                        sheet_sess = tf_sess_dict[subject]
                        answered = "fixed"
                    elif decide_blank_sheet(image):
                        sheet_sess = tf_sess_dict[subject + '_blank']
                        subject = subject + '_blank'
                        answered = "blank"
                    else:
                        sheet_sess = tf_sess_dict[subject]
                        answered = "answered"

                    status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
                                                                               save_path, subject,
                                                                               sheet_sess, ocr=ocr_res)
                    # print(sheet_dict)
                    sheet_dict['series_number'] = series_number
                    sheet_dict["img_name"] = raw_name
                    sheet_dict["answered"] = answered
                    SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                    small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
                    detail_xml.append(small_box_xml_path)
                    shutil.copy(raw_xml_path, small_box_xml_path)
                    try:
                        # 找题号
                        small_box_sheet_dict = sheet_detail_resolve(image, sheet_dict, small_box_xml_path, shrink=True)
                    except Exception as e:
                        traceback.print_exc()
                        status = 0
                        small_box_sheet_dict = sheet_dict

                    SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=small_box_xml_path)
                    is_success = status
                    end_time = time.time()
                    cost_time = '{:.2f}s'.format(float(end_time - start_time))
                    small_box_sheet_dict.update({'cost_time': cost_time})

                    res_info_list.append(small_box_sheet_dict)
                    image_list.append(image)
                except Exception as e:
                    traceback.print_exc()
                    logger.info('{}试卷： {} 解析失败: {}'.format(subject, raw_name, e))
                    is_success = 0
                    error_info = error_info + 'box resolve error;'

            try:
                res_info_list = sheet_points(res_info_list, image_list, ocr_list, if_ocr=False)
            except Exception as e:
                print(e)
                traceback.print_exc()
                error_info = error_info + 'number and points error;'

            try:
                logger.info('before format: {}\n'.format(res_info_list))
                init_number = 500
                crt_numbers = []
                for i, ele in enumerate(res_info_list):
                    image = image_list[i]
                    res, init_number, crt_numbers = sheet_format_output(init_number, crt_numbers,
                                                                        ele, image, subject_init,
                                                                        shrink=True)
                    res_info_list[i] = res
            except Exception as e:
                print(e)
                traceback.print_exc()
                error_info = error_info + 'format output error ;'

            try:
                for image_index, image in enumerate(image_list):
                    anchor_list = sheet_anchor(image)
                    res_info_list[image_index]['regions'] = res_info_list[image_index]['regions'] + anchor_list
            except Exception as e:
                print(e)
                traceback.print_exc()
                error_info = error_info + 'anchor error;'

            res = {'is_success': is_success, 'imgs_info': res_info_list}
            try:
                for xml_index, ele in enumerate(res_info_list):
                    regions = ele['regions']
                    gen_xml(regions, detail_xml[xml_index])
            except Exception as e:
                print(e)
                traceback.print_exc()

            if error_info:
                res['error'] = error_info
            res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
            logger.info('segment_info: {}\n'.format(res_json))

            print('sheet resolve done')
            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def saas_analysis_box_once_with_multiple_img(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img_list = request.FILES.getlist('img_data')

            res_info_list = []
            image_list = []
            detail_xml = []
            ocr_list = []
            error_info = ''
            is_success = 1

            save_path = ''
            image = ''
            series_number = ''
            for img_index, img in enumerate(upload_img_list):
                start_time = time.time()
                raw_name = img.name
                try:
                    save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
                    series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                    img_instance = SheetBigBoxes(series_number=series_number,
                                                 raw_name=raw_name, save_path=save_path,
                                                 subject_id=subject_id, subject=subject)
                    img_instance.save()
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    # traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                try:
                    status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
                                                                               save_path, subject,
                                                                               tf_sess_dict[subject])
                    sheet_dict['series_number'] = series_number
                    sheet_dict["img_name"] = raw_name
                    SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                    small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
                    detail_xml.append(small_box_xml_path)
                    shutil.copy(raw_xml_path, small_box_xml_path)

                    small_box_sheet_dict = sheet_detail_resolve(image, sheet_dict, small_box_xml_path, shrink=False)

                    SheetBigBoxes.objects.filter(series_number=series_number) \
                        .update(small_box_path=small_box_xml_path)

                    is_success = status
                    end_time = time.time()
                    cost_time = '{:.2f}s'.format(float(end_time - start_time))
                    small_box_sheet_dict.update({'cost_time': cost_time})
                    res_info_list.append(small_box_sheet_dict)
                    image_list.append(image)

                    ocr_res = get_ocr_text_and_coordinate(image)
                    ocr_list.append(ocr_res)

                except Exception as e:
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))
                    is_success = 0
                    error_info = error_info + 'box resolve error;'

            try:
                res_info_list = sheet_points(res_info_list, image_list, ocr_list, if_ocr=True)
            except Exception as e:
                print(e)
                traceback.print_exc()
                is_success = 0
                error_info = error_info + 'number and points error;'
                res_info_list = [ele.update({'ocr': ocr_list[i]}) for i, ele in enumerate(res_info_list)]

            for i, ele in enumerate(res_info_list):
                image = image_list[i]
                res = sheet_format_output(ele, image, subject, shrink=True)
                res_info_list[i] = res

            res = {'is_success': is_success, 'imgs_info': res_info_list}
            for xml_index, ele in enumerate(res_info_list):
                regions = ele['regions']
                gen_xml(regions, detail_xml[xml_index])
            if error_info:
                res['error'] = error_info
            res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
            # logger.info('segment_info: {}\n'.format(res_json))

            print('sheet resolve done')
            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_box_once_and_single_img(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img = request.FILES.get('img_data')

            error_info = ''
            detail_box_sheet_dict = dict()
            try:
                save_path = ''
                image = ''
                series_number = ''
                start_time = time.time()
                raw_name = upload_img.name
                try:
                    save_path, image, _ = save_raw_image_without_segment(subject, time_str, upload_img, 'sheet')
                    series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                    img_instance = SheetBigBoxes(series_number=series_number,
                                                 raw_name=raw_name, save_path=save_path,
                                                 subject_id=subject_id, subject=subject)
                    img_instance.save()
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    # traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image, save_path, subject,
                                                                          tf_sess_dict[subject])
                bbox_info['series_number'] = series_number
                bbox_info["img_name"] = raw_name
                SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                detail_box_xml_path = raw_xml_path.replace('.xml', '_detail.xml')
                shutil.copy(raw_xml_path, detail_box_xml_path)
                sheet_dict = bbox_info
                # small_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
                #                                              tf_sess_dict['choice_m'], tf_sess_dict['cloze'],
                #                                              small_box_xml_path)
                detail_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
                                                              '', tf_sess_dict['cloze'],
                                                              detail_box_xml_path)
                SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=detail_box_xml_path)

                is_success = status
                # end_time = time.time()
                # cost_time = '{:.2f}s'.format(float(end_time - start_time))
                # small_box_sheet_dict.update({'cost_time': cost_time})

            except Exception as e:
                logger.info('box resolve error: {}'.format(e))
                is_success = 0
                error_info = 'box resolve error'

            detail_box_sheet_dict.update({'is_success': is_success})

            if error_info:
                detail_box_sheet_dict.update({'is_success': is_success, 'error': error_info})
            res_json = json.dumps(detail_box_sheet_dict, ensure_ascii=False)
            # logger.info('segment_info: {}\n'.format(res_json))

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_box_once_and_single_img_with_paperid(request):
    if request.method == 'POST':
        form = UploadImageWithPaperIdForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img = request.FILES.get('img_data')
            paper_id = form.cleaned_data['paper_id']

            error_info = ''
            small_box_sheet_dict = dict()
            try:
                save_path = ''
                image = ''
                start_time = time.time()
                raw_name = upload_img.name
                try:
                    save_path, image, image_url = save_raw_image_with_paper_id(subject, paper_id, upload_img, 'sheet')
                    # series_number = generate_serial_number(time_str.replace('-', ''), SheetBoxes)
                    img_instance = SheetBoxes(paper_id=paper_id,
                                              raw_name=raw_name, save_path=save_path,
                                              subject_id=subject_id, subject=subject,
                                              download_path=image_url)
                    img_instance.save()
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    # traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                sheet_sess = tf_sess_dict[subject]
                status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(paper_id, image, save_path, subject,
                                                                           sheet_sess)
                sheet_dict['series_number'] = paper_id
                sheet_dict["img_name"] = raw_name

                # small_box_sheet_dict = sheet_small_boxes_resolve(image, sheet_dict,
                #                                                  tf_sess_dict['choice'], tf_sess_dict['cloze'],
                #                                                  raw_xml_path)
                small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
                shutil.copy(raw_xml_path, small_box_xml_path)
                small_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
                                                             '', tf_sess_dict['cloze'],
                                                             small_box_xml_path)
                SheetBoxes.objects.filter(paper_id=paper_id).update(xml_box_path=small_box_xml_path)

                is_success = status
                end_time = time.time()
                cost_time = '{:.2f}s'.format(float(end_time - start_time))
                small_box_sheet_dict.update({'cost_time': cost_time})

            except Exception as e:
                logger.info('box resolve error: {}'.format(e))
                is_success = 0
                error_info = 'box resolve error'

            small_box_sheet_dict.update({'is_success': is_success})

            if error_info:
                small_box_sheet_dict.update({'is_success': is_success, 'error': error_info})
            res_json = json.dumps(small_box_sheet_dict, ensure_ascii=False)
            # logger.info('segment_info: {}\n'.format(res_json))

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form = UploadImageWithPaperIdForm()
    return render(request, 'sheet_with_id.html', {'form': form})


@csrf_exempt
def analysis_box_detail(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img = request.FILES.get('img_data')
            error_info = ''
            sheet_dict = dict()

            try:
                save_path = ''
                image = ''
                series_number = ''
                raw_name = upload_img.name
                try:
                    save_path, image, _ = save_raw_image_without_segment(subject, time_str, upload_img, 'sheet')  # 保存文件

                    series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                    img_instance = SheetBigBoxes(series_number=series_number,
                                                 raw_name=raw_name, save_path=save_path,
                                                 subject_id=subject_id, subject=subject)
                    img_instance.save()
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    # traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                sheet_sess = tf_sess_dict[subject]
                status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
                                                                          save_path, subject, sheet_sess)
                bbox_info['series_number'] = series_number
                bbox_info["img_name"] = raw_name
                SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                # small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
                # shutil.copy(raw_xml_path, small_box_xml_path)
                sheet_dict = bbox_info

                is_success = status

            except Exception as e:
                logger.info('box resolve error: {}'.format(e))
                is_success = 0
                error_info = 'box resolve error'

            sheet_dict.update({'is_success': is_success})

            if error_info:
                sheet_dict.update({'is_success': is_success, 'error': error_info})
            res_json = json.dumps(sheet_dict, ensure_ascii=False)
            # logger.info('segment_info: {}\n'.format(res_json))

            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def analysis_box_detail_and_show(request):
    if request.method == 'POST':
        time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
        form = UploadImageForm(request.POST, request.FILES)
        if form.is_valid():
            subject_id = int(form.cleaned_data['subject'])
            subject = subject_id_dict.get(subject_id)
            if not subject:
                subject = 'unknown_subject'
            upload_img = request.FILES.get('img_data')
            error_info = ''
            img_url_path = ''
            sheet_dict = dict()
            height, width = 1200, 1600
            try:
                save_path = ''
                image = ''
                series_number = ''
                raw_name = upload_img.name
                try:
                    save_path, image, img_url_path = save_raw_image_without_segment(subject, time_str, upload_img,
                                                                                    'sheet')
                    # small_img_path = save_path.replace('.jpg', '_small.jpg').replace('\\', '/')
                    series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
                    img_instance = SheetBigBoxes(series_number=series_number,
                                                 raw_name=raw_name, save_path=save_path,
                                                 subject_id=subject_id, subject=subject)
                    img_instance.save()
                    logger.info('{}试卷： {} 存储成功: {}'.format(subject, raw_name, save_path))
                except Exception as e:
                    # traceback.print_exc()
                    logger.info('{}试卷： {} 存储失败: {}'.format(subject, raw_name, e))

                sheet_sess = tf_sess_dict[subject]
                status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
                                                                          save_path, subject, sheet_sess)
                bbox_info['series_number'] = series_number
                bbox_info["img_name"] = raw_name
                SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)

                # small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
                # shutil.copy(raw_xml_path, small_box_xml_path)
                sheet_dict = bbox_info
                is_success = status

                show_ratio = 0.4
                img_small = resize_by_percent(image, show_ratio)
                font = cv2.FONT_HERSHEY_COMPLEX
                for ele in bbox_info['regions']:
                    name = ele['class_name']
                    xmin = int(int(ele['bounding_box']['xmin']) * show_ratio)
                    ymin = int(int(ele['bounding_box']['ymin']) * show_ratio)
                    xmax = int(int(ele['bounding_box']['xmax']) * show_ratio)
                    ymax = int(int(ele['bounding_box']['ymax']) * show_ratio)

                    cv2.rectangle(img_small, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0), 1)
                    cv2.putText(img_small, name, (xmin, ymax), font, 0.7, (255, 106, 106), 2, False)
                height, width = img_small.shape[0], img_small.shape[1]

                line_xmax1, line_xmax2 = subfield_answer_sheet(image, bbox_info)
                if line_xmax1 != 0 and line_xmax2 != 0:
                    line_xmax1_1 = int(line_xmax1 * show_ratio)
                    line_xmax2_1 = int(line_xmax2 * show_ratio)
                    cv2.rectangle(img_small, (20, 20), (line_xmax1_1, height - 20), (0, 0, 255), 1)
                    cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
                    cv2.rectangle(img_small, (line_xmax1_1, 20), (line_xmax2_1, height - 20), (0, 0, 255), 1)
                    cv2.putText(img_small, 'subfield_line', (line_xmax1_1, height - 20), font, 0.7, (0, 0, 255), 2,
                                False)
                    cv2.rectangle(img_small, (line_xmax2_1, 20), (width - 20, height - 20), (0, 0, 255), 1)
                    cv2.putText(img_small, 'subfield_line', (line_xmax2_1, height - 20), font, 0.7, (0, 0, 255), 2,
                                False)
                elif line_xmax1 != 0 and line_xmax2 == 0:
                    if int(line_xmax1 * show_ratio) == width:
                        line_xmax1_1 = int(line_xmax1 * show_ratio)
                        cv2.rectangle(img_small, (20, 20), (line_xmax1_1 - 20, height - 20), (0, 0, 255), 1)
                        cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
                    else:
                        line_xmax1_1 = int(line_xmax1 * show_ratio)
                        cv2.rectangle(img_small, (20, 20), (line_xmax1_1, height - 20), (0, 0, 255), 1)
                        cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
                        cv2.rectangle(img_small, (line_xmax1_1, 20), (width - 20, height - 20), (0, 0, 255), 1)
                        cv2.putText(img_small, 'subfield_line', (line_xmax1_1, height - 20), font, 0.7, (0, 0, 255), 2,
                                    False)

                write_single_img(img_small, save_path)
                height, width = img_small.shape[0], img_small.shape[1]

            except Exception as e:
                logger.info('box resolve error: {}'.format(e))
                is_success = 0
                error_info = 'box resolve error'

            sheet_dict.update({'is_success': is_success})

            if error_info:
                sheet_dict.update({'is_success': is_success, 'error': error_info})
            res_json = json.dumps(sheet_dict, ensure_ascii=False)
            # logger.info('segment_info: {}\n'.format(res_json))

            res_dict = {'url': img_url_path,
                        'img_height': height,
                        'text_height': 0.9 * height,
                        'texts': res_json,
                        'raw_texts': ''
                        }

            return render(request, 'showimg.html', res_dict)
            # return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form_img = UploadImageForm()
        form_xml = UploadFileForm()
    return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})


@csrf_exempt
def download_image(request):
    if request.method == 'POST':
        form = DownloadImage(request.POST, request.FILES)

        if form.is_valid():
            is_success = 1
            try:
                paper_id = form.cleaned_data['paper_id']
                # download_path = SheetBoxes.objects.get(paper_id=paper_id).download_path
                paper_id_obj = SheetBoxes.objects.filter(paper_id__icontains=paper_id)
                paper_id_list = [ele.paper_id for ele in paper_id_obj]
                download_path_list = [SheetBoxes.objects.get(paper_id=ele).download_path for ele in paper_id_list]

                res_dict = {'paper_id': paper_id_list,
                            'image_url': download_path_list,
                            # 'img_height': 1000,
                            # 'text_height': 10,
                            # 'texts': download_path,
                            # 'raw_texts': '',
                            'is_success': is_success
                            }
            except Exception as e:
                logger.info('image address load failed: {}'.format(e))
                is_success = 0
                error_info = 'image address load failed'
                res_dict = {'is_success': is_success, 'error': error_info}

            res_json = json.dumps(res_dict, ensure_ascii=False)
            # return render(request, 'showimg.html', res_dict)
            return HttpResponse(res_json)
        else:
            error_json = form.errors.as_json()
            is_success = 99
            res = {'is_success': is_success, 'error': error_json}
            return HttpResponse('{}'.format(res))

    else:
        form = DownloadImage()
    return render(request, 'downloadimg.html', {'form': form})
