-
Notifications
You must be signed in to change notification settings - Fork 2
/
app.py
92 lines (71 loc) · 2.56 KB
/
app.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
import os
import sys
import numpy as np
import cv2
import json
import pandas as pd
import imutils
from imutils import paths
# Flask
from flask import Flask, redirect, url_for, request, render_template, Response, jsonify, redirect
from werkzeug.utils import secure_filename
from gevent.pywsgi import WSGIServer
from util import base64_to_pil
from util import np_to_base64
# Declare a flask app
app = Flask(__name__)
from IPython.display import Image
def model_predict(img1,img2):
#.......open cv here
# img1 = cv2.imread("images/s1.png")
# img2 = cv2.imread("images/s2.png")
img1=(np.array(img1))
img2=(np.array(img2))
if(len(img1.shape)==2):
img1 = cv2.cvtColor(img1,cv2.COLOR_GRAY2BGR)
if(len(img2.shape)==2):
img2 = cv2.cvtColor(img2,cv2.COLOR_GRAY2BGR)
images = [img1,img2]
stitcher = cv2.Stitcher_create()
(status, stitched) = stitcher.stitch(images)
stitched = cv2.copyMakeBorder(stitched, 10, 10, 10, 10,
cv2.BORDER_CONSTANT, (0, 0, 0))
gray = cv2.cvtColor(stitched, cv2.COLOR_BGR2GRAY)
thresh = cv2.threshold(gray, 0, 255, cv2.THRESH_BINARY)[1]
cnts = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
mask = np.zeros(thresh.shape, dtype="uint8")
(x, y, w, h) = cv2.boundingRect(c)
cv2.rectangle(mask, (x, y), (x + w, y + h), 255, -1)
minRect = mask.copy()
sub = mask.copy()
while cv2.countNonZero(sub) > 0:
minRect = cv2.erode(minRect, None)
sub = cv2.subtract(minRect, thresh)
cnts = cv2.findContours(minRect.copy(), cv2.RETR_EXTERNAL,
cv2.CHAIN_APPROX_SIMPLE)
cnts = imutils.grab_contours(cnts)
c = max(cnts, key=cv2.contourArea)
(x, y, w, h) = cv2.boundingRect(c)
stitched = stitched[y:y + h, x:x + w]
# cv2.imwrite("output.png", stitched)
return stitched
@app.route('/', methods=['GET'])
def index():
# Main page
return render_template('index.html')
@app.route('/predict', methods=['GET', 'POST'])
def predict():
if request.method == 'POST':
img1,img2=base64_to_pil(request.json)
img3=model_predict(img1,img2)
img3=cv2.resize(img3, (400,300),interpolation=cv2.INTER_AREA)
img3=np_to_base64(img3)
#return jsonify({'image_url': '/output.png'})
return jsonify(img3)
return None
if __name__ == '__main__':
http_server = WSGIServer(('0.0.0.0', 4000), app)
http_server.serve_forever()