使用Python操作从PHP页面检索的照片

我想用Python创建一个伪验证API,用于处理图像(工人的ID)。它应该基于图像中的某些模式(如圆形、方形等,类似于Snapchat著名的系统)。如何在用户通过普通的HTML文件输入表单发送图像时运行Python脚本?我是说用户将照片发送到服务器,服务器运行Python脚本并给出结果?提前感谢。

我在网上查找这个问题时,没有找到任何解决方案。似乎这是一个非常昂贵的操作,可能不是最优的,但尽管如此,我还是想这样做。


回答:

你可以使用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 sysimport cv2import timecamera = cv2.VideoCapture(0)if (camera.isOpened() == False):    print("Can not open camera #0.")    sys.exit(0)print("Camera ready")doAgain = Truewhile 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 = Falsecamera.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 formif ($_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";}?>

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

<!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的公共托管进行测试。

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注