-
Notifications
You must be signed in to change notification settings - Fork 0
/
application.py
96 lines (79 loc) · 3.06 KB
/
application.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
# -*- coding: utf-8 -*-
import os
import sys
import json
import random
import datetime
import string
import traceback
import requests
import copy
import io
import re
import base64
from functools import wraps
import flask
from flask import request, Response, send_file, render_template, make_response, flash
from flask import session, redirect, send_from_directory, jsonify, _request_ctx_stack
from flask import Flask
from flask_cors import CORS
from werkzeug.local import LocalProxy
import cv2
import numpy as np
from PIL import Image
# Getting server filepath
scriptpath = os.path.dirname(os.path.realpath(__file__))
# Create the Flask app
application = flask.Flask(__name__)
application.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
CORS(application, origins=os.environ.get('ORIGIN'))
def arrayIntoBase64String(imgArr):
pil_img = Image.fromarray(imgArr, mode='RGB')
buff = io.BytesIO()
pil_img.save(buff, format="JPEG")
return base64.b64encode(buff.getvalue()).decode("utf-8")
def getLines(grayImg, edges, lineRho, lineTheta, lineThreshold, lineMinLength, lineMaxGap):
lines = cv2.HoughLinesP(edges, lineRho, lineTheta, lineThreshold, minLineLength=lineMinLength,maxLineGap=lineMaxGap)
grayImg = cv2.cvtColor(grayImg, cv2.COLOR_GRAY2RGB)
if lines is None:
return grayImg
for line in lines:
x1,y1,x2,y2 = line[0]
cv2.line(grayImg,(x1,y1),(x2,y2),(0,255,0),2)
return grayImg
def getCorners(grayImg, cornerBlockSize, cornerKSize, cornerK):
grayImg = np.float32(grayImg)
dst = cv2.cornerHarris(grayImg, cornerBlockSize, cornerKSize, cornerK)
#result is dilated for marking the corners, not important
dst = cv2.dilate(dst,None)
# Threshold for an optimal value, it may vary depending on the image.
grayImg = cv2.cvtColor(grayImg, cv2.COLOR_GRAY2RGB)
grayImg[dst>0.01*dst.max()]=[0,0,255]
cornerImg = np.uint8(grayImg)
return cornerImg
@application.route('/api/v1/featureprocessing', methods=['POST'])
def apiResponse():
postData = request.get_json()
imgData = postData['imgData']
imgData = Image.open(io.BytesIO(base64.b64decode(imgData)))
imgData = np.array(imgData)
grayImg = cv2.cvtColor(imgData, cv2.COLOR_BGR2GRAY)
grayImg2 = copy.copy(grayImg)
edges = cv2.Canny( grayImg,
float(postData['edgeMinVal']),
float(postData['edgeMaxVal']) )
edgeImgBase64 = arrayIntoBase64String(np.dstack([edges, edges, edges]))
lineImgBase64 = arrayIntoBase64String(getLines(grayImg, edges,
float(postData['lineRho']),
float(postData['lineTheta']),
int(float(postData['lineThreshold'])),
float(postData['lineMinLength']),
float(postData['lineMaxGap']) ))
cornerImgBase64 = arrayIntoBase64String(getCorners(grayImg2,
int(float(postData['cornerBlockSize'])),
int(float(postData['cornerKSize'])),
float(postData['cornerK']) ))
return jsonify({'edgeImg': edgeImgBase64, 'lineImg': lineImgBase64, 'cornerImg': cornerImgBase64 })
# App stuff
if __name__ == '__main__':
application.run(port=os.environ['PORT'])