sheet_views.py 47 KB

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