代码之家  ›  专栏  ›  技术社区  ›  user25947831

使用python对从php页面检索到的照片进行操作

  •  0
  • user25947831  · 技术社区  · 11 月前

    我想在python中制作一个伪验证api,用于处理图像(工作者的ID)。它应该基于图像中的某些模式,如(snapchat著名系统中的圆圈、正方形等)。当用户使用普通的html输入文件表单发送图像时,我该如何运行python脚本?我的意思是,就像用户将照片发送到服务器一样,它用python运行脚本并给出判断?提前感谢。

    我试着在网上查这个问题。我没有找到任何解决方案。这似乎是一项非常昂贵的操作,而且可能不是最佳的,但无论如何我都想这样做。

    2 回复  |  直到 11 月前
        1
  •  1
  •   dann    11 月前

    您可以尝试使用Flask设置web服务器。通过使用它,您接下来可以处理从HTML表单上传的文件,因此,当上传图像时,您可以使用Python脚本对其进行处理,以检测圆圈和正方形等模式。

    最后,您应该将结果发送回用户。

    要继续,请执行以下操作:

    • 设置烧瓶
    • 用python创建Flask应用程序(有很多好的教程)
    • 创建HTML表单
    • 运行Flask应用程序
    • 访问Web应用程序

    希望这能有所帮助。

        2
  •  0
  •   jbsidis    11 月前

    您可以使用PHP+Python的组合创建该解决方案,有几个库可以操作图像以获得所需的结果,在员工ID验证的情况下,Python脚本应该能够识别正在查看的内容,例如,让我们公司里有一个同事,一个相机设备或类似设备拍摄了ID的照片:

    客户端应用程序需要不断读取相机或图片,以下是相机和本地保存图片的代码:

    Python条形码阅读器:(引入Numpy是增强cv2条形码识别的好主意)

    ##import cv2
    ##img = cv2.imread('/home/jbsidis/Pictures/sofiaID.png')
    ##
    ##barcode_detector = cv2.barcode_BarcodeDetector()
    ##
    ### 'retval' is boolean mentioning whether barcode has been detected or not
    ##retval, points, _ = barcode_detector.detectAndDecode(img)
    ##
    ### copy of original image
    ##img2 = img.copy()
    ##
    ### proceed further only if at least one barcode is detected:
    ##if retval:
    ##    points = points.astype(np.int)
    ##    for i, point in enumerate(points):
    ##        img2 = cv2.drawContours(img2,[point],0,(0, 255, 0),2)
    ##
    ##
    ##print(retval,points,barcode_detector.detectAndDecode(img))
    
    import sys
    import cv2
    import time
    camera = cv2.VideoCapture(0)
    if (camera.isOpened() == False):
        print("Can not open camera #0.")
        sys.exit(0)
    print("Camera ready")
    doAgain = True
    while doAgain:
        ret, image = camera.read()
        if ret:
            qrCodeDetector = cv2.QRCodeDetector()
            text, points, _ = qrCodeDetector.detectAndDecode(image)
            if points is not None:
                print(text)
                cv2.imwrite("./result.jpg",image)
            else:
                print("QR code not detected")
            cv2.imshow("Image", image)
            key = cv2.waitKey(1) & 0xFF
            if key == 27:
                cv2.destroyAllWindows()
                doAgain = False
    camera.release()
    

    服务器端应用程序(PHP):

    <?php
    //server side for the python process
    $target_dir = "uploads/"; //add a directory where to save the file in the server
    //this receives the data from the HTML form
    if ($_SERVER["REQUEST_METHOD"] == "POST") {
        $file = $_FILES["picture"];
        if ($file["type"] != "image/jpeg" && $file["type"] != "image/png" && $file["type"] != "image/gif") {
            echo "This file is not allowed! IMages only please";
            exit();
        }
        if ($file["size"] > 1000000) {
            echo "Error: File size is too large.";
            exit();
        }
        // here we are guessing that you have storage in your server or php server
        $filename = basename($file["name"]);
        $target_path = $target_dir . $filename;
        move_uploaded_file($file["tmp_name"], $target_path);
    
        echo "THanks for uploading the image, Barcode is being recognized!";
    } else {
        echo "No file was uploaded. Try again please";
    }
    if (move_uploaded_file($file["tmp_name"], $target_path)) {
        // this will run the script the python script we created, you have to modify to if this will read the png or the image file or a camera, in this case it must be the camera 
        $python_script = "python /securepathtothescriptorsomeonecouldfindit/to/your/python/script.py " . $target_path;
        $output = shell_exec($python_script);
        echo "Barcode generated successfully!";
    } else {
        echo "There was an error processing your image, please contact support";
    }
    ?>
    

    客户端(访问您的网站或网络应用程序的用户):

    <!DOCTYPE html>
    <html>
    <head>
        <title>User will upload Picture</title>
        <style>
            .card {width: 900px;background-color: #f9f9f9;border: 1px solid #ddd;border-radius: 10px;box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);padding: 20px;margin: 40px auto;}
        </style>
    </head>
    <body>
        <div class="card">
            <h2>Upload Picture</h2>
            <form action="action.php" method="post" enctype="multipart/form-data">
                <input type="file" name="picture" accept="image/*">
                <button type="submit">Upload</button>
            </form>
            <div id="image-preview"></div>
        </div>
    
        <script>
            
            const fileInput = document.querySelector('input[type="file"]');
    
            
            fileInput.addEventListener('change', (event) => {
                const file = event.target.files[0];
                const reader = new FileReader();
    
                reader.onload = (event) => {
                    const imageDataUrl = event.target.result;
    
                    
                    document.getElementById('image-preview').innerHTML = `<img src="${imageDataUrl}" alt="Uploaded Image width=200 height=550">`;
                };
    
                reader.readAsDataURL(file);
            });
        </script>
        </div>
    </body>
    </html>
    

    结果: enter image description here

    为了测试这一点,您可以安装XAMPP或类似的工具来本地运行PHP服务器并在没有云的情况下离线测试,或者您可以使用与PHP兼容的公共主机进行测试。