read_image_scale.py 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627
  1. import logging
  2. import sys
  3. import os
  4. import cv2
  5. import numpy as np
  6. import importlib
  7. import multiprocessing as mp
  8. from opton_pred_v2.config import get_task_models
  9. def read_scale(im_inp, method='cnn',print_info='yes'):
  10. if method == 'cnn':
  11. return read_scale_cnn(im_inp,print_info)
  12. else:
  13. return read_scale_cv(im_inp)
  14. def read_scale_cnn(im_inp,print_info):
  15. zoom_key = None
  16. um_per_pix = None
  17. ruler_rect = None
  18. mark_bbox = None
  19. h, w = im_inp.shape[:2]
  20. crop_x = w - int(w * 0.3)
  21. crop_y = h - int(h * 0.2)
  22. im_crop = im_inp[crop_y:h, crop_x:w, :]
  23. # check task script
  24. task_name = 'Ruler'
  25. root_dir = os.getenv('VI_OPTON')
  26. task_script_pyc = os.path.join(root_dir, 'opton_pred_v2', 'tasks', '{}.pyc'.format(task_name))
  27. task_script_py = os.path.join(root_dir, 'opton_pred_v2','tasks', '{}.py'.format(task_name))
  28. if not os.path.exists(task_script_pyc) and not os.path.exists(task_script_py):
  29. logging.error('Task script not existes: {}'.format(task_name))
  30. return zoom_key, um_per_pix, None
  31. zoom_txt = None
  32. zoom_bbox = None
  33. if sys.platform.find('linux') >= 0:
  34. mp_queue = mp.Queue()
  35. mp_kwargs = dict(
  36. task_name=task_name,
  37. im_crop=im_crop,
  38. queue=mp_queue)
  39. p = mp.Process(target=do_ruler_task, kwargs=mp_kwargs)
  40. p.start()
  41. (zoom_txt, zoom_bbox) = mp_queue.get()
  42. p.join()
  43. else:
  44. (zoom_txt, zoom_bbox) = do_ruler_task(task_name, im_crop)
  45. if zoom_txt is None:
  46. return zoom_key, um_per_pix, None
  47. zoom_val = int(zoom_txt)
  48. zoom_key = 10000 / zoom_val
  49. ruler_len = -1
  50. crop_bbox = (crop_x, crop_y, w, h)
  51. # 1) try to find red ruler
  52. ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_red(im_crop, zoom_bbox)
  53. if ruler_len > 0:
  54. um_per_pix = float(zoom_val) / ruler_len
  55. if print_info=='yes':
  56. print("get_ruler_len_red:",zoom_val,ruler_len,um_per_pix)
  57. return zoom_key, um_per_pix, (crop_bbox, None, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
  58. # 2) try to find green ruler
  59. ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_green(im_crop, zoom_bbox)
  60. if ruler_len > 0:
  61. um_per_pix = float(zoom_val) / ruler_len
  62. if print_info=='yes':
  63. print("get_ruler_len_green:",zoom_val,ruler_len,um_per_pix)
  64. return zoom_key, um_per_pix, (crop_bbox, None, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
  65. # 3) try to find white ruler
  66. ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_white(im_crop, zoom_bbox)
  67. if ruler_len > 0:
  68. um_per_pix = float(zoom_val) / ruler_len
  69. if print_info=='yes':
  70. print("get_ruler_len_white:",zoom_val,ruler_len,um_per_pix)
  71. return zoom_key, um_per_pix, (crop_bbox, None, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
  72. # 4) try to find black ruler
  73. ruler_len, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_black(im_crop, zoom_bbox)
  74. if ruler_len > 0:
  75. um_per_pix = float(zoom_val) / ruler_len
  76. if bg_bbox is not None:
  77. (x1,y1,x2,y2) = bg_bbox
  78. bg_bbox = (crop_x+x1, crop_y+y1, crop_x+x2, crop_y+y2)
  79. if print_info=='yes':
  80. print("get_ruler_len_black:",zoom_val,ruler_len,um_per_pix)
  81. return zoom_key, um_per_pix, (crop_bbox, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
  82. return zoom_key, um_per_pix, None
  83. def do_ruler_task(task_name, im_crop, queue=None):
  84. logging.debug('(In process {}) do_ruler_task'.format(os.getpid()))
  85. zoom_txt = None
  86. zoom_bbox = None
  87. # create task instance
  88. try:
  89. module = importlib.import_module('tasks.{}'.format(task_name))
  90. except:
  91. module = importlib.import_module('opton_pred_v2.tasks.{}'.format(task_name))
  92. task = module.pred_task(task_name, {})
  93. # get model ids that assigned to this task
  94. model_ids = get_task_models(task_name)
  95. if model_ids is not None:
  96. if task.init_models(model_ids) == False:
  97. logging.error('Fail to init models for task: {}'.format(task_name))
  98. if queue is not None:
  99. queue.put((zoom_txt, zoom_bbox))
  100. return (zoom_txt, zoom_bbox)
  101. # pred image
  102. zoom_txt, zoom_bbox = task.pred_img_data(im_crop)
  103. if queue is not None:
  104. queue.put((zoom_txt, zoom_bbox))
  105. return (zoom_txt, zoom_bbox)
  106. def get_ruler_len_red(im_crop, zoom_bbox):
  107. ruler_len = -1
  108. im_hsv = cv2.cvtColor(im_crop, cv2.COLOR_BGR2HSV)
  109. _show_img(im_hsv, 'get_ruler_len_red - im_hsv')
  110. # lower_red = np.array([0, 43, 46])
  111. # upper_red = np.array([10, 255, 255])
  112. lower_red = np.array([0, 220, 120])
  113. upper_red = np.array([5, 255, 255])
  114. mask0 = cv2.inRange(im_hsv, lower_red, upper_red)
  115. # lower_red = np.array([156, 43, 46])
  116. # upper_red = np.array([180, 255, 255])
  117. # mask1 = cv2.inRange(im_hsv, lower_red, upper_red)
  118. # mask = mask0 + mask1
  119. # roi_red = cv2.bitwise_and(im_crop, im_crop, mask=mask)
  120. # _show_img(roi_red, 'roi_red')
  121. # im_bgr = cv2.cvtColor(roi_red, cv2.COLOR_HSV2BGR)
  122. # im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY)
  123. # _show_img(im_gray, 'im_gray')
  124. # global debug_img
  125. # debug_img=True
  126. # _show_img(mask0, 'im_gray')
  127. _, im_thresh = cv2.threshold(mask0, 150, 255, cv2.THRESH_BINARY)
  128. #debug_img=False
  129. #_show_img(im_thresh, 'im_thresh')
  130. max_w = 0
  131. max_w_rect = None
  132. outer_cnts = []
  133. inner_cnts = []
  134. ruler_bbox = None
  135. mark_bbox = None
  136. cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  137. num = len(cnts)
  138. if num == 0:
  139. return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  140. for i in range(num):
  141. (x,y,w,h) = cv2.boundingRect(cnts[i])
  142. if w < 5 and h < 5:
  143. continue
  144. if hierarchy[0][i][3] == -1:
  145. outer_cnts.append(cnts[i])
  146. else:
  147. inner_cnts.append(cnts[i])
  148. if w > max_w:
  149. max_w = w
  150. max_w_rect = (x,y,w,h)
  151. if debug_img:
  152. im_debug = im_crop.copy()
  153. cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
  154. #_show_img(im_debug, '{}/{} cnt rect on im_crop'.format(i+1, num))
  155. if max_w > 10:
  156. ruler_len = max_w
  157. ruler_bbox = (max_w_rect[0], max_w_rect[1], max_w_rect[0] + max_w_rect[2], max_w_rect[1] + max_w_rect[3])
  158. mark_bbox = (
  159. min(ruler_bbox[0], zoom_bbox[0]),
  160. min(ruler_bbox[1], zoom_bbox[1]),
  161. max(ruler_bbox[2], zoom_bbox[2]),
  162. max(ruler_bbox[3], zoom_bbox[3])
  163. )
  164. return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  165. def get_ruler_len_green(im_crop, zoom_bbox):
  166. ruler_len = -1
  167. im_hsv = cv2.cvtColor(im_crop, cv2.COLOR_BGR2HSV)
  168. #_show_img(im_hsv, 'get_ruler_len_green - im_hsv')
  169. # lower_color = np.array([35, 43, 46])
  170. # upper_color = np.array([77, 255, 255])
  171. lower_color = np.array([60, 100, 110])
  172. upper_color = np.array([90, 255, 255])
  173. mask = cv2.inRange(im_hsv, lower_color, upper_color)
  174. # roi_color = cv2.bitwise_and(im_crop, im_crop, mask=mask)
  175. # #_show_img(roi_color, 'roi_green')
  176. # im_bgr = cv2.cvtColor(roi_color, cv2.COLOR_HSV2BGR)
  177. # im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY)
  178. # global debug_img
  179. # debug_img=True
  180. # _show_img(mask, 'im_gray')
  181. _, im_thresh = cv2.threshold(mask, 20, 255, cv2.THRESH_BINARY)
  182. #_show_img(im_thresh, 'im_thresh')
  183. max_w = 0
  184. max_w_rect = None
  185. outer_cnts = []
  186. inner_cnts = []
  187. ruler_bbox = None
  188. mark_bbox = None
  189. cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  190. cv2.drawContours(im_thresh,cnts,-1,(0,0,255))
  191. _show_img(im_crop, 'im_thresh')
  192. num = len(cnts)
  193. if num == 0:
  194. return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  195. for i in range(num):
  196. (x,y,w,h) = cv2.boundingRect(cnts[i])
  197. if w < 5 and h < 5:
  198. continue
  199. if hierarchy[0][i][3] == -1:
  200. outer_cnts.append(cnts[i])
  201. else:
  202. inner_cnts.append(cnts[i])
  203. if w > max_w:
  204. max_w = w
  205. max_w_rect = (x,y,w,h)
  206. if debug_img:
  207. im_debug = im_crop.copy()
  208. cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
  209. #_show_img(im_debug, '{}/{} cnt rect on im_crop'.format(i+1, num))
  210. if max_w > 10:
  211. ruler_len = max_w
  212. ruler_bbox = (max_w_rect[0], max_w_rect[1], max_w_rect[0] + max_w_rect[2], max_w_rect[1] + max_w_rect[3])
  213. mark_bbox = (
  214. min(ruler_bbox[0], zoom_bbox[0]),
  215. min(ruler_bbox[1], zoom_bbox[1]),
  216. max(ruler_bbox[2], zoom_bbox[2]),
  217. max(ruler_bbox[3], zoom_bbox[3])
  218. )
  219. return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  220. def get_ruler_len_white(im_crop, zoom_bbox):
  221. ruler_len = -1
  222. im_gray = cv2.cvtColor(im_crop, cv2.COLOR_BGR2GRAY)
  223. _show_img(im_gray, 'get_ruler_len_white - im_gray')
  224. _, im_thresh = cv2.threshold(im_gray, 240, 255, cv2.THRESH_BINARY)
  225. _show_img(im_thresh, 'im_thresh')
  226. max_w = 0
  227. max_w_rect = None
  228. outer_cnts = []
  229. inner_cnts = []
  230. ruler_bbox = None
  231. mark_bbox = None
  232. cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  233. num = len(cnts)
  234. if num == 0:
  235. return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  236. for i in range(num):
  237. (x,y,w,h) = cv2.boundingRect(cnts[i])
  238. if w < 5 and h < 5:
  239. continue
  240. if hierarchy[0][i][3] == -1:
  241. outer_cnts.append(cnts[i])
  242. else:
  243. inner_cnts.append(cnts[i])
  244. if w > max_w:
  245. max_w = w
  246. max_w_rect = (x,y,w,h)
  247. if debug_img:
  248. im_debug = im_crop.copy()
  249. cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
  250. _show_img(im_debug, '{}/{} cnt rect on im_crop'.format(i+1, num))
  251. if max_w > 10:
  252. ruler_len = max_w
  253. ruler_bbox = (max_w_rect[0], max_w_rect[1], max_w_rect[0] + max_w_rect[2], max_w_rect[1] + max_w_rect[3])
  254. mark_bbox = (
  255. min(ruler_bbox[0], zoom_bbox[0]),
  256. min(ruler_bbox[1], zoom_bbox[1]),
  257. max(ruler_bbox[2], zoom_bbox[2]),
  258. max(ruler_bbox[3], zoom_bbox[3])
  259. )
  260. return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  261. def get_ruler_len_black(im_crop, zoom_bbox):
  262. ruler_len = -1
  263. im_gray = cv2.cvtColor(im_crop, cv2.COLOR_BGR2GRAY)
  264. _show_img(im_gray, 'get_ruler_len_black - im_gray')
  265. im_roi = im_gray
  266. roi_x = 0
  267. roi_y = 0
  268. # try to find white bg box
  269. bg_bbox = get_ruler_white_bg_rect_by_cnt(im_gray, zoom_bbox)
  270. if bg_bbox is not None:
  271. (x1,y1,x2,y2) = bg_bbox
  272. im_roi = im_gray[y1:y2, x1:x2]
  273. roi_x = x1
  274. roi_y = y1
  275. _show_img(im_roi, 'im_roi: found bg box')
  276. else:
  277. _show_img(im_roi, 'im_roi: not found bg box')
  278. _, im_thresh = cv2.threshold(im_roi, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
  279. # _, im_thresh = cv2.threshold(im_roi, 50, 255, cv2.THRESH_BINARY_INV)
  280. _show_img(im_thresh, 'im_thresh')
  281. max_w = 0
  282. max_w_rect = None
  283. outer_cnts = []
  284. inner_cnts = []
  285. ruler_bbox = None
  286. mark_bbox = None
  287. cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  288. num = len(cnts)
  289. if num == 0:
  290. logging.debug('get_ruler_len_black: not found any cnts')
  291. return ruler_len, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  292. for i in range(num):
  293. (x,y,w,h) = cv2.boundingRect(cnts[i])
  294. if w < 5 and h < 5:
  295. continue
  296. if hierarchy[0][i][3] == -1:
  297. outer_cnts.append(cnts[i])
  298. else:
  299. inner_cnts.append(cnts[i])
  300. if w > max_w:
  301. max_w = w
  302. max_w_rect = (x,y,w,h)
  303. if debug_img:
  304. im_debug = im_crop.copy()
  305. if bg_bbox is not None:
  306. (x1,y1,x2,y2) = bg_bbox
  307. im_debug = im_crop[y1:y2, x1:x2].copy()
  308. cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
  309. #_show_img(im_debug, '{}/{} cnt on im_roi: w={}'.format(i+1, num, w))
  310. if max_w > 10:
  311. ruler_len = max_w
  312. ruler_bbox = (max_w_rect[0], max_w_rect[1], max_w_rect[0] + max_w_rect[2], max_w_rect[1] + max_w_rect[3])
  313. mark_bbox = (
  314. min(ruler_bbox[0], zoom_bbox[0]-roi_x),
  315. min(ruler_bbox[1], zoom_bbox[1]-roi_y),
  316. max(ruler_bbox[2], zoom_bbox[2]-roi_x),
  317. max(ruler_bbox[3], zoom_bbox[3]-roi_y)
  318. )
  319. return ruler_len, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
  320. def get_ruler_white_bg_rect_by_cnt(im_gray, zoom_bbox):
  321. _, im_thresh = cv2.threshold(im_gray, 250, 255, cv2.THRESH_BINARY)
  322. _show_img(im_thresh, 'get_ruler_white_bg_rect_by_cnt - im_thresh')
  323. cnts, _ = cv2.findContours(im_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  324. if len(cnts) > 0:
  325. (x1,y1,x2,y2) = zoom_bbox
  326. cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
  327. for cnt in cnts:
  328. (x,y,w,h) = cv2.boundingRect(cnt)
  329. if x1 > x and y1 > y and x2 < (x+w) and y2 < (y+h):
  330. margin = 5
  331. x1 = x + margin
  332. y1 = y + margin
  333. x2 = x + w - margin
  334. y2 = y + h - margin
  335. return (x1, y1, x2, y2)
  336. return None
  337. def read_scale_cv(im_inp):
  338. _show_img(im_inp, 'im_inp')
  339. zoom_key = None
  340. um_per_pix = None
  341. h, w = im_inp.shape[:2]
  342. crop_h = int(h / 4 * 3)
  343. crop_w = int(w / 4 * 3)
  344. im_crop = im_inp[crop_h:h, crop_w:w, :]
  345. _show_img(im_crop, 'im_crop')
  346. # im_blur = cv2.GaussianBlur(im_crop, (5, 5), 0)
  347. # _show_img(im_blur, 'im_blur')
  348. im_hsv = cv2.cvtColor(im_crop, cv2.COLOR_BGR2HSV)
  349. _show_img(im_hsv, 'im_hsv')
  350. logging.debug('mask0')
  351. lower_red = np.array([0, 43, 46])
  352. upper_red = np.array([10, 255, 255])
  353. mask0 = cv2.inRange(im_hsv, lower_red, upper_red)
  354. # logging.debug(mask0)
  355. logging.debug('mask1')
  356. lower_red = np.array([156, 43, 46])
  357. upper_red = np.array([180, 255, 255])
  358. mask1 = cv2.inRange(im_hsv, lower_red, upper_red)
  359. # logging.debug(mask1)
  360. mask = mask0 + mask1
  361. roi_red = cv2.bitwise_and(im_crop, im_crop, mask=mask)
  362. _show_img(roi_red, 'roi_red')
  363. im_bgr = cv2.cvtColor(roi_red, cv2.COLOR_HSV2BGR)
  364. im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY)
  365. _show_img(im_gray, 'im_gray')
  366. _, im_thresh = cv2.threshold(im_gray, 200, 255, cv2.THRESH_BINARY)
  367. _show_img(im_thresh, 'im_thresh')
  368. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  369. im_thresh_erode = cv2.erode(im_thresh, kernel, iterations=1)
  370. _show_img(im_thresh_erode, 'im_thresh_erode')
  371. kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
  372. im_thresh_dilate = cv2.dilate(im_thresh_erode, kernel, iterations=1)
  373. _show_img(im_thresh_dilate, 'im_thresh_dilate')
  374. cnts, _ = cv2.findContours(im_thresh_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
  375. cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
  376. num = len(cnts)
  377. if num == 0:
  378. logging.debug('not found cnts')
  379. return zoom_key, um_per_pix
  380. x_list = []
  381. for i in range(num):
  382. x, y, w, h = cv2.boundingRect(cnts[i])
  383. if w < 5 or h < 10:
  384. continue
  385. x_list.append([x, y, w, h])
  386. num = len(x_list)
  387. if num < 2:
  388. logging.debug('all cnts small')
  389. return zoom_key, um_per_pix
  390. logging.debug('found {} big cnts'.format(num))
  391. res_x_list = sorted(x_list, key=lambda x: x[0]) # sort by x
  392. if debug_img:
  393. im_debug = im_crop.copy()
  394. for i in range(num):
  395. (x,y,w,h) = res_x_list[i]
  396. cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
  397. _show_img(im_debug, 'draw {}/{} contour boxe: {}x{}'.format(i+1, num, w, h))
  398. iVal255 = []
  399. if num == 5:
  400. zoom_key = 100
  401. else:
  402. x, y, w, h = res_x_list[1]
  403. img1 = im_thresh[y:y + h, x:x + w]
  404. res = cv2.resize(img1, (40, 50), interpolation=cv2.INTER_LINEAR) # resize to match template size
  405. root_dir = os.getenv('VI_OPTON')
  406. temp_file = os.path.join(root_dir, 'opton_core', 'read_image_scale.npz')
  407. temp_list = np.load(temp_file)
  408. for i in temp_list.keys():
  409. img_and = cv2.bitwise_and(res, res, mask=temp_list[i])
  410. iVal255.append(cv2.countNonZero(img_and))
  411. index_num = iVal255.index(max(iVal255))
  412. if index_num == 0:
  413. zoom_key = 200
  414. elif index_num == 1:
  415. zoom_key = 500
  416. if num == 6:
  417. zoom_key = 50
  418. elif index_num == 2:
  419. zoom_key = 1000
  420. if num == 6:
  421. zoom_key = 10
  422. if zoom_key is None:
  423. return zoom_key, um_per_pix
  424. scale_len_pix = 0
  425. for x in res_x_list:
  426. if x[2] > scale_len_pix:
  427. scale_len_pix = x[2]
  428. um_per_pix = float(10000 / zoom_key) / scale_len_pix
  429. return zoom_key, um_per_pix
  430. def _draw_ruler(im_inp, ruler_cnts):
  431. if ruler_cnts is None:
  432. return
  433. (crop_bbox, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox) = ruler_cnts
  434. (cx1,cy1,cx2,cy2) = crop_bbox
  435. im_cnt = None
  436. im_copy = None
  437. if bg_bbox is not None:
  438. (bx1,by1,bx2,by2) = bg_bbox
  439. im_cnt = im_inp[by1:by2,bx1:bx2,:]
  440. cv2.rectangle(im_cnt, (bx1,by1), (bx2,by2), (255,255,255), -1)
  441. cv2.drawContours(im_cnt, outer_cnts, -1, (0, 0, 0), -1)
  442. cv2.drawContours(im_cnt, inner_cnts, -1, (255, 255, 255), -1)
  443. else:
  444. im_cnt = im_inp[cy1:cy2,cx1:cx2,:]
  445. im_copy = im_cnt.copy()
  446. cv2.drawContours(im_cnt, outer_cnts, -1, (0, 0, 255), -1)
  447. for cnt in inner_cnts:
  448. (x,y,w,h) = cv2.boundingRect(cnt)
  449. im_cnt[y:y+h,x:x+w,:] = im_copy[y:y+h,x:x+w,:]
  450. # draw ruler bbox
  451. if ruler_bbox is not None:
  452. (x1,y1,x2,y2) = ruler_bbox
  453. cv2.rectangle(im_cnt, (x1,y1), (x2,y2), (0,255,0), 2)
  454. # draw marker bbox
  455. if mark_bbox is not None:
  456. (x1,y1,x2,y2) = mark_bbox
  457. cv2.rectangle(im_cnt, (x1,y1), (x2,y2), (255,0,0), 2)
  458. debug_img = False
  459. def _show_img(im_inp, msg=None):
  460. if debug_img:
  461. if msg is not None:
  462. print('[IMAGE] ' + msg)
  463. h,w = im_inp.shape[:2]
  464. cv2.namedWindow("Example", cv2.WINDOW_NORMAL)
  465. cv2.resizeWindow('Example', int(w*1.0), int(h*1.0))
  466. cv2.moveWindow('Example', 600, 20)
  467. cv2.imshow("Example", im_inp)
  468. k = cv2.waitKey()
  469. if k == 27:
  470. cv2.destroyAllWindows()
  471. if __name__ == '__main__':
  472. vi_lib_dir = os.getenv('VI_LIB')
  473. vi_opton_dir = os.getenv('VI_OPTON')
  474. opton_pred_dir = os.path.join(vi_opton_dir, 'opton_pred_v2')
  475. sys.path.append(vi_lib_dir)
  476. sys.path.append(vi_opton_dir)
  477. sys.path.append(opton_pred_dir)
  478. if os.path.isdir(sys.argv[1]):
  479. from_dir = sys.argv[1]
  480. # to_dir = sys.argv[2]
  481. to_dir = '/home/reacher/Pictures/ruler'
  482. print('save output to ' + to_dir)
  483. names = os.listdir(from_dir)
  484. names = [n.strip() for n in names if n.strip().lower().endswith('.jpg')]
  485. num = len(names)
  486. for i in range(num):
  487. basename, ext = os.path.splitext(names[i])
  488. from_jpg = os.path.join(from_dir, names[i])
  489. im_inp = cv2.imread(from_jpg)
  490. zoom_key, um_per_pix, ruler_cnts = read_scale(im_inp)
  491. suffix = 'fail'
  492. if zoom_key is not None:
  493. suffix = zoom_key
  494. if um_per_pix is not None:
  495. suffix = '{}_{:.3f}'.format(suffix, um_per_pix)
  496. else:
  497. suffix = '{}_fail'.format(suffix)
  498. to_jpg = os.path.join(to_dir, '{}_{}_inp.jpg'.format(basename, suffix))
  499. cv2.imwrite(to_jpg, im_inp)
  500. to_jpg = os.path.join(to_dir, '{}_{}_out.jpg'.format(basename, suffix))
  501. _draw_ruler(im_inp, ruler_cnts)
  502. cv2.imwrite(to_jpg, im_inp)
  503. print('{}/{} {:80} {}'.format(i+1, num, names[i], suffix))
  504. else:
  505. logging.basicConfig(
  506. format='%(asctime)s [%(levelname)s] - %(message)s',
  507. level=logging.DEBUG)
  508. jpg_file = sys.argv[1]
  509. im_inp = cv2.imread(jpg_file)
  510. zoom_key, um_per_pix, ruler_cnts = read_scale(im_inp)
  511. print(zoom_key, um_per_pix)
  512. _draw_ruler(im_inp, ruler_cnts)
  513. _show_img(im_inp)