mardi 4 septembre 2018

How do I give my python code a web interface

I need a little help on how to give python code a web interface. I know how to deal with html/css a bit so thats not the issue. I also know how to use python flask to connect a web page to a db (not using a db here but just info for reference) but I would like to know how to perform this type of connection with flask.

How would I make my code execute off the press of a button on a html page?

python code:

import numpy as np
import argparse
import cv2

def skew_correction():

    global x
    x = 'page-1.png'
    global image
    image = cv2.imread(x, 1)

    gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
    gray = cv2.bitwise_not(gray)

    thresh = cv2.threshold(gray, 0, 255,
                           cv2.THRESH_BINARY | cv2.THRESH_OTSU)[1]

    coords = np.column_stack(np.where(thresh > 0))
    angle = cv2.minAreaRect(coords)[-1]

    if angle < -45:
        angle = -(90 + angle)

    else:
        angle = -angle

    (h, w) = image.shape[:2]
    center = (w // 2, h // 2)
    M = cv2.getRotationMatrix2D(center, angle, 1.0)
    global rotated
    rotated = cv2.warpAffine(image, M, (w, h),
                             flags=cv2.INTER_CUBIC, borderMode=cv2.BORDER_REPLICATE)

    """cv2.putText(rotated, "Angle: {:.2f} degrees".format(angle),
                (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)"""

    # print("[INFO] angle: {:.3f}".format(angle))
    """cv2.imshow("Original", image)
    cv2.imshow("Skew Corrected", rotated)
    cv2.waitKey(0)"""

def gamma_correction():

    def adjust_gamma(image, gamma=1.0):
        invGamma = 1.0 / gamma
        table = np.array([((i / 255.0) ** invGamma) * 255
                          for i in np.arange(0, 256)]).astype("uint8")

        return cv2.LUT(image, table)

    #original = cv2.imread(rotated, 1)
    # cv2.imshow('original', original)

    print("Choose a value between 0-1 for gamma correction:")
    print("[Choose 0.0 to exit]")

    def apply_gamma():
        while True:
            gamma = float(input("\nWhat value would you like the gamma correction to be? "))
            if 1 > gamma > 0:
                global adjusted
                adjusted = adjust_gamma(rotated, gamma=gamma)
                #cv2.putText(adjusted, "g={}".format(gamma), (10, 30), cv2.FONT_HERSHEY_SIMPLEX, 0.8, (0, 0, 255), 3)
                """cv2.imshow('Original', original)"""
                cv2.imshow("Gamma corrected image", adjusted)
                cv2.waitKey(0)
                cv2.destroyAllWindows()

                print('\n[press "y" to continue. Press "n" to readjust"]')
                while True:
                    gc = input('Are you satisfied with the image? ')

                    if gc == 'n':
                        apply_gamma()
                    if gc == 'y':
                        print('\nDenoise is next')

                        break
                    else:
                        print('\nInvalid input')
                    gc = True

            break

    apply_gamma()

def denoise():

    threshold = 85
    cv2.threshold(adjusted, threshold, 255, cv2.THRESH_BINARY, adjusted)
    cv2.imshow('SDG Image.', adjusted)
    cv2.waitKey(0)




if __name__ == "__main__":
    skew_correction()
    gamma_correction()
    denoise()

I also already have a start from the button on a html page. I Just don't know how to connect the two.

Web page:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>


<form method="post" enctype="multipart/form-data">
 <div>
   <label for="file">Please choose an image for SDG</label>
   <input type="file" id="file" name="file" multiple>
 </div>
 <div>
   <button>Submit</button>
 </div>




</form>






</body>
</html>

Thanks guys,




Aucun commentaire:

Enregistrer un commentaire