sheet_views.py 47 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  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. logger.info('before format: {}\n'.format(res_info_list))
  407. init_number = 500
  408. crt_numbers = []
  409. for i, ele in enumerate(res_info_list):
  410. image = image_list[i]
  411. res, init_number, crt_numbers = sheet_format_output(init_number, crt_numbers,
  412. ele, image, subject_init,
  413. shrink=True)
  414. res_info_list[i] = res
  415. except Exception as e:
  416. print(e)
  417. traceback.print_exc()
  418. error_info = error_info + 'format output error ;'
  419. try:
  420. for image_index, image in enumerate(image_list):
  421. anchor_list = sheet_anchor(image)
  422. res_info_list[image_index]['regions'] = res_info_list[image_index]['regions'] + anchor_list
  423. except Exception as e:
  424. print(e)
  425. traceback.print_exc()
  426. error_info = error_info + 'anchor error;'
  427. res = {'is_success': is_success, 'imgs_info': res_info_list}
  428. try:
  429. for xml_index, ele in enumerate(res_info_list):
  430. regions = ele['regions']
  431. gen_xml(regions, detail_xml[xml_index])
  432. except Exception as e:
  433. print(e)
  434. traceback.print_exc()
  435. if error_info:
  436. res['error'] = error_info
  437. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  438. logger.info('segment_info: {}\n'.format(res_json))
  439. print('sheet resolve done')
  440. return HttpResponse(res_json)
  441. else:
  442. error_json = form.errors.as_json()
  443. is_success = 99
  444. res = {'is_success': is_success, 'error': error_json}
  445. return HttpResponse('{}'.format(res))
  446. else:
  447. form_img = UploadImageForm()
  448. form_xml = UploadFileForm()
  449. return render(request, 'sheet.html', {'form_img': form_img, 'form_xml': form_xml})
  450. @csrf_exempt
  451. def saas_analysis_box_once_with_multiple_img(request):
  452. if request.method == 'POST':
  453. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  454. form = UploadImageForm(request.POST, request.FILES)
  455. if form.is_valid():
  456. subject_id = int(form.cleaned_data['subject'])
  457. subject = subject_id_dict.get(subject_id)
  458. if not subject:
  459. subject = 'unknown_subject'
  460. upload_img_list = request.FILES.getlist('img_data')
  461. res_info_list = []
  462. image_list = []
  463. detail_xml = []
  464. ocr_list = []
  465. error_info = ''
  466. is_success = 1
  467. save_path = ''
  468. image = ''
  469. series_number = ''
  470. for img_index, img in enumerate(upload_img_list):
  471. start_time = time.time()
  472. raw_name = img.name
  473. try:
  474. save_path, image, _ = save_raw_image_without_segment(subject, time_str, img, 'sheet')
  475. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  476. img_instance = SheetBigBoxes(series_number=series_number,
  477. raw_name=raw_name, save_path=save_path,
  478. subject_id=subject_id, subject=subject)
  479. img_instance.save()
  480. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  481. except Exception as e:
  482. # traceback.print_exc()
  483. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  484. try:
  485. status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  486. save_path, subject,
  487. tf_sess_dict[subject])
  488. sheet_dict['series_number'] = series_number
  489. sheet_dict["img_name"] = raw_name
  490. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  491. small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  492. detail_xml.append(small_box_xml_path)
  493. shutil.copy(raw_xml_path, small_box_xml_path)
  494. small_box_sheet_dict = sheet_detail_resolve(image, sheet_dict, small_box_xml_path, shrink=False)
  495. SheetBigBoxes.objects.filter(series_number=series_number) \
  496. .update(small_box_path=small_box_xml_path)
  497. is_success = status
  498. end_time = time.time()
  499. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  500. small_box_sheet_dict.update({'cost_time': cost_time})
  501. res_info_list.append(small_box_sheet_dict)
  502. image_list.append(image)
  503. ocr_res = get_ocr_text_and_coordinate(image)
  504. ocr_list.append(ocr_res)
  505. except Exception as e:
  506. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  507. is_success = 0
  508. error_info = error_info + 'box resolve error;'
  509. try:
  510. res_info_list = sheet_points(res_info_list, image_list, ocr_list, if_ocr=True)
  511. except Exception as e:
  512. print(e)
  513. traceback.print_exc()
  514. is_success = 0
  515. error_info = error_info + 'number and points error;'
  516. res_info_list = [ele.update({'ocr': ocr_list[i]}) for i, ele in enumerate(res_info_list)]
  517. for i, ele in enumerate(res_info_list):
  518. image = image_list[i]
  519. res = sheet_format_output(ele, image, subject, shrink=True)
  520. res_info_list[i] = res
  521. res = {'is_success': is_success, 'imgs_info': res_info_list}
  522. for xml_index, ele in enumerate(res_info_list):
  523. regions = ele['regions']
  524. gen_xml(regions, detail_xml[xml_index])
  525. if error_info:
  526. res['error'] = error_info
  527. res_json = json.dumps(res, ensure_ascii=False, cls=NpEncoder)
  528. # logger.info('segment_info: {}\n'.format(res_json))
  529. print('sheet resolve done')
  530. return HttpResponse(res_json)
  531. else:
  532. error_json = form.errors.as_json()
  533. is_success = 99
  534. res = {'is_success': is_success, 'error': error_json}
  535. return HttpResponse('{}'.format(res))
  536. else:
  537. form_img = UploadImageForm()
  538. form_xml = UploadFileForm()
  539. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  540. @csrf_exempt
  541. def analysis_box_once_and_single_img(request):
  542. if request.method == 'POST':
  543. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  544. form = UploadImageForm(request.POST, request.FILES)
  545. if form.is_valid():
  546. subject_id = int(form.cleaned_data['subject'])
  547. subject = subject_id_dict.get(subject_id)
  548. if not subject:
  549. subject = 'unknown_subject'
  550. upload_img = request.FILES.get('img_data')
  551. error_info = ''
  552. detail_box_sheet_dict = dict()
  553. try:
  554. save_path = ''
  555. image = ''
  556. series_number = ''
  557. start_time = time.time()
  558. raw_name = upload_img.name
  559. try:
  560. save_path, image, _ = save_raw_image_without_segment(subject, time_str, upload_img, 'sheet')
  561. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  562. img_instance = SheetBigBoxes(series_number=series_number,
  563. raw_name=raw_name, save_path=save_path,
  564. subject_id=subject_id, subject=subject)
  565. img_instance.save()
  566. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  567. except Exception as e:
  568. # traceback.print_exc()
  569. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  570. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image, save_path, subject,
  571. tf_sess_dict[subject])
  572. bbox_info['series_number'] = series_number
  573. bbox_info["img_name"] = raw_name
  574. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  575. detail_box_xml_path = raw_xml_path.replace('.xml', '_detail.xml')
  576. shutil.copy(raw_xml_path, detail_box_xml_path)
  577. sheet_dict = bbox_info
  578. # small_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
  579. # tf_sess_dict['choice_m'], tf_sess_dict['cloze'],
  580. # small_box_xml_path)
  581. detail_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
  582. '', tf_sess_dict['cloze'],
  583. detail_box_xml_path)
  584. SheetBigBoxes.objects.filter(series_number=series_number).update(small_box_path=detail_box_xml_path)
  585. is_success = status
  586. # end_time = time.time()
  587. # cost_time = '{:.2f}s'.format(float(end_time - start_time))
  588. # small_box_sheet_dict.update({'cost_time': cost_time})
  589. except Exception as e:
  590. logger.info('box resolve error: {}'.format(e))
  591. is_success = 0
  592. error_info = 'box resolve error'
  593. detail_box_sheet_dict.update({'is_success': is_success})
  594. if error_info:
  595. detail_box_sheet_dict.update({'is_success': is_success, 'error': error_info})
  596. res_json = json.dumps(detail_box_sheet_dict, ensure_ascii=False)
  597. # logger.info('segment_info: {}\n'.format(res_json))
  598. return HttpResponse(res_json)
  599. else:
  600. error_json = form.errors.as_json()
  601. is_success = 99
  602. res = {'is_success': is_success, 'error': error_json}
  603. return HttpResponse('{}'.format(res))
  604. else:
  605. form_img = UploadImageForm()
  606. form_xml = UploadFileForm()
  607. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  608. @csrf_exempt
  609. def analysis_box_once_and_single_img_with_paperid(request):
  610. if request.method == 'POST':
  611. form = UploadImageWithPaperIdForm(request.POST, request.FILES)
  612. if form.is_valid():
  613. subject_id = int(form.cleaned_data['subject'])
  614. subject = subject_id_dict.get(subject_id)
  615. if not subject:
  616. subject = 'unknown_subject'
  617. upload_img = request.FILES.get('img_data')
  618. paper_id = form.cleaned_data['paper_id']
  619. error_info = ''
  620. small_box_sheet_dict = dict()
  621. try:
  622. save_path = ''
  623. image = ''
  624. start_time = time.time()
  625. raw_name = upload_img.name
  626. try:
  627. save_path, image, image_url = save_raw_image_with_paper_id(subject, paper_id, upload_img, 'sheet')
  628. # series_number = generate_serial_number(time_str.replace('-', ''), SheetBoxes)
  629. img_instance = SheetBoxes(paper_id=paper_id,
  630. raw_name=raw_name, save_path=save_path,
  631. subject_id=subject_id, subject=subject,
  632. download_path=image_url)
  633. img_instance.save()
  634. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  635. except Exception as e:
  636. # traceback.print_exc()
  637. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  638. sheet_sess = tf_sess_dict[subject]
  639. status, sheet_dict, raw_xml_path = sheet_big_boxes_resolve(paper_id, image, save_path, subject,
  640. sheet_sess)
  641. sheet_dict['series_number'] = paper_id
  642. sheet_dict["img_name"] = raw_name
  643. # small_box_sheet_dict = sheet_small_boxes_resolve(image, sheet_dict,
  644. # tf_sess_dict['choice'], tf_sess_dict['cloze'],
  645. # raw_xml_path)
  646. small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  647. shutil.copy(raw_xml_path, small_box_xml_path)
  648. small_box_sheet_dict = sheet_row_col_resolve(image, sheet_dict,
  649. '', tf_sess_dict['cloze'],
  650. small_box_xml_path)
  651. SheetBoxes.objects.filter(paper_id=paper_id).update(xml_box_path=small_box_xml_path)
  652. is_success = status
  653. end_time = time.time()
  654. cost_time = '{:.2f}s'.format(float(end_time - start_time))
  655. small_box_sheet_dict.update({'cost_time': cost_time})
  656. except Exception as e:
  657. logger.info('box resolve error: {}'.format(e))
  658. is_success = 0
  659. error_info = 'box resolve error'
  660. small_box_sheet_dict.update({'is_success': is_success})
  661. if error_info:
  662. small_box_sheet_dict.update({'is_success': is_success, 'error': error_info})
  663. res_json = json.dumps(small_box_sheet_dict, ensure_ascii=False)
  664. # logger.info('segment_info: {}\n'.format(res_json))
  665. return HttpResponse(res_json)
  666. else:
  667. error_json = form.errors.as_json()
  668. is_success = 99
  669. res = {'is_success': is_success, 'error': error_json}
  670. return HttpResponse('{}'.format(res))
  671. else:
  672. form = UploadImageWithPaperIdForm()
  673. return render(request, 'sheet_with_id.html', {'form': form})
  674. @csrf_exempt
  675. def analysis_box_detail(request):
  676. if request.method == 'POST':
  677. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  678. form = UploadImageForm(request.POST, request.FILES)
  679. if form.is_valid():
  680. subject_id = int(form.cleaned_data['subject'])
  681. subject = subject_id_dict.get(subject_id)
  682. if not subject:
  683. subject = 'unknown_subject'
  684. upload_img = request.FILES.get('img_data')
  685. error_info = ''
  686. sheet_dict = dict()
  687. try:
  688. save_path = ''
  689. image = ''
  690. series_number = ''
  691. raw_name = upload_img.name
  692. try:
  693. save_path, image, _ = save_raw_image_without_segment(subject, time_str, upload_img, 'sheet') # 保存文件
  694. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  695. img_instance = SheetBigBoxes(series_number=series_number,
  696. raw_name=raw_name, save_path=save_path,
  697. subject_id=subject_id, subject=subject)
  698. img_instance.save()
  699. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  700. except Exception as e:
  701. # traceback.print_exc()
  702. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  703. sheet_sess = tf_sess_dict[subject]
  704. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  705. save_path, subject, sheet_sess)
  706. bbox_info['series_number'] = series_number
  707. bbox_info["img_name"] = raw_name
  708. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  709. # small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  710. # shutil.copy(raw_xml_path, small_box_xml_path)
  711. sheet_dict = bbox_info
  712. is_success = status
  713. except Exception as e:
  714. logger.info('box resolve error: {}'.format(e))
  715. is_success = 0
  716. error_info = 'box resolve error'
  717. sheet_dict.update({'is_success': is_success})
  718. if error_info:
  719. sheet_dict.update({'is_success': is_success, 'error': error_info})
  720. res_json = json.dumps(sheet_dict, ensure_ascii=False)
  721. # logger.info('segment_info: {}\n'.format(res_json))
  722. return HttpResponse(res_json)
  723. else:
  724. error_json = form.errors.as_json()
  725. is_success = 99
  726. res = {'is_success': is_success, 'error': error_json}
  727. return HttpResponse('{}'.format(res))
  728. else:
  729. form_img = UploadImageForm()
  730. form_xml = UploadFileForm()
  731. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  732. @csrf_exempt
  733. def analysis_box_detail_and_show(request):
  734. if request.method == 'POST':
  735. time_str = time.strftime('%Y-%m-%d', time.localtime(time.time()))
  736. form = UploadImageForm(request.POST, request.FILES)
  737. if form.is_valid():
  738. subject_id = int(form.cleaned_data['subject'])
  739. subject = subject_id_dict.get(subject_id)
  740. if not subject:
  741. subject = 'unknown_subject'
  742. upload_img = request.FILES.get('img_data')
  743. error_info = ''
  744. img_url_path = ''
  745. sheet_dict = dict()
  746. height, width = 1200, 1600
  747. try:
  748. save_path = ''
  749. image = ''
  750. series_number = ''
  751. raw_name = upload_img.name
  752. try:
  753. save_path, image, img_url_path = save_raw_image_without_segment(subject, time_str, upload_img,
  754. 'sheet')
  755. # small_img_path = save_path.replace('.jpg', '_small.jpg').replace('\\', '/')
  756. series_number = generate_serial_number(time_str.replace('-', ''), SheetBigBoxes)
  757. img_instance = SheetBigBoxes(series_number=series_number,
  758. raw_name=raw_name, save_path=save_path,
  759. subject_id=subject_id, subject=subject)
  760. img_instance.save()
  761. logger.info('{}试卷: {} 存储成功: {}'.format(subject, raw_name, save_path))
  762. except Exception as e:
  763. # traceback.print_exc()
  764. logger.info('{}试卷: {} 存储失败: {}'.format(subject, raw_name, e))
  765. sheet_sess = tf_sess_dict[subject]
  766. status, bbox_info, raw_xml_path = sheet_big_boxes_resolve(series_number, image,
  767. save_path, subject, sheet_sess)
  768. bbox_info['series_number'] = series_number
  769. bbox_info["img_name"] = raw_name
  770. SheetBigBoxes.objects.filter(series_number=series_number).update(raw_big_box_path=raw_xml_path)
  771. # small_box_xml_path = raw_xml_path.replace('.xml', '_small.xml')
  772. # shutil.copy(raw_xml_path, small_box_xml_path)
  773. sheet_dict = bbox_info
  774. is_success = status
  775. show_ratio = 0.4
  776. img_small = resize_by_percent(image, show_ratio)
  777. font = cv2.FONT_HERSHEY_COMPLEX
  778. for ele in bbox_info['regions']:
  779. name = ele['class_name']
  780. xmin = int(int(ele['bounding_box']['xmin']) * show_ratio)
  781. ymin = int(int(ele['bounding_box']['ymin']) * show_ratio)
  782. xmax = int(int(ele['bounding_box']['xmax']) * show_ratio)
  783. ymax = int(int(ele['bounding_box']['ymax']) * show_ratio)
  784. cv2.rectangle(img_small, (int(xmin), int(ymin)), (int(xmax), int(ymax)), (0, 255, 0), 1)
  785. cv2.putText(img_small, name, (xmin, ymax), font, 0.7, (255, 106, 106), 2, False)
  786. height, width = img_small.shape[0], img_small.shape[1]
  787. line_xmax1, line_xmax2 = subfield_answer_sheet(image, bbox_info)
  788. if line_xmax1 != 0 and line_xmax2 != 0:
  789. line_xmax1_1 = int(line_xmax1 * show_ratio)
  790. line_xmax2_1 = int(line_xmax2 * show_ratio)
  791. cv2.rectangle(img_small, (20, 20), (line_xmax1_1, height - 20), (0, 0, 255), 1)
  792. cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
  793. cv2.rectangle(img_small, (line_xmax1_1, 20), (line_xmax2_1, height - 20), (0, 0, 255), 1)
  794. cv2.putText(img_small, 'subfield_line', (line_xmax1_1, height - 20), font, 0.7, (0, 0, 255), 2,
  795. False)
  796. cv2.rectangle(img_small, (line_xmax2_1, 20), (width - 20, height - 20), (0, 0, 255), 1)
  797. cv2.putText(img_small, 'subfield_line', (line_xmax2_1, height - 20), font, 0.7, (0, 0, 255), 2,
  798. False)
  799. elif line_xmax1 != 0 and line_xmax2 == 0:
  800. if int(line_xmax1 * show_ratio) == width:
  801. line_xmax1_1 = int(line_xmax1 * show_ratio)
  802. cv2.rectangle(img_small, (20, 20), (line_xmax1_1 - 20, height - 20), (0, 0, 255), 1)
  803. cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
  804. else:
  805. line_xmax1_1 = int(line_xmax1 * show_ratio)
  806. cv2.rectangle(img_small, (20, 20), (line_xmax1_1, height - 20), (0, 0, 255), 1)
  807. cv2.putText(img_small, 'subfield_line', (20, height - 20), font, 0.7, (0, 0, 255), 2, False)
  808. cv2.rectangle(img_small, (line_xmax1_1, 20), (width - 20, height - 20), (0, 0, 255), 1)
  809. cv2.putText(img_small, 'subfield_line', (line_xmax1_1, height - 20), font, 0.7, (0, 0, 255), 2,
  810. False)
  811. write_single_img(img_small, save_path)
  812. height, width = img_small.shape[0], img_small.shape[1]
  813. except Exception as e:
  814. logger.info('box resolve error: {}'.format(e))
  815. is_success = 0
  816. error_info = 'box resolve error'
  817. sheet_dict.update({'is_success': is_success})
  818. if error_info:
  819. sheet_dict.update({'is_success': is_success, 'error': error_info})
  820. res_json = json.dumps(sheet_dict, ensure_ascii=False)
  821. # logger.info('segment_info: {}\n'.format(res_json))
  822. res_dict = {'url': img_url_path,
  823. 'img_height': height,
  824. 'text_height': 0.9 * height,
  825. 'texts': res_json,
  826. 'raw_texts': ''
  827. }
  828. return render(request, 'showimg.html', res_dict)
  829. # return HttpResponse(res_json)
  830. else:
  831. error_json = form.errors.as_json()
  832. is_success = 99
  833. res = {'is_success': is_success, 'error': error_json}
  834. return HttpResponse('{}'.format(res))
  835. else:
  836. form_img = UploadImageForm()
  837. form_xml = UploadFileForm()
  838. return render(request, 'sheet_detail.html', {'form_img': form_img, 'form_xml': form_xml})
  839. @csrf_exempt
  840. def download_image(request):
  841. if request.method == 'POST':
  842. form = DownloadImage(request.POST, request.FILES)
  843. if form.is_valid():
  844. is_success = 1
  845. try:
  846. paper_id = form.cleaned_data['paper_id']
  847. # download_path = SheetBoxes.objects.get(paper_id=paper_id).download_path
  848. paper_id_obj = SheetBoxes.objects.filter(paper_id__icontains=paper_id)
  849. paper_id_list = [ele.paper_id for ele in paper_id_obj]
  850. download_path_list = [SheetBoxes.objects.get(paper_id=ele).download_path for ele in paper_id_list]
  851. res_dict = {'paper_id': paper_id_list,
  852. 'image_url': download_path_list,
  853. # 'img_height': 1000,
  854. # 'text_height': 10,
  855. # 'texts': download_path,
  856. # 'raw_texts': '',
  857. 'is_success': is_success
  858. }
  859. except Exception as e:
  860. logger.info('image address load failed: {}'.format(e))
  861. is_success = 0
  862. error_info = 'image address load failed'
  863. res_dict = {'is_success': is_success, 'error': error_info}
  864. res_json = json.dumps(res_dict, ensure_ascii=False)
  865. # return render(request, 'showimg.html', res_dict)
  866. return HttpResponse(res_json)
  867. else:
  868. error_json = form.errors.as_json()
  869. is_success = 99
  870. res = {'is_success': is_success, 'error': error_json}
  871. return HttpResponse('{}'.format(res))
  872. else:
  873. form = DownloadImage()
  874. return render(request, 'downloadimg.html', {'form': form})