brajshukl commited on
Commit
dbac6fe
·
1 Parent(s): 1d3930f

Upload views.py

Browse files
Files changed (1) hide show
  1. app/views.py +68 -0
app/views.py ADDED
@@ -0,0 +1,68 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ from app import app
2
+ from flask import request, render_template
3
+ import os
4
+ from skimage.metrics import structural_similarity
5
+ import imutils
6
+ import cv2
7
+ from PIL import Image
8
+
9
+ # adding path of config
10
+ app.config['INITIAL_FILE_UPLOADS'] = 'app/static/uploads'
11
+ app.config['EXTISTING_FILE'] = 'app/static/original'
12
+ app.config['GENERATED_FILE'] = 'app/static/generated'
13
+
14
+ # route to home page
15
+ @app.route("/", methods=["GET", "POST"])
16
+ def index():
17
+
18
+ # Execute if requst is get
19
+ if request.method == "GET":
20
+ return render_template(index.html)
21
+
22
+ # Execute if request is post
23
+ if request.method == "POST":
24
+ # Get uploaded image
25
+ file_upload = request.files['file_upload']
26
+ filename = file_upload.filename
27
+
28
+ # Resize and save uploaded image
29
+ uploaded_image = Image.open(file_upload).resize((250, 160))
30
+ uploaded_image.save(os.path.join(app.config['INITIAL_FILE_UPLOADS'], 'IMAGE.JPG'))
31
+
32
+ # Resize and save the original image to ensure both uploaded and original matches is size
33
+ original_image = Image.open(os.path.join(app.config['EXISTING_FILE'], 'image.jpg')).resize((250,160))
34
+ original_image.save(os.path.join(app.config['EXISTING_FILE'], 'image.jpg'))
35
+
36
+ # Read uploaded and original image as array
37
+ original_image = cv2.imread(os.path.join(app.config['EXISTING_FILE'], 'image.jpg'))
38
+ uploaded_image = cv2.imread(os.path.join(app.config['INITIAL_FILE_UPLOADS'], 'image.jpg'))
39
+
40
+ # Convert image into grayscale
41
+ original_gray = cv2.cvtColor(original_image, cv2.COLOR_BGR2GRAY)
42
+ uploaded_gray = cv2.cvtColor(uploaded_image, cv2.COLOR_BGR2GRAY)
43
+
44
+ # Calculate structure similarity
45
+ (score, diff) = structural_similarity(original_gray, uploaded_gray, full=True)
46
+ diff = (diff * 255).astype('uint8')
47
+
48
+ # Calculate thresold and contours
49
+ thresh = cv2.threshold(diff, 0, 255, cv2.THRESH_BINARY_INV | cv2.THRESH_OTSU)[1]
50
+ cnts = cv2.findCountours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
51
+ cnts = imutils.grab_contours(cnts)
52
+
53
+ # Draw contours on image
54
+ for c in cnts:
55
+ (x, y, w, h) = cv2.boundingRect(c)
56
+ cv2.rectangle(original_image, (x, y), (x+w, y+h), (0, 0, 255), 2)
57
+ cv2.rectangle(uploaded_image, (x, y), (x+w, y+h), (0, 0, 255), 2)
58
+
59
+ # Save all output image (if required)
60
+ cv2.imwrite(os.path.join(app.config['GENERATE_FILE'], 'image_original.jpg'), original_image)
61
+ cv2.imwrite(os.path.join(app.config['GENERATED_FILE'], 'image_uploaded.jpg'), uploaded_image)
62
+ cv2.imwrite(os.path.join(app.config['GENERATED_FILE'], 'image_diff.jpg'), diff)
63
+ cv2.imwrite(os.path.join(app.config['GENERATED_FILE'], 'image_thresh.jpg'), thresh)
64
+ return render_template('index.html',pred=str(round(score*100,2)) + '%' + 'correct')
65
+
66
+ # main function
67
+ if __name__ == '__main__':
68
+ app.run(debug=True)