sheet_views.py 47 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009
  1. # @Author : lightXu
  2. # @File : sheet_views.py
  3. # @Time : 2018/12/19 0019 下午 14:28
  4. import json
  5. import os
  6. import shutil
  7. import time
  8. import traceback
  9. import cv2
  10. from django.conf import settings
  11. from django.http import HttpResponse
  12. from django.shortcuts import render
  13. from django.views.decorators.csrf import csrf_exempt
  14. import segment.logging_config as logging
  15. from segment.form import SubmitSeriesNumberForm, UploadFileForm
  16. from segment.form import UploadImageForm, UploadImageWithPaperIdForm, DownloadImage
  17. from segment.image_operation.utils import resize_by_percent, write_single_img
  18. from segment.models import SheetBigBoxes, SheetBoxes
  19. from segment.sheet_resolve.tools.tf_sess import TfSess
  20. from segment.sheet_resolve.tools.utils import read_xml_to_json, read_single_img, NpEncoder
  21. from segment.sheet_server import handle_uploaded_xml_file, generate_serial_number
  22. from segment.sheet_server import save_raw_image_without_segment, save_raw_image_with_paper_id
  23. from segment.sheet_server import sheet_big_boxes_resolve, sheet_small_boxes_resolve, gen_xml, decide_blank_sheet
  24. from segment.sheet_server import sheet_row_col_resolve, sheet_detail_resolve, sheet_format_output, sheet_points, \
  25. sheet_anchor
  26. from segment.sheet_resolve.analysis.sheet.sheet_infer import subfield_answer_sheet
  27. from segment.sheet_resolve.tools.brain_api import get_ocr_text_and_coordinate
  28. logger = logging.getLogger(settings.LOGGING_TYPE)
  29. subject_id_dict = {0: 'unknown_subject',
  30. 3: 'math',
  31. 6: 'math_zxhx',
  32. 8: 'english',
  33. 9: 'chinese',
  34. 12: 'physics',
  35. 13: 'chemistry',
  36. 14: 'biology',
  37. 15: 'politics',
  38. 16: 'history',
  39. 17: 'geography',
  40. 18: 'science_comprehensive',
  41. 19: 'arts_comprehensive'
  42. }
  43. tf_sess_dict = {
  44. # 'choice_m': TfSess('choice_m'),
  45. # 'cloze': TfSess('cloze'),
  46. # 'math_zxhx': TfSess('math_zxhx'),
  47. # 'math_zxhx_detail': TfSess('math_zxhx_detail'),
  48. # 'math': TfSess('math'),
  49. # 'english': TfSess('english'),
  50. # 'chinese': TfSess('chinese'),
  51. # 'physics': TfSess('physics'),
  52. 'chemistry': TfSess('chemistry'),
  53. # 'biology': TfSess('biology'),
  54. # 'politics': TfSess('politics'),
  55. # 'history': TfSess('history'),
  56. # 'geography': TfSess('geography'),
  57. # 'science_comprehensive': TfSess('science_comprehensive'),
  58. # 'arts_comprehensive': TfSess('arts_comprehensive'),
  59. # 'chinese_blank': TfSess('chinese_blank'),
  60. # 'science_comprehensive_blank': TfSess('science_comprehensive_blank'),
  61. # 'arts_comprehensive_blank': TfSess('arts_comprehensive_blank'),
  62. }
  63. # Create your views here.
  64. def index(request):
  65. return render(request, 'sheet.html')
  66. @csrf_exempt
  67. def analysis_big_box(request):
  68. if request.method == 'POST':
  69. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  70. form = UploadImageForm(request.POST, request.FILES)
  71. if form.is_valid():
  72. subject_id = int(form.cleaned_data['subject'])
  73. subject = subject_id_dict.get(subject_id)
  74. if not subject:
  75. subject = 'unknown_subject'
  76. upload_img_list = request.FILES.getlist('img_data')
  77. res_info_list = []
  78. error_info = ''
  79. is_success = 1
  80. try:
  81. save_path = ''
  82. image = ''
  83. series_number = ''
  84. for img in upload_img_list:
  85. start_time = time.time()
  86. raw_name = img.name
  87. try:
  88. save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, subject)
  89. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  90. img_instance = SheetBigBoxes(series_number=series_number,
  91. raw_name=raw_name, save_path=save_path,
  92. subject_id=subject_id, subject=subject)
  93. img_instance.save()
  94. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  95. except Exception as e:
  96. traceback.print_exc()
  97. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  98. sheet_sess = tf_sess_dict[subject]
  99. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  100. save_path, subject, sheet_sess)
  101. bbox_info['series_number'] = series_number
  102. bbox_info["img_name"] = raw_name
  103. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  104. is_success = status
  105. end_time = time.time()
  106. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  107. bbox_info.update({'cost_time': cost_time})
  108. ocr_res = get_ocr_text_and_coordinate(image)
  109. bbox_info.update({'ocr': ocr_res})
  110. res_info_list.append(bbox_info)
  111. except Exception as e:
  112. traceback.print_exc()
  113. logger.info('big box resolve error: ', e)
  114. is_success = 0
  115. error_info = 'big box resolve error'
  116. res = {'is_success': is_success, 'imgs_info': res_info_list}
  117. if error_info:
  118. res['error'] = error_info
  119. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  120. logger.info('segment_info: {}\n'.format(res_json))
  121. return HttpResponse(res_json)
  122. else:
  123. error_json = form.errors.as_json()
  124. is_success = 99
  125. res = {'is_success': is_success, 'error': error_json}
  126. return HttpResponse('{}'.format(res))
  127. else:
  128. form_img = UploadImageForm()
  129. form_xml = UploadFileForm()
  130. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  131. @csrf_exempt
  132. def analysis_small_box(request):
  133. if request.method == 'POST':
  134. form = UploadFileForm(request.POST, request.FILES)
  135. if form.is_valid():
  136. upload_xml_list = request.FILES.getlist('xml_file')
  137. res_info_list = []
  138. error_info = ''
  139. is_success = 1
  140. try:
  141. for xml in upload_xml_list:
  142. raw_name = xml.name
  143. reviewed_xml_save_path = os.path.join(settings.XML_ROOT, raw_name.replace('.xml', '_review.xml'))
  144. handle_uploaded_xml_file(xml, reviewed_xml_save_path)
  145. sheet_dict = read_xml_to_json(reviewed_xml_save_path)
  146. start_time = time.time()
  147. series_number = sheet_dict['series_number']
  148. raw_image_path = SheetBigBoxes.objects.get(series_number=series_number).save_path
  149. final_xml_path = raw_image_path.replace('.jpg', '.xml')
  150. shutil.move(reviewed_xml_save_path, final_xml_path)
  151. raw_img = read_single_img(raw_image_path)
  152. small_box_sheet_dict = sheet_small_boxes_resolve(raw_img, sheet_dict,
  153. tf_sess_dict['choice'], tf_sess_dict['cloze'],
  154. final_xml_path)
  155. is_success = 1
  156. end_time = time.time()
  157. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  158. small_box_sheet_dict.update({'cost_time': cost_time})
  159. res_info_list.append(small_box_sheet_dict)
  160. except Exception as e:
  161. logger.info('small box resolve error: {}'.format(e))
  162. is_success = 0
  163. error_info = 'small box resolve error'
  164. res = {'is_success': is_success, 'imgs_info': res_info_list}
  165. if error_info:
  166. res['error'] = error_info
  167. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  168. # logger.info('resolve: {}\n'.format(res_json))
  169. return HttpResponse(res_json)
  170. else:
  171. error_json = form.errors.as_json()
  172. is_sucecss = 99
  173. res = {'is_success': is_sucecss, 'error': error_json}
  174. return HttpResponse('{}'.format(res))
  175. else:
  176. form_img = UploadImageForm()
  177. form_xml = UploadFileForm()
  178. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  179. @csrf_exempt
  180. def upload_exam(request):
  181. if request.method == 'POST':
  182. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  183. form = UploadImageForm(request.POST, request.FILES)
  184. if form.is_valid():
  185. subject_id = int(form.cleaned_data['subject'])
  186. subject = subject_id_dict.get(subject_id)
  187. if not subject:
  188. subject = 'unknown_subject'
  189. upload_img_list = request.FILES.getlist('img_data')
  190. res_info_list = []
  191. error_info = ''
  192. is_success = 1
  193. for img in upload_img_list:
  194. raw_name = img.name
  195. try:
  196. save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
  197. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  198. img_instance = SheetBigBoxes(series_number=series_number,
  199. raw_name=raw_name, save_path=save_path,
  200. subject_id=subject_id, subject=subject)
  201. img_instance.save()
  202. res_info_list.append({'raw_name': raw_name,
  203. 'series_number': series_number,
  204. raw_name: series_number})
  205. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  206. except Exception as e:
  207. # traceback.print_exc()
  208. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  209. res = {'is_success': is_success, 'imgs_info': res_info_list}
  210. if error_info:
  211. res['error'] = error_info
  212. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  213. return HttpResponse(res_json)
  214. else:
  215. error_json = form.errors.as_json()
  216. is_success = 99
  217. res = {'is_success': is_success, 'error': error_json}
  218. return HttpResponse('{}'.format(res))
  219. else:
  220. form_img = UploadImageForm()
  221. form_xml = UploadFileForm()
  222. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  223. @csrf_exempt
  224. def analysis_box(request):
  225. if request.method == 'POST':
  226. form = SubmitSeriesNumberForm(request.POST)
  227. if form.is_valid():
  228. series_number = form.cleaned_data['series_number']
  229. small_box_path = SheetBigBoxes.objects.get(series_number=series_number).small_box_path
  230. if not small_box_path:
  231. res = {'series_number': series_number, 'info': '尚未处理完成', 'status': 100}
  232. return HttpResponse('{}'.format(res))
  233. else:
  234. res = json.load(small_box_path.replace('.xml', '.json'))
  235. res['series_number'] = series_number
  236. res['status'] = 1000
  237. return HttpResponse('{}'.format(res))
  238. else:
  239. error_json = form.errors.as_json()
  240. is_success = 99
  241. res = {'is_success': is_success, 'error': error_json}
  242. return HttpResponse('{}'.format(res))
  243. else:
  244. form_img = UploadImageForm()
  245. form_xml = UploadFileForm()
  246. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  247. @csrf_exempt
  248. def analysis_box_once(request):
  249. if request.method == 'POST':
  250. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  251. form = UploadImageForm(request.POST, request.FILES)
  252. if form.is_valid():
  253. subject_id = int(form.cleaned_data['subject'])
  254. subject = subject_id_dict.get(subject_id)
  255. if not subject:
  256. subject = 'math' # default
  257. upload_img_list = request.FILES.getlist('img_data')
  258. res_info_list = []
  259. error_info = ''
  260. is_success = 1
  261. try:
  262. save_path = ''
  263. image = ''
  264. series_number = ''
  265. for img in upload_img_list:
  266. start_time = time.time()
  267. raw_name = img.name
  268. try:
  269. save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
  270. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  271. img_instance = SheetBigBoxes(series_number=series_number,
  272. raw_name=raw_name, save_path=save_path,
  273. subject_id=subject_id, subject=subject)
  274. img_instance.save()
  275. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  276. except Exception as e:
  277. # traceback.print_exc()
  278. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  279. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  280. save_path, 'sheet',
  281. tf_sess_dict['math_zxhx'])
  282. bbox_info['series_number'] = series_number
  283. bbox_info["img_name"] = raw_name
  284. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  285. small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  286. shutil.copy(raw_xml_path, small_box_xml_path)
  287. sheet_dict = bbox_info
  288. small_box_sheet_dict = sheet_small_boxes_resolve(image, sheet_dict,
  289. tf_sess_dict['choice'], tf_sess_dict['cloze'],
  290. small_box_xml_path)
  291. SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=small_box_xml_path)
  292. is_success = status
  293. end_time = time.time()
  294. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  295. small_box_sheet_dict.update({'cost_time': cost_time})
  296. res_info_list.append(small_box_sheet_dict)
  297. except Exception as e:
  298. logger.info('box resolve error: {}'.format(e))
  299. is_success = 0
  300. error_info = 'box resolve error'
  301. res = {'is_success': is_success, 'imgs_info': res_info_list}
  302. if error_info:
  303. res['error'] = error_info
  304. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  305. # logger.info('segment_info: {}\n'.format(res_json))
  306. return HttpResponse(res_json)
  307. else:
  308. error_json = form.errors.as_json()
  309. is_success = 99
  310. res = {'is_success': is_success, 'error': error_json}
  311. return HttpResponse('{}'.format(res))
  312. else:
  313. form_img = UploadImageForm()
  314. form_xml = UploadFileForm()
  315. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  316. @csrf_exempt
  317. def analysis_box_once_with_multiple_img(request):
  318. if request.method == 'POST':
  319. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  320. form = UploadImageForm(request.POST, request.FILES)
  321. if form.is_valid():
  322. subject_id = int(form.cleaned_data['subject'])
  323. subject_init = subject_id_dict.get(subject_id)
  324. if not subject_init:
  325. subject_init = 'math'
  326. upload_img_list = request.FILES.getlist('img_data')
  327. res_info_list = []
  328. image_list = []
  329. detail_xml = []
  330. ocr_list = []
  331. error_info = ''
  332. is_success = 1
  333. save_path = ''
  334. image = ''
  335. series_number = ''
  336. for img_index, img in enumerate(upload_img_list):
  337. subject = subject_init
  338. start_time = time.time()
  339. raw_name = img.name
  340. try:
  341. save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
  342. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  343. img_instance = SheetBigBoxes(series_number=series_number,
  344. raw_name=raw_name, save_path=save_path,
  345. subject_id=subject_id, subject=subject)
  346. img_instance.save()
  347. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  348. except Exception as e:
  349. traceback.print_exc()
  350. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  351. try:
  352. try:
  353. ocr_res = get_ocr_text_and_coordinate(image)
  354. except Exception as e:
  355. traceback.print_exc()
  356. error_info = error_info + 'ocr error;'
  357. ocr_res = ''
  358. ocr_list.append(ocr_res)
  359. if subject not in ["chinese", "science_comprehensive", "arts_comprehensive"]:
  360. sheet_sess = tf_sess_dict[subject]
  361. answered = "fixed"
  362. elif decide_blank_sheet(image):
  363. sheet_sess = tf_sess_dict[subject + '_blank']
  364. subject = subject + '_blank'
  365. answered = "blank"
  366. else:
  367. sheet_sess = tf_sess_dict[subject]
  368. answered = "answered"
  369. status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  370. save_path, subject,
  371. sheet_sess, ocr=ocr_res)
  372. # print(sheet_dict)
  373. sheet_dict['series_number'] = series_number
  374. sheet_dict["img_name"] = raw_name
  375. sheet_dict["answered"] = answered
  376. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  377. small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  378. detail_xml.append(small_box_xml_path)
  379. shutil.copy(raw_xml_path, small_box_xml_path)
  380. try:
  381. # 找题号
  382. small_box_sheet_dict = sheet_detail_resolve(image, sheet_dict, small_box_xml_path, shrink=True)
  383. except Exception as e:
  384. traceback.print_exc()
  385. status = 0
  386. small_box_sheet_dict = sheet_dict
  387. SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=small_box_xml_path)
  388. is_success = status
  389. end_time = time.time()
  390. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  391. small_box_sheet_dict.update({'cost_time': cost_time})
  392. res_info_list.append(small_box_sheet_dict)
  393. image_list.append(image)
  394. except Exception as e:
  395. traceback.print_exc()
  396. logger.info('{}试卷: {} 解析失败: {}'.format(subject, raw_name, e))
  397. is_success = 0
  398. error_info = error_info + 'box resolve error;'
  399. try:
  400. res_info_list = sheet_points(res_info_list, image_list, ocr_list, if_ocr=False)
  401. except Exception as e:
  402. print(e)
  403. traceback.print_exc()
  404. error_info = error_info + 'number and points error;'
  405. try:
  406. init_number = 500
  407. crt_numbers = []
  408. for i, ele in enumerate(res_info_list):
  409. image = image_list[i]
  410. res, init_number, crt_numbers = sheet_format_output(init_number, crt_numbers,
  411. ele, image, subject_init,
  412. shrink=True)
  413. res_info_list[i] = res
  414. except Exception as e:
  415. print(e)
  416. traceback.print_exc()
  417. error_info = error_info + 'format output error ;'
  418. try:
  419. for image_index, image in enumerate(image_list):
  420. anchor_list = sheet_anchor(image)
  421. res_info_list[image_index]['regions'] = res_info_list[image_index]['regions'] + anchor_list
  422. except Exception as e:
  423. print(e)
  424. traceback.print_exc()
  425. error_info = error_info + 'anchor error;'
  426. res = {'is_success': is_success, 'imgs_info': res_info_list}
  427. try:
  428. for xml_index, ele in enumerate(res_info_list):
  429. regions = ele['regions']
  430. gen_xml(regions, detail_xml[xml_index])
  431. except Exception as e:
  432. print(e)
  433. traceback.print_exc()
  434. if error_info:
  435. res['error'] = error_info
  436. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  437. logger.info('segment_info: {}\n'.format(res_json))
  438. print('sheet resolve done')
  439. return HttpResponse(res_json)
  440. else:
  441. error_json = form.errors.as_json()
  442. is_success = 99
  443. res = {'is_success': is_success, 'error': error_json}
  444. return HttpResponse('{}'.format(res))
  445. else:
  446. form_img = UploadImageForm()
  447. form_xml = UploadFileForm()
  448. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  449. @csrf_exempt
  450. def saas_analysis_box_once_with_multiple_img(request):
  451. if request.method == 'POST':
  452. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  453. form = UploadImageForm(request.POST, request.FILES)
  454. if form.is_valid():
  455. subject_id = int(form.cleaned_data['subject'])
  456. subject = subject_id_dict.get(subject_id)
  457. if not subject:
  458. subject = 'unknown_subject'
  459. upload_img_list = request.FILES.getlist('img_data')
  460. res_info_list = []
  461. image_list = []
  462. detail_xml = []
  463. ocr_list = []
  464. error_info = ''
  465. is_success = 1
  466. save_path = ''
  467. image = ''
  468. series_number = ''
  469. for img_index, img in enumerate(upload_img_list):
  470. start_time = time.time()
  471. raw_name = img.name
  472. try:
  473. save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
  474. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  475. img_instance = SheetBigBoxes(series_number=series_number,
  476. raw_name=raw_name, save_path=save_path,
  477. subject_id=subject_id, subject=subject)
  478. img_instance.save()
  479. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  480. except Exception as e:
  481. # traceback.print_exc()
  482. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  483. try:
  484. status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  485. save_path, subject,
  486. tf_sess_dict[subject])
  487. sheet_dict['series_number'] = series_number
  488. sheet_dict["img_name"] = raw_name
  489. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  490. small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  491. detail_xml.append(small_box_xml_path)
  492. shutil.copy(raw_xml_path, small_box_xml_path)
  493. small_box_sheet_dict = sheet_detail_resolve(image, sheet_dict, small_box_xml_path, shrink=False)
  494. SheetBigBoxes.objects.filter(series_number=series_number) \
  495. .update(small_box_path=small_box_xml_path)
  496. is_success = status
  497. end_time = time.time()
  498. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  499. small_box_sheet_dict.update({'cost_time': cost_time})
  500. res_info_list.append(small_box_sheet_dict)
  501. image_list.append(image)
  502. ocr_res = get_ocr_text_and_coordinate(image)
  503. ocr_list.append(ocr_res)
  504. except Exception as e:
  505. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  506. is_success = 0
  507. error_info = error_info + 'box resolve error;'
  508. try:
  509. res_info_list = sheet_points(res_info_list, image_list, ocr_list, if_ocr=True)
  510. except Exception as e:
  511. print(e)
  512. traceback.print_exc()
  513. is_success = 0
  514. error_info = error_info + 'number and points error;'
  515. res_info_list = [ele.update({'ocr': ocr_list[i]}) for i, ele in enumerate(res_info_list)]
  516. for i, ele in enumerate(res_info_list):
  517. image = image_list[i]
  518. res = sheet_format_output(ele, image, subject, shrink=True)
  519. res_info_list[i] = res
  520. res = {'is_success': is_success, 'imgs_info': res_info_list}
  521. for xml_index, ele in enumerate(res_info_list):
  522. regions = ele['regions']
  523. gen_xml(regions, detail_xml[xml_index])
  524. if error_info:
  525. res['error'] = error_info
  526. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  527. # logger.info('segment_info: {}\n'.format(res_json))
  528. print('sheet resolve done')
  529. return HttpResponse(res_json)
  530. else:
  531. error_json = form.errors.as_json()
  532. is_success = 99
  533. res = {'is_success': is_success, 'error': error_json}
  534. return HttpResponse('{}'.format(res))
  535. else:
  536. form_img = UploadImageForm()
  537. form_xml = UploadFileForm()
  538. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  539. @csrf_exempt
  540. def analysis_box_once_and_single_img(request):
  541. if request.method == 'POST':
  542. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  543. form = UploadImageForm(request.POST, request.FILES)
  544. if form.is_valid():
  545. subject_id = int(form.cleaned_data['subject'])
  546. subject = subject_id_dict.get(subject_id)
  547. if not subject:
  548. subject = 'unknown_subject'
  549. upload_img = request.FILES.get('img_data')
  550. error_info = ''
  551. detail_box_sheet_dict = dict()
  552. try:
  553. save_path = ''
  554. image = ''
  555. series_number = ''
  556. start_time = time.time()
  557. raw_name = upload_img.name
  558. try:
  559. save_path, image, _ = save_raw_image_without_segment(subject, time_str, upload_img, 'sheet')
  560. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  561. img_instance = SheetBigBoxes(series_number=series_number,
  562. raw_name=raw_name, save_path=save_path,
  563. subject_id=subject_id, subject=subject)
  564. img_instance.save()
  565. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  566. except Exception as e:
  567. # traceback.print_exc()
  568. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  569. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image, save_path, subject,
  570. tf_sess_dict[subject])
  571. bbox_info['series_number'] = series_number
  572. bbox_info["img_name"] = raw_name
  573. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  574. detail_box_xml_path = raw_xml_path.replace('.xml', '_detail.xml')
  575. shutil.copy(raw_xml_path, detail_box_xml_path)
  576. sheet_dict = bbox_info
  577. # small_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
  578. # tf_sess_dict['choice_m'], tf_sess_dict['cloze'],
  579. # small_box_xml_path)
  580. detail_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
  581. '', tf_sess_dict['cloze'],
  582. detail_box_xml_path)
  583. SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=detail_box_xml_path)
  584. is_success = status
  585. # end_time = time.time()
  586. # cost_time = '{:.2f}s'.format(float(end_time - start_time))
  587. # small_box_sheet_dict.update({'cost_time': cost_time})
  588. except Exception as e:
  589. logger.info('box resolve error: {}'.format(e))
  590. is_success = 0
  591. error_info = 'box resolve error'
  592. detail_box_sheet_dict.update({'is_success': is_success})
  593. if error_info:
  594. detail_box_sheet_dict.update({'is_success': is_success, 'error': error_info})
  595. res_json = json.dumps(detail_box_sheet_dict, ensure_ascii=False)
  596. # logger.info('segment_info: {}\n'.format(res_json))
  597. return HttpResponse(res_json)
  598. else:
  599. error_json = form.errors.as_json()
  600. is_success = 99
  601. res = {'is_success': is_success, 'error': error_json}
  602. return HttpResponse('{}'.format(res))
  603. else:
  604. form_img = UploadImageForm()
  605. form_xml = UploadFileForm()
  606. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  607. @csrf_exempt
  608. def analysis_box_once_and_single_img_with_paperid(request):
  609. if request.method == 'POST':
  610. form = UploadImageWithPaperIdForm(request.POST, request.FILES)
  611. if form.is_valid():
  612. subject_id = int(form.cleaned_data['subject'])
  613. subject = subject_id_dict.get(subject_id)
  614. if not subject:
  615. subject = 'unknown_subject'
  616. upload_img = request.FILES.get('img_data')
  617. paper_id = form.cleaned_data['paper_id']
  618. error_info = ''
  619. small_box_sheet_dict = dict()
  620. try:
  621. save_path = ''
  622. image = ''
  623. start_time = time.time()
  624. raw_name = upload_img.name
  625. try:
  626. save_path, image, image_url = save_raw_image_with_paper_id(subject, paper_id, upload_img, 'sheet')
  627. # series_number = generate_serial_number(time_str.replace('-', ''), SheetBoxes)
  628. img_instance = SheetBoxes(paper_id=paper_id,
  629. raw_name=raw_name, save_path=save_path,
  630. subject_id=subject_id, subject=subject,
  631. download_path=image_url)
  632. img_instance.save()
  633. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  634. except Exception as e:
  635. # traceback.print_exc()
  636. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  637. sheet_sess = tf_sess_dict[subject]
  638. status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(paper_id, image, save_path, subject,
  639. sheet_sess)
  640. sheet_dict['series_number'] = paper_id
  641. sheet_dict["img_name"] = raw_name
  642. # small_box_sheet_dict = sheet_small_boxes_resolve(image, sheet_dict,
  643. # tf_sess_dict['choice'], tf_sess_dict['cloze'],
  644. # raw_xml_path)
  645. small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  646. shutil.copy(raw_xml_path, small_box_xml_path)
  647. small_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
  648. '', tf_sess_dict['cloze'],
  649. small_box_xml_path)
  650. SheetBoxes.objects.filter(paper_id=paper_id).update(xml_box_path=small_box_xml_path)
  651. is_success = status
  652. end_time = time.time()
  653. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  654. small_box_sheet_dict.update({'cost_time': cost_time})
  655. except Exception as e:
  656. logger.info('box resolve error: {}'.format(e))
  657. is_success = 0
  658. error_info = 'box resolve error'
  659. small_box_sheet_dict.update({'is_success': is_success})
  660. if error_info:
  661. small_box_sheet_dict.update({'is_success': is_success, 'error': error_info})
  662. res_json = json.dumps(small_box_sheet_dict, ensure_ascii=False)
  663. # logger.info('segment_info: {}\n'.format(res_json))
  664. return HttpResponse(res_json)
  665. else:
  666. error_json = form.errors.as_json()
  667. is_success = 99
  668. res = {'is_success': is_success, 'error': error_json}
  669. return HttpResponse('{}'.format(res))
  670. else:
  671. form = UploadImageWithPaperIdForm()
  672. return render(request, 'sheet_with_id.html', {'form': form})
  673. @csrf_exempt
  674. def analysis_box_detail(request):
  675. if request.method == 'POST':
  676. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  677. form = UploadImageForm(request.POST, request.FILES)
  678. if form.is_valid():
  679. subject_id = int(form.cleaned_data['subject'])
  680. subject = subject_id_dict.get(subject_id)
  681. if not subject:
  682. subject = 'unknown_subject'
  683. upload_img = request.FILES.get('img_data')
  684. error_info = ''
  685. sheet_dict = dict()
  686. try:
  687. save_path = ''
  688. image = ''
  689. series_number = ''
  690. raw_name = upload_img.name
  691. try:
  692. save_path, image, _ = save_raw_image_without_segment(subject, time_str, upload_img, 'sheet') # 保存文件
  693. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  694. img_instance = SheetBigBoxes(series_number=series_number,
  695. raw_name=raw_name, save_path=save_path,
  696. subject_id=subject_id, subject=subject)
  697. img_instance.save()
  698. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  699. except Exception as e:
  700. # traceback.print_exc()
  701. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  702. sheet_sess = tf_sess_dict[subject]
  703. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  704. save_path, subject, sheet_sess)
  705. bbox_info['series_number'] = series_number
  706. bbox_info["img_name"] = raw_name
  707. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  708. # small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  709. # shutil.copy(raw_xml_path, small_box_xml_path)
  710. sheet_dict = bbox_info
  711. is_success = status
  712. except Exception as e:
  713. logger.info('box resolve error: {}'.format(e))
  714. is_success = 0
  715. error_info = 'box resolve error'
  716. sheet_dict.update({'is_success': is_success})
  717. if error_info:
  718. sheet_dict.update({'is_success': is_success, 'error': error_info})
  719. res_json = json.dumps(sheet_dict, ensure_ascii=False)
  720. # logger.info('segment_info: {}\n'.format(res_json))
  721. return HttpResponse(res_json)
  722. else:
  723. error_json = form.errors.as_json()
  724. is_success = 99
  725. res = {'is_success': is_success, 'error': error_json}
  726. return HttpResponse('{}'.format(res))
  727. else:
  728. form_img = UploadImageForm()
  729. form_xml = UploadFileForm()
  730. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  731. @csrf_exempt
  732. def analysis_box_detail_and_show(request):
  733. if request.method == 'POST':
  734. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  735. form = UploadImageForm(request.POST, request.FILES)
  736. if form.is_valid():
  737. subject_id = int(form.cleaned_data['subject'])
  738. subject = subject_id_dict.get(subject_id)
  739. if not subject:
  740. subject = 'unknown_subject'
  741. upload_img = request.FILES.get('img_data')
  742. error_info = ''
  743. img_url_path = ''
  744. sheet_dict = dict()
  745. height, width = 1200, 1600
  746. try:
  747. save_path = ''
  748. image = ''
  749. series_number = ''
  750. raw_name = upload_img.name
  751. try:
  752. save_path, image, img_url_path = save_raw_image_without_segment(subject, time_str, upload_img,
  753. 'sheet')
  754. # small_img_path = save_path.replace('.jpg', '_small.jpg').replace('\\', '/')
  755. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  756. img_instance = SheetBigBoxes(series_number=series_number,
  757. raw_name=raw_name, save_path=save_path,
  758. subject_id=subject_id, subject=subject)
  759. img_instance.save()
  760. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  761. except Exception as e:
  762. # traceback.print_exc()
  763. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  764. sheet_sess = tf_sess_dict[subject]
  765. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  766. save_path, subject, sheet_sess)
  767. bbox_info['series_number'] = series_number
  768. bbox_info["img_name"] = raw_name
  769. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  770. # small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  771. # shutil.copy(raw_xml_path, small_box_xml_path)
  772. sheet_dict = bbox_info
  773. is_success = status
  774. show_ratio = 0.4
  775. img_small = resize_by_percent(image, show_ratio)
  776. font = cv2.FONT_HERSHEY_COMPLEX
  777. for ele in bbox_info['regions']:
  778. name = ele['class_name']
  779. xmin = int(int(ele['bounding_box']['xmin']) * show_ratio)
  780. ymin = int(int(ele['bounding_box']['ymin']) * show_ratio)
  781. xmax = int(int(ele['bounding_box']['xmax']) * show_ratio)
  782. ymax = int(int(ele['bounding_box']['ymax']) * show_ratio)
  783. cv2.rectangle(img_small, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0), 1)
  784. cv2.putText(img_small, name, (xmin, ymax), font, 0.7, (255, 106, 106), 2, False)
  785. height, width = img_small.shape[0], img_small.shape[1]
  786. line_xmax1, line_xmax2 = subfield_answer_sheet(image, bbox_info)
  787. if line_xmax1 != 0 and line_xmax2 != 0:
  788. line_xmax1_1 = int(line_xmax1 * show_ratio)
  789. line_xmax2_1 = int(line_xmax2 * show_ratio)
  790. cv2.rectangle(img_small, (20, 20), (line_xmax1_1, height - 20), (0, 0, 255), 1)
  791. cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
  792. cv2.rectangle(img_small, (line_xmax1_1, 20), (line_xmax2_1, height - 20), (0, 0, 255), 1)
  793. cv2.putText(img_small, 'subfield_line', (line_xmax1_1, height - 20), font, 0.7, (0, 0, 255), 2,
  794. False)
  795. cv2.rectangle(img_small, (line_xmax2_1, 20), (width - 20, height - 20), (0, 0, 255), 1)
  796. cv2.putText(img_small, 'subfield_line', (line_xmax2_1, height - 20), font, 0.7, (0, 0, 255), 2,
  797. False)
  798. elif line_xmax1 != 0 and line_xmax2 == 0:
  799. if int(line_xmax1 * show_ratio) == width:
  800. line_xmax1_1 = int(line_xmax1 * show_ratio)
  801. cv2.rectangle(img_small, (20, 20), (line_xmax1_1 - 20, height - 20), (0, 0, 255), 1)
  802. cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
  803. else:
  804. line_xmax1_1 = int(line_xmax1 * show_ratio)
  805. cv2.rectangle(img_small, (20, 20), (line_xmax1_1, height - 20), (0, 0, 255), 1)
  806. cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
  807. cv2.rectangle(img_small, (line_xmax1_1, 20), (width - 20, height - 20), (0, 0, 255), 1)
  808. cv2.putText(img_small, 'subfield_line', (line_xmax1_1, height - 20), font, 0.7, (0, 0, 255), 2,
  809. False)
  810. write_single_img(img_small, save_path)
  811. height, width = img_small.shape[0], img_small.shape[1]
  812. except Exception as e:
  813. logger.info('box resolve error: {}'.format(e))
  814. is_success = 0
  815. error_info = 'box resolve error'
  816. sheet_dict.update({'is_success': is_success})
  817. if error_info:
  818. sheet_dict.update({'is_success': is_success, 'error': error_info})
  819. res_json = json.dumps(sheet_dict, ensure_ascii=False)
  820. # logger.info('segment_info: {}\n'.format(res_json))
  821. res_dict = {'url': img_url_path,
  822. 'img_height': height,
  823. 'text_height': 0.9 * height,
  824. 'texts': res_json,
  825. 'raw_texts': ''
  826. }
  827. return render(request, 'showimg.html', res_dict)
  828. # return HttpResponse(res_json)
  829. else:
  830. error_json = form.errors.as_json()
  831. is_success = 99
  832. res = {'is_success': is_success, 'error': error_json}
  833. return HttpResponse('{}'.format(res))
  834. else:
  835. form_img = UploadImageForm()
  836. form_xml = UploadFileForm()
  837. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  838. @csrf_exempt
  839. def download_image(request):
  840. if request.method == 'POST':
  841. form = DownloadImage(request.POST, request.FILES)
  842. if form.is_valid():
  843. is_success = 1
  844. try:
  845. paper_id = form.cleaned_data['paper_id']
  846. # download_path = SheetBoxes.objects.get(paper_id=paper_id).download_path
  847. paper_id_obj = SheetBoxes.objects.filter(paper_id__icontains=paper_id)
  848. paper_id_list = [ele.paper_id for ele in paper_id_obj]
  849. download_path_list = [SheetBoxes.objects.get(paper_id=ele).download_path for ele in paper_id_list]
  850. res_dict = {'paper_id': paper_id_list,
  851. 'image_url': download_path_list,
  852. # 'img_height': 1000,
  853. # 'text_height': 10,
  854. # 'texts': download_path,
  855. # 'raw_texts': '',
  856. 'is_success': is_success
  857. }
  858. except Exception as e:
  859. logger.info('image address load failed: {}'.format(e))
  860. is_success = 0
  861. error_info = 'image address load failed'
  862. res_dict = {'is_success': is_success, 'error': error_info}
  863. res_json = json.dumps(res_dict, ensure_ascii=False)
  864. # return render(request, 'showimg.html', res_dict)
  865. return HttpResponse(res_json)
  866. else:
  867. error_json = form.errors.as_json()
  868. is_success = 99
  869. res = {'is_success': is_success, 'error': error_json}
  870. return HttpResponse('{}'.format(res))
  871. else:
  872. form = DownloadImage()
  873. return render(request, 'downloadimg.html', {'form': form})