def crop(image): r = model_crop.detect([image], verbose=1)[0] mask = r["masks"][:, :, 0] mask_image = np.reshape(mask == 1, (mask.shape[0], mask.shape[1], 1)).astype(np.uint8) cnts = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] im = None if len(cnts) != 0: c = max(cnts, key=cv2.contourArea) box = cv2.boxPoints(cv2.minAreaRect(c)) im = transform.four_point_transform(image, box) if im.shape[0] > im.shape[1]: im = cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE) return im
def crop(image): global sess, graph start_time = time.time() im = None with sess.as_default(): with graph.as_default(): r = model_crop.detect([image], verbose=1)[0] process_time = round(time.time() - start_time) if r["masks"].shape[2] == 0: return im, 0 mask = r["masks"][:, :, 0] mask_image = np.reshape(mask == 1, (mask.shape[0], mask.shape[1], 1)).astype(np.uint8) cnts = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] if len(cnts) != 0: c = max(cnts, key=cv2.contourArea) box = cv2.boxPoints(cv2.minAreaRect(c)) im = transform.four_point_transform(image, box) if im.shape[0] > im.shape[1]: im = cv2.rotate(im, cv2.ROTATE_90_COUNTERCLOCKWISE) return im, process_time
for cnt in cnts: peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, .02 * peri, True) if len(approx) == 4: docCnt = approx break print("SETP 2: Find contours of paper") cv2.drawContours(image, [docCnt], -1, (0, 0, 255), 2) plt.subplot(1, 3, 1) plt.imshow(image[..., (2, 1, 0)]) plt.title("Outline") print("STEP 3: Apply perspective transform") paper = four_point_transform(original, docCnt.squeeze()) warped = four_point_transform(gray, docCnt.squeeze()) ret, thresh = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) plt.subplot(1, 3, 2) plt.imshow(warped, cmap='gray') plt.title("Warped") plt.subplot(1, 3, 3) plt.imshow(thresh, cmap='gray') plt.title("Thresh") plt.show() print("STEP 4: Find contours of bubbles") cnts, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) questionCnts = [] for cnt in cnts:
def main(input, output, padding, model_name, model_path, verbose, show_size, max_size, images_per_gpu, gpus, quality): image_paths = get_images(input) # can not exceed the length of images images_per_gpu = min(images_per_gpu, len(image_paths)) if images_per_gpu < 1: return config = CropConfig(model_name, max_size, images_per_gpu, gpus) # config.IMAGE_MAX_DIM = max_size # config.IMAGE_MIN_DIM = min_size # config.IMAGE_SHAPE = np.array([config.IMAGE_MAX_DIM, config.IMAGE_MAX_DIM, # config.IMAGE_CHANNEL_COUNT]) if verbose: config.display() model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config) model.load_weights(model_path, by_name=True) image_paths = list(chunks(image_paths, images_per_gpu)) need_wait = False for chunk_paths in image_paths: origin_images = [] for path in chunk_paths: origin_image = cv2.imread(path) if origin_image is not None: origin_images.append(origin_image) if len(origin_images) < 1: continue basenames = [ os.path.basename(path).split(".")[0] for path in chunk_paths ] # fix model config # model.config.BATCH_SIZE = len(chunk_paths) if len(chunk_paths) < images_per_gpu: for i in range(0, images_per_gpu - len(chunk_paths)): origin_images.append(np.zeros((1, 1, 3), np.uint8)) # process image start = time.time() result = model.detect(origin_images, verbose=verbose) cost_time = (time.time() - start) click.secho("cost time: {:.2f}s".format(cost_time), fg="yellow") for index, r in enumerate(result): origin_image = origin_images[index] if origin_image.shape[0] < 10: continue basename = basenames[index] for i in range(0, len(r['scores'])): mask = r["masks"][:, :, i] mask_image = np.reshape( mask == 1, (mask.shape[0], mask.shape[1], 1)).astype(np.uint8) cnts = cv2.findContours(mask_image, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[-2] if len(cnts) != 0: c = max(cnts, key=cv2.contourArea) # try to find quadrilateral # epsilon = 0.01 * cv2.arcLength(c, True) # approx = cv2.approxPolyDP(c, epsilon, True) # if len(approx) == 4: # box = np.squeeze(approx, axis=1) # else: # box = cv2.boxPoints(cv2.minAreaRect(c)) box = cv2.boxPoints(cv2.minAreaRect(c)) # apply the perspective transformation img = transform.four_point_transform(origin_image, box) # fix clockwise if img.shape[0] > img.shape[1]: img = cv2.rotate(img, cv2.ROTATE_90_COUNTERCLOCKWISE) h, w, _ = img.shape if quality == 100: file_name = "{}/{}_{}.png".format(output, basename, i) else: file_name = "{}/{}_{}.jpg".format(output, basename, i) if not output: cv2.namedWindow(file_name, cv2.WINDOW_NORMAL) cv2.resizeWindow(file_name, show_size, int(show_size * (h / w))) cv2.imshow(file_name, img) else: if quality == 100: cv2.imwrite(file_name, img) else: cv2.imwrite( file_name, img, [int(cv2.IMWRITE_JPEG_QUALITY), quality]) if not output: cv2.waitKey(0)
plt.show() cnts, hier = cv2.findContours(edged, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) cnts = sorted(cnts, key=cv2.contourArea, reverse=True) # 找到显示屏 for cnt in cnts: peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, .02 * peri, True) if len(approx) == 4: displayCnt = cnt break # 透视变换 warped = four_point_transform(gray, displayCnt.squeeze()) output = four_point_transform(image, displayCnt.squeeze()) plt.subplot(2, 2, 1) plt.imshow(warped, cmap='gray') plt.title("Warped") # 使用大津算法确定的阈值进行阈值化,将显示的内容凸显出来 ret, thresh = cv2.threshold(warped, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU) # 使用形态学闭运算操作,让每个数字的各个区域连在一起 thresh = cv2.morphologyEx(thresh, cv2.MORPH_CLOSE, None) cnts, hier = cv2.findContours(thresh, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE) digitCnts = []
def recognize(): UPLOAD_FOLDER = "uploads" img = None file = request.files['file'] filename = secure_filename(file.filename) filename = filename + ".png" destination = os.path.join(UPLOAD_FOLDER, filename) file.save(destination) original_image = cv2.imread(destination) if helper.elements(np.array(original_image)) == 0: return jsonify({ "error": { "message": "Không nhận diện được ảnh" }, "data": {} }), 200 img, process_card = crop(original_image) if helper.elements(np.array(img)) == 0: return jsonify({ "error": { "message": "Không nhận diện được ảnh" }, "data": {} }), 200 h, w, _ = img.shape if h <= 500: return jsonify({ "error": { "message": "Kích thước ảnh hơi nhỏ" }, "data": {} }), 200 else: rotate_image = cv2.rotate(img, cv2.ROTATE_180) if helper.rotate_face(img): img = rotate_image original_image_crop = img.copy() print("shape: ", original_image_crop.shape) img = utils.resize(img, height=900) src_image = img.copy() cv2.imwrite("test.png", img) rh = original_image_crop.shape[0] / img.shape[0] rw = original_image_crop.shape[1] / img.shape[1] centers = segmentWord(img) images = [] model_names = [] # group image with line for key in centers: model_name = "vie" boxes = np.asarray(centers[key]) boxes = boxes.reshape(-1, 2) minBox = cv2.minAreaRect(np.array(boxes)) minBox = utils.fix_rect(minBox, 3) box = cv2.boxPoints(minBox) cnt = np.int0(box) # return origin_image original_box = np.array([[x * rw, y * rh] for x, y in box], dtype="float32") img_ind = transform.four_point_transform(original_image_crop, original_box) cv2.drawContours(src_image, [np.array(cnt)], -1, (0, 255, 0), 2) if int(key) == len(centers) - 3: h, w, _ = img_ind.shape img_ind = img_ind[0:h, int(w / 6):w] # img_ind = cv2.resize(img_ind, None, fx=1.5, # fy=1.5, interpolation=cv2.INTER_CUBIC) model_name = "ID" img_ind = cv2.detailEnhance(img_ind, sigma_s=10, sigma_r=0.015) images.append(img_ind) model_names.append(model_name) start = time.time() result = pool.map(helper.detect, [(im, model_name) for (im, model_name) in zip(images, model_names)]) print(result) cv2.imwrite("test_2.png", src_image) # print(result.get(timeout=2)) # result = list(result) raw_data = result result = helper.extraction(result) cost_time = (time.time() - start) result["process_time"] = round(process_card + cost_time, 2) return jsonify({ "error": { "message": "Thành công" }, "data": result }), 200
peri = cv2.arcLength(cnt, True) approx = cv2.approxPolyDP(cnt, .02 * peri, True) if len(approx) == 4: screenCnt = approx break print("SETP 2: Find contours of paper") cv2.drawContours(image, [screenCnt], -1, (0, 255, 0), 2) plt.subplot(1, 4, 1) plt.imshow(image[..., (2, 1, 0)]) plt.title("Outline") print("STEP 3: Apply perspective transform") # 除以ration进行还原 warped = four_point_transform(original, screenCnt.squeeze() / ratio) plt.subplot(1, 4, 2) plt.imshow(warped[..., (2, 1, 0)]) plt.title("Warped") # 使用scikit-image包里的threshold_local将灰度图片转换为阈值 warped = cv2.cvtColor(warped, cv2.COLOR_BGR2GRAY) plt.subplot(1, 4, 3) plt.imshow(warped, cmap='gray') plt.title("Gray") T = threshold_local(warped, 11, offset=10, method="gaussian") warped = (warped > T).astype("uint8") * 255 plt.subplot(1, 4, 4) plt.imshow(warped, cmap='gray') plt.title("Scanned")
def main(image): mean = np.array([103.939, 116.779, 123.68]) img = cv2.imread(image) # img = crop(img) # print(img.shape) rotate_image = cv2.rotate(img, cv2.ROTATE_180) if helper.rotate_face(img): img = rotate_image cv2.imwrite("test.png", img) original_image = img.copy() img = utils.resize(img, height=1000) rh = original_image.shape[0] / img.shape[0] rw = original_image.shape[1] / img.shape[1] src_image = img.copy() src_image_2 = src_image.copy() src_image_3 = img.copy() h, w = img.shape[:2] img = resize_image(img) img = img.astype(np.float32) img -= mean image_input = np.expand_dims(img, axis=0) start = time.time() p = model.predict(image_input)[0] bitmap = p > 0.3 boxes, scores = polygons_from_bitmap(p, bitmap, w, h, box_thresh=0.5) predict_time = round(time.time() - start, 2) rects = [cv2.minAreaRect(np.array(box)) for box in boxes] centers = utils.group_box_row(src_image, rects, boxes) images = [] model_names = [] print("predict: ", predict_time) for box in boxes: cv2.drawContours(src_image_2, [np.array(box)], -1, (0, 255, 0), 2) for key in centers: model_name = "vie" boxes = np.asarray(centers[key]) boxes = boxes.reshape(-1, 2) minBox = cv2.minAreaRect(np.array(boxes)) minBox = utils.fix_rect(minBox, 3) box = cv2.boxPoints(minBox) cnt = np.int0(box) cv2.drawContours(src_image, [np.array(cnt)], -1, (0, 255, 0), 2) img_ind = transform.four_point_transform(src_image_3, box) # return origin_image original_box = np.array([[x * rw, y * rh] for x, y in box], dtype="float32") img_ind = transform.four_point_transform(original_image, original_box) if int(key) == len(centers) - 3: h, w, _ = img_ind.shape img_ind = img_ind[0:h, int(w / 7) + 10:w] # img_ind = cv2.resize(img_ind, None, fx=1.5, # fy=1.5, interpolation=cv2.INTER_CUBIC) model_name = "ID" cv2.imwrite("crop/img_imd_{}.png".format(key), img_ind) images.append(img_ind) model_names.append(model_name) # images.reverse() cv2.namedWindow('image', cv2.WINDOW_NORMAL) cv2.imshow('image', src_image) cv2.imshow("image_2", src_image_2) cv2.waitKey(0) start = time.time() result = helper.detect_images(images, model_names) print(result) # result = list(result) result = helper.extraction(result) cost_time = (time.time() - start) print(cost_time) print(result)