Head vs breakz

[Chatbot]-명함에서 글씨 추출 본문

Head/chatbot

[Chatbot]-명함에서 글씨 추출

headbreakz 2020. 1. 16. 17:40

명함 추출 과정

image_copy=body.copy()
gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
gray = cv2.GaussianBlur(gray, (9, 9), 0)

thr,mask=cv2.threshold(gray,0,255,cv2.THRESH_BINARY +cv2.THRESH_OTSU)
opened = cv2.morphologyEx(mask,cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)),iterations=7)
contours, _ = cv2.findContours(opened, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

for c in contours:
	peri = cv2.arcLength(c, True)
	approx = cv2.approxPolyDP(c, 0.02 * peri, True)

	if len(approx) == 4:

        cv2.drawContours(image_copy, [approx], -1, (0, 255, 0), 4)
        pts1 = np.array(approx, dtype="float32")
        pts2 = np.float32([[width,0],[0,0],[0,height],[width,height]])
        M = cv2.getPerspectiveTransform(pts1,pts2)
        image_result = cv2.warpPerspective(image_copy, M, (int(width),int(height)))
str = pytesseract.image_to_string(image_result)

 

웹서비스에서 사용

import numpy as np
import cv2
from flask import Flask, escape, request
import urllib.request
from urllib.parse import urlparse

@app.route('/', methods=['POST'])

def hi():
    return{
        "version": "2.0",
        "template": {
            "outputs": [
                {
                    "simpleText": {
                        "text": " 멋쟁이"
                    }
                }
            ]
        }
    }

@app.route('/input_namecard', methods=['POST'])

def namecard():
    body = request.json
    img_url = body['userRequest']['utterance']

	urllib.request.urlretrieve(img_url,./image/image1.png)
    img_url_2=urllib.request.urlopen(img_url)
    image_copy=img_url_2

	gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (9, 9), 0)
    thr,mask=cv2.threshold(gray,0,255,cv2.THRESH_BINARY +cv2.THRESH_OTSU)
    opened = cv2.morphologyEx(mask,cv2.MORPH_OPEN,cv2.getStructuringElement(cv2.MORPH_RECT,(3,3)),iterations=7)
    contours, _ = cv2.findContours(opened, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for c in contours:
        peri = cv2.arcLength(c, True)
        approx = cv2.approxPolyDP(c, 0.02 * peri, True)
        if len(approx) == 4:
            cv2.drawContours(image_copy, [approx], -1, (0, 255, 0), 4)
            pts1 = np.array(approx, dtype="float32")
            pts2 = np.float32([[width,0],[0,0],[0,height],[width,height]])
            M = cv2.getPerspectiveTransform(pts1,pts2)
            image_result = cv2.warpPerspective(image_copy, M, (int(width),int(height)))
    result = pytesseract.image_to_string(image_result)

    return result 
Comments