| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627 |
- import logging
- import sys
- import os
- import cv2
- import numpy as np
- import importlib
- import multiprocessing as mp
- from opton_pred_v2.config import get_task_models
- def read_scale(im_inp, method='cnn',print_info='yes'):
- if method == 'cnn':
- return read_scale_cnn(im_inp,print_info)
- else:
- return read_scale_cv(im_inp)
- def read_scale_cnn(im_inp,print_info):
- zoom_key = None
- um_per_pix = None
- ruler_rect = None
- mark_bbox = None
- h, w = im_inp.shape[:2]
- crop_x = w - int(w * 0.3)
- crop_y = h - int(h * 0.2)
- im_crop = im_inp[crop_y:h, crop_x:w, :]
- # check task script
- task_name = 'Ruler'
- root_dir = os.getenv('VI_OPTON')
- task_script_pyc = os.path.join(root_dir, 'opton_pred_v2', 'tasks', '{}.pyc'.format(task_name))
- task_script_py = os.path.join(root_dir, 'opton_pred_v2','tasks', '{}.py'.format(task_name))
- if not os.path.exists(task_script_pyc) and not os.path.exists(task_script_py):
- logging.error('Task script not existes: {}'.format(task_name))
- return zoom_key, um_per_pix, None
- zoom_txt = None
- zoom_bbox = None
- if sys.platform.find('linux') >= 0:
- mp_queue = mp.Queue()
- mp_kwargs = dict(
- task_name=task_name,
- im_crop=im_crop,
- queue=mp_queue)
- p = mp.Process(target=do_ruler_task, kwargs=mp_kwargs)
- p.start()
- (zoom_txt, zoom_bbox) = mp_queue.get()
- p.join()
- else:
- (zoom_txt, zoom_bbox) = do_ruler_task(task_name, im_crop)
- if zoom_txt is None:
- return zoom_key, um_per_pix, None
- zoom_val = int(zoom_txt)
-
- zoom_key = 10000 / zoom_val
- ruler_len = -1
- crop_bbox = (crop_x, crop_y, w, h)
- # 1) try to find red ruler
- ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_red(im_crop, zoom_bbox)
- if ruler_len > 0:
- um_per_pix = float(zoom_val) / ruler_len
- if print_info=='yes':
- print("get_ruler_len_red:",zoom_val,ruler_len,um_per_pix)
- return zoom_key, um_per_pix, (crop_bbox, None, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
-
- # 2) try to find green ruler
- ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_green(im_crop, zoom_bbox)
- if ruler_len > 0:
- um_per_pix = float(zoom_val) / ruler_len
- if print_info=='yes':
- print("get_ruler_len_green:",zoom_val,ruler_len,um_per_pix)
- return zoom_key, um_per_pix, (crop_bbox, None, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
- # 3) try to find white ruler
- ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_white(im_crop, zoom_bbox)
- if ruler_len > 0:
- um_per_pix = float(zoom_val) / ruler_len
- if print_info=='yes':
- print("get_ruler_len_white:",zoom_val,ruler_len,um_per_pix)
- return zoom_key, um_per_pix, (crop_bbox, None, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
- # 4) try to find black ruler
- ruler_len, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox = get_ruler_len_black(im_crop, zoom_bbox)
- if ruler_len > 0:
- um_per_pix = float(zoom_val) / ruler_len
- if bg_bbox is not None:
- (x1,y1,x2,y2) = bg_bbox
- bg_bbox = (crop_x+x1, crop_y+y1, crop_x+x2, crop_y+y2)
- if print_info=='yes':
- print("get_ruler_len_black:",zoom_val,ruler_len,um_per_pix)
- return zoom_key, um_per_pix, (crop_bbox, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox)
-
- return zoom_key, um_per_pix, None
- def do_ruler_task(task_name, im_crop, queue=None):
- logging.debug('(In process {}) do_ruler_task'.format(os.getpid()))
- zoom_txt = None
- zoom_bbox = None
- # create task instance
- try:
- module = importlib.import_module('tasks.{}'.format(task_name))
- except:
- module = importlib.import_module('opton_pred_v2.tasks.{}'.format(task_name))
- task = module.pred_task(task_name, {})
- # get model ids that assigned to this task
- model_ids = get_task_models(task_name)
- if model_ids is not None:
- if task.init_models(model_ids) == False:
- logging.error('Fail to init models for task: {}'.format(task_name))
- if queue is not None:
- queue.put((zoom_txt, zoom_bbox))
- return (zoom_txt, zoom_bbox)
- # pred image
- zoom_txt, zoom_bbox = task.pred_img_data(im_crop)
- if queue is not None:
- queue.put((zoom_txt, zoom_bbox))
- return (zoom_txt, zoom_bbox)
- def get_ruler_len_red(im_crop, zoom_bbox):
- ruler_len = -1
- im_hsv = cv2.cvtColor(im_crop, cv2.COLOR_BGR2HSV)
- _show_img(im_hsv, 'get_ruler_len_red - im_hsv')
- # lower_red = np.array([0, 43, 46])
- # upper_red = np.array([10, 255, 255])
- lower_red = np.array([0, 220, 120])
- upper_red = np.array([5, 255, 255])
- mask0 = cv2.inRange(im_hsv, lower_red, upper_red)
- # lower_red = np.array([156, 43, 46])
- # upper_red = np.array([180, 255, 255])
- # mask1 = cv2.inRange(im_hsv, lower_red, upper_red)
- # mask = mask0 + mask1
- # roi_red = cv2.bitwise_and(im_crop, im_crop, mask=mask)
- # _show_img(roi_red, 'roi_red')
- # im_bgr = cv2.cvtColor(roi_red, cv2.COLOR_HSV2BGR)
- # im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY)
- # _show_img(im_gray, 'im_gray')
- # global debug_img
- # debug_img=True
- # _show_img(mask0, 'im_gray')
- _, im_thresh = cv2.threshold(mask0, 150, 255, cv2.THRESH_BINARY)
- #debug_img=False
- #_show_img(im_thresh, 'im_thresh')
- max_w = 0
- max_w_rect = None
- outer_cnts = []
- inner_cnts = []
- ruler_bbox = None
- mark_bbox = None
- cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- num = len(cnts)
- if num == 0:
- return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- for i in range(num):
- (x,y,w,h) = cv2.boundingRect(cnts[i])
- if w < 5 and h < 5:
- continue
- if hierarchy[0][i][3] == -1:
- outer_cnts.append(cnts[i])
- else:
- inner_cnts.append(cnts[i])
- if w > max_w:
- max_w = w
- max_w_rect = (x,y,w,h)
- if debug_img:
- im_debug = im_crop.copy()
- cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
- #_show_img(im_debug, '{}/{} cnt rect on im_crop'.format(i+1, num))
-
- if max_w > 10:
- ruler_len = max_w
- 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])
- mark_bbox = (
- min(ruler_bbox[0], zoom_bbox[0]),
- min(ruler_bbox[1], zoom_bbox[1]),
- max(ruler_bbox[2], zoom_bbox[2]),
- max(ruler_bbox[3], zoom_bbox[3])
- )
- return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- def get_ruler_len_green(im_crop, zoom_bbox):
- ruler_len = -1
-
- im_hsv = cv2.cvtColor(im_crop, cv2.COLOR_BGR2HSV)
- #_show_img(im_hsv, 'get_ruler_len_green - im_hsv')
- # lower_color = np.array([35, 43, 46])
- # upper_color = np.array([77, 255, 255])
- lower_color = np.array([60, 100, 110])
- upper_color = np.array([90, 255, 255])
- mask = cv2.inRange(im_hsv, lower_color, upper_color)
- # roi_color = cv2.bitwise_and(im_crop, im_crop, mask=mask)
- # #_show_img(roi_color, 'roi_green')
- # im_bgr = cv2.cvtColor(roi_color, cv2.COLOR_HSV2BGR)
- # im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY)
- # global debug_img
- # debug_img=True
- # _show_img(mask, 'im_gray')
-
- _, im_thresh = cv2.threshold(mask, 20, 255, cv2.THRESH_BINARY)
- #_show_img(im_thresh, 'im_thresh')
- max_w = 0
- max_w_rect = None
- outer_cnts = []
- inner_cnts = []
- ruler_bbox = None
- mark_bbox = None
- cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- cv2.drawContours(im_thresh,cnts,-1,(0,0,255))
- _show_img(im_crop, 'im_thresh')
- num = len(cnts)
- if num == 0:
- return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- for i in range(num):
- (x,y,w,h) = cv2.boundingRect(cnts[i])
- if w < 5 and h < 5:
- continue
- if hierarchy[0][i][3] == -1:
- outer_cnts.append(cnts[i])
- else:
- inner_cnts.append(cnts[i])
- if w > max_w:
- max_w = w
- max_w_rect = (x,y,w,h)
- if debug_img:
- im_debug = im_crop.copy()
- cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
- #_show_img(im_debug, '{}/{} cnt rect on im_crop'.format(i+1, num))
- if max_w > 10:
- ruler_len = max_w
- 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])
- mark_bbox = (
- min(ruler_bbox[0], zoom_bbox[0]),
- min(ruler_bbox[1], zoom_bbox[1]),
- max(ruler_bbox[2], zoom_bbox[2]),
- max(ruler_bbox[3], zoom_bbox[3])
- )
- return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- def get_ruler_len_white(im_crop, zoom_bbox):
- ruler_len = -1
- im_gray = cv2.cvtColor(im_crop, cv2.COLOR_BGR2GRAY)
- _show_img(im_gray, 'get_ruler_len_white - im_gray')
- _, im_thresh = cv2.threshold(im_gray, 240, 255, cv2.THRESH_BINARY)
- _show_img(im_thresh, 'im_thresh')
- max_w = 0
- max_w_rect = None
- outer_cnts = []
- inner_cnts = []
- ruler_bbox = None
- mark_bbox = None
- cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- num = len(cnts)
- if num == 0:
- return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- for i in range(num):
- (x,y,w,h) = cv2.boundingRect(cnts[i])
- if w < 5 and h < 5:
- continue
- if hierarchy[0][i][3] == -1:
- outer_cnts.append(cnts[i])
- else:
- inner_cnts.append(cnts[i])
- if w > max_w:
- max_w = w
- max_w_rect = (x,y,w,h)
- if debug_img:
- im_debug = im_crop.copy()
- cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
- _show_img(im_debug, '{}/{} cnt rect on im_crop'.format(i+1, num))
- if max_w > 10:
- ruler_len = max_w
- 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])
- mark_bbox = (
- min(ruler_bbox[0], zoom_bbox[0]),
- min(ruler_bbox[1], zoom_bbox[1]),
- max(ruler_bbox[2], zoom_bbox[2]),
- max(ruler_bbox[3], zoom_bbox[3])
- )
- return ruler_len, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- def get_ruler_len_black(im_crop, zoom_bbox):
- ruler_len = -1
- im_gray = cv2.cvtColor(im_crop, cv2.COLOR_BGR2GRAY)
- _show_img(im_gray, 'get_ruler_len_black - im_gray')
- im_roi = im_gray
- roi_x = 0
- roi_y = 0
- # try to find white bg box
- bg_bbox = get_ruler_white_bg_rect_by_cnt(im_gray, zoom_bbox)
- if bg_bbox is not None:
- (x1,y1,x2,y2) = bg_bbox
- im_roi = im_gray[y1:y2, x1:x2]
- roi_x = x1
- roi_y = y1
- _show_img(im_roi, 'im_roi: found bg box')
- else:
- _show_img(im_roi, 'im_roi: not found bg box')
- _, im_thresh = cv2.threshold(im_roi, 0, 255, cv2.THRESH_BINARY_INV + cv2.THRESH_OTSU)
- # _, im_thresh = cv2.threshold(im_roi, 50, 255, cv2.THRESH_BINARY_INV)
- _show_img(im_thresh, 'im_thresh')
- max_w = 0
- max_w_rect = None
- outer_cnts = []
- inner_cnts = []
- ruler_bbox = None
- mark_bbox = None
- cnts, hierarchy = cv2.findContours(im_thresh, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
- num = len(cnts)
- if num == 0:
- logging.debug('get_ruler_len_black: not found any cnts')
- return ruler_len, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- for i in range(num):
- (x,y,w,h) = cv2.boundingRect(cnts[i])
- if w < 5 and h < 5:
- continue
- if hierarchy[0][i][3] == -1:
- outer_cnts.append(cnts[i])
- else:
- inner_cnts.append(cnts[i])
- if w > max_w:
- max_w = w
- max_w_rect = (x,y,w,h)
- if debug_img:
- im_debug = im_crop.copy()
- if bg_bbox is not None:
- (x1,y1,x2,y2) = bg_bbox
- im_debug = im_crop[y1:y2, x1:x2].copy()
- cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
- #_show_img(im_debug, '{}/{} cnt on im_roi: w={}'.format(i+1, num, w))
- if max_w > 10:
- ruler_len = max_w
- 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])
- mark_bbox = (
- min(ruler_bbox[0], zoom_bbox[0]-roi_x),
- min(ruler_bbox[1], zoom_bbox[1]-roi_y),
- max(ruler_bbox[2], zoom_bbox[2]-roi_x),
- max(ruler_bbox[3], zoom_bbox[3]-roi_y)
- )
- return ruler_len, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox
- def get_ruler_white_bg_rect_by_cnt(im_gray, zoom_bbox):
- _, im_thresh = cv2.threshold(im_gray, 250, 255, cv2.THRESH_BINARY)
- _show_img(im_thresh, 'get_ruler_white_bg_rect_by_cnt - im_thresh')
-
- cnts, _ = cv2.findContours(im_thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- if len(cnts) > 0:
- (x1,y1,x2,y2) = zoom_bbox
- cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
- for cnt in cnts:
- (x,y,w,h) = cv2.boundingRect(cnt)
- if x1 > x and y1 > y and x2 < (x+w) and y2 < (y+h):
- margin = 5
- x1 = x + margin
- y1 = y + margin
- x2 = x + w - margin
- y2 = y + h - margin
- return (x1, y1, x2, y2)
- return None
- def read_scale_cv(im_inp):
- _show_img(im_inp, 'im_inp')
- zoom_key = None
- um_per_pix = None
- h, w = im_inp.shape[:2]
- crop_h = int(h / 4 * 3)
- crop_w = int(w / 4 * 3)
- im_crop = im_inp[crop_h:h, crop_w:w, :]
- _show_img(im_crop, 'im_crop')
- # im_blur = cv2.GaussianBlur(im_crop, (5, 5), 0)
- # _show_img(im_blur, 'im_blur')
- im_hsv = cv2.cvtColor(im_crop, cv2.COLOR_BGR2HSV)
- _show_img(im_hsv, 'im_hsv')
- logging.debug('mask0')
- lower_red = np.array([0, 43, 46])
- upper_red = np.array([10, 255, 255])
- mask0 = cv2.inRange(im_hsv, lower_red, upper_red)
- # logging.debug(mask0)
- logging.debug('mask1')
- lower_red = np.array([156, 43, 46])
- upper_red = np.array([180, 255, 255])
- mask1 = cv2.inRange(im_hsv, lower_red, upper_red)
- # logging.debug(mask1)
- mask = mask0 + mask1
- roi_red = cv2.bitwise_and(im_crop, im_crop, mask=mask)
- _show_img(roi_red, 'roi_red')
- im_bgr = cv2.cvtColor(roi_red, cv2.COLOR_HSV2BGR)
- im_gray = cv2.cvtColor(im_bgr, cv2.COLOR_BGR2GRAY)
- _show_img(im_gray, 'im_gray')
- _, im_thresh = cv2.threshold(im_gray, 200, 255, cv2.THRESH_BINARY)
- _show_img(im_thresh, 'im_thresh')
- kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
- im_thresh_erode = cv2.erode(im_thresh, kernel, iterations=1)
- _show_img(im_thresh_erode, 'im_thresh_erode')
- kernel = cv2.getStructuringElement(cv2.MORPH_RECT, (3, 3))
- im_thresh_dilate = cv2.dilate(im_thresh_erode, kernel, iterations=1)
- _show_img(im_thresh_dilate, 'im_thresh_dilate')
- cnts, _ = cv2.findContours(im_thresh_dilate, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
- cnts = sorted(cnts, key=cv2.contourArea, reverse=True)
- num = len(cnts)
- if num == 0:
- logging.debug('not found cnts')
- return zoom_key, um_per_pix
- x_list = []
- for i in range(num):
- x, y, w, h = cv2.boundingRect(cnts[i])
- if w < 5 or h < 10:
- continue
- x_list.append([x, y, w, h])
- num = len(x_list)
- if num < 2:
- logging.debug('all cnts small')
- return zoom_key, um_per_pix
- logging.debug('found {} big cnts'.format(num))
- res_x_list = sorted(x_list, key=lambda x: x[0]) # sort by x
- if debug_img:
- im_debug = im_crop.copy()
- for i in range(num):
- (x,y,w,h) = res_x_list[i]
- cv2.rectangle(im_debug, (x,y), (x+w,y+h), (0,255,0), 2)
- _show_img(im_debug, 'draw {}/{} contour boxe: {}x{}'.format(i+1, num, w, h))
- iVal255 = []
- if num == 5:
- zoom_key = 100
- else:
- x, y, w, h = res_x_list[1]
- img1 = im_thresh[y:y + h, x:x + w]
- res = cv2.resize(img1, (40, 50), interpolation=cv2.INTER_LINEAR) # resize to match template size
- root_dir = os.getenv('VI_OPTON')
- temp_file = os.path.join(root_dir, 'opton_core', 'read_image_scale.npz')
- temp_list = np.load(temp_file)
- for i in temp_list.keys():
- img_and = cv2.bitwise_and(res, res, mask=temp_list[i])
- iVal255.append(cv2.countNonZero(img_and))
- index_num = iVal255.index(max(iVal255))
- if index_num == 0:
- zoom_key = 200
- elif index_num == 1:
- zoom_key = 500
- if num == 6:
- zoom_key = 50
- elif index_num == 2:
- zoom_key = 1000
- if num == 6:
- zoom_key = 10
- if zoom_key is None:
- return zoom_key, um_per_pix
- scale_len_pix = 0
- for x in res_x_list:
- if x[2] > scale_len_pix:
- scale_len_pix = x[2]
- um_per_pix = float(10000 / zoom_key) / scale_len_pix
- return zoom_key, um_per_pix
- def _draw_ruler(im_inp, ruler_cnts):
- if ruler_cnts is None:
- return
- (crop_bbox, bg_bbox, outer_cnts, inner_cnts, ruler_bbox, mark_bbox) = ruler_cnts
- (cx1,cy1,cx2,cy2) = crop_bbox
- im_cnt = None
- im_copy = None
- if bg_bbox is not None:
- (bx1,by1,bx2,by2) = bg_bbox
- im_cnt = im_inp[by1:by2,bx1:bx2,:]
- cv2.rectangle(im_cnt, (bx1,by1), (bx2,by2), (255,255,255), -1)
- cv2.drawContours(im_cnt, outer_cnts, -1, (0, 0, 0), -1)
- cv2.drawContours(im_cnt, inner_cnts, -1, (255, 255, 255), -1)
- else:
- im_cnt = im_inp[cy1:cy2,cx1:cx2,:]
- im_copy = im_cnt.copy()
- cv2.drawContours(im_cnt, outer_cnts, -1, (0, 0, 255), -1)
- for cnt in inner_cnts:
- (x,y,w,h) = cv2.boundingRect(cnt)
- im_cnt[y:y+h,x:x+w,:] = im_copy[y:y+h,x:x+w,:]
-
- # draw ruler bbox
- if ruler_bbox is not None:
- (x1,y1,x2,y2) = ruler_bbox
- cv2.rectangle(im_cnt, (x1,y1), (x2,y2), (0,255,0), 2)
- # draw marker bbox
- if mark_bbox is not None:
- (x1,y1,x2,y2) = mark_bbox
- cv2.rectangle(im_cnt, (x1,y1), (x2,y2), (255,0,0), 2)
- debug_img = False
- def _show_img(im_inp, msg=None):
- if debug_img:
- if msg is not None:
- print('[IMAGE] ' + msg)
- h,w = im_inp.shape[:2]
- cv2.namedWindow("Example", cv2.WINDOW_NORMAL)
- cv2.resizeWindow('Example', int(w*1.0), int(h*1.0))
- cv2.moveWindow('Example', 600, 20)
- cv2.imshow("Example", im_inp)
- k = cv2.waitKey()
- if k == 27:
- cv2.destroyAllWindows()
- if __name__ == '__main__':
- vi_lib_dir = os.getenv('VI_LIB')
- vi_opton_dir = os.getenv('VI_OPTON')
- opton_pred_dir = os.path.join(vi_opton_dir, 'opton_pred_v2')
- sys.path.append(vi_lib_dir)
- sys.path.append(vi_opton_dir)
- sys.path.append(opton_pred_dir)
- if os.path.isdir(sys.argv[1]):
- from_dir = sys.argv[1]
- # to_dir = sys.argv[2]
- to_dir = '/home/reacher/Pictures/ruler'
- print('save output to ' + to_dir)
- names = os.listdir(from_dir)
- names = [n.strip() for n in names if n.strip().lower().endswith('.jpg')]
- num = len(names)
- for i in range(num):
- basename, ext = os.path.splitext(names[i])
- from_jpg = os.path.join(from_dir, names[i])
- im_inp = cv2.imread(from_jpg)
- zoom_key, um_per_pix, ruler_cnts = read_scale(im_inp)
- suffix = 'fail'
- if zoom_key is not None:
- suffix = zoom_key
- if um_per_pix is not None:
- suffix = '{}_{:.3f}'.format(suffix, um_per_pix)
- else:
- suffix = '{}_fail'.format(suffix)
- to_jpg = os.path.join(to_dir, '{}_{}_inp.jpg'.format(basename, suffix))
- cv2.imwrite(to_jpg, im_inp)
- to_jpg = os.path.join(to_dir, '{}_{}_out.jpg'.format(basename, suffix))
- _draw_ruler(im_inp, ruler_cnts)
- cv2.imwrite(to_jpg, im_inp)
- print('{}/{} {:80} {}'.format(i+1, num, names[i], suffix))
- else:
- logging.basicConfig(
- format='%(asctime)s [%(levelname)s] - %(message)s',
- level=logging.DEBUG)
- jpg_file = sys.argv[1]
- im_inp = cv2.imread(jpg_file)
- zoom_key, um_per_pix, ruler_cnts = read_scale(im_inp)
- print(zoom_key, um_per_pix)
- _draw_ruler(im_inp, ruler_cnts)
- _show_img(im_inp)
|